article

How to Set Border Style, Margins & Padding of Table in PDF Document

Email
Submitted on: 8/6/2015 5:57:08 PM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 8692
 
     This technical tip shows how developers can set border style, margins & padding of table in PDF docs in Android apps. Aspose.Pdf for Android allows developers to create tables in PDF documents. It also allows them to control table formatting, like setting border style, margins and cell padding. Before going into more technical details, it's important to understand the concepts of border, margins and padding. The borders for tables, rows and cells overlap. Using Aspose.Pdf for .NET, tables can have margins and cells can have paddings. To set cell margins, we have to set cell padding. To set the borders for Table, Row and Cell classes, use the Table.setBorder(..), Row.setBorder(..) and Cell.setBorder(..) methods. Cell borders can also be set using the Table class' setDefaultCellBorder(..) method. All methods discussed above are assigned an instance of the BorderInfo class, which is created by calling its constructor. The BorderInfo constructor has many overloads that take almost all parameters required to customize the border.

 
				This technical tip shows how developers can set border style, margins & padding of table in PDF docs in Android apps. Aspose.Pdf for Android allows developers to create tables in PDF documents. It also allows them to control table formatting, like setting border style, margins and cell padding. Before going into more technical details, it's important to understand the concepts of border, margins and padding. The borders for tables, rows and cells overlap. Using Aspose.Pdf for .NET, tables can have margins and cells can have paddings. To set cell margins, we have to set cell padding. To set the borders for Table, Row and Cell classes, use the Table.setBorder(..), Row.setBorder(..) and Cell.setBorder(..) methods. Cell borders can also be set using the Table class' setDefaultCellBorder(..) method. All methods discussed above are assigned an instance of the BorderInfo class, which is created by calling its constructor. The BorderInfo constructor has many overloads that take almost all parameters required to customize the border. To set a table’s margins or padding, use the Table class' setDefaultCellPadding(..) method. All properties relating to padding are assigned an instance of the MarginInfo class that takes information about the Left, Right, Top and Bottom parameters for creating customized margins. In the following example, we have created a simple table with header row and subsequent rows with three columns each.
The following code snippet shows how to set border style, margins & padding of table in PDF docs in Android apps
//Java Code Sample
// Instantiate Document object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
//Instantiate a table object
Table table1 = new Table();
//Add the table in paragraphs collection of the desired section
page.getParagraphs().add(table1);
//Set with column widths of the table
table1.setColumnWidths("50 50 50");
//Create MarginInfo object and set its left, bottom, right and top margins
MarginInfo margin = new MarginInfo();
margin.setTop(2f);
margin.setLeft(5f);
margin.setRight(5f);
margin.setBottom(5f);
//Set the default cell padding to the MarginInfo object
table1.setDefaultCellPadding(margin);
table1.setRepeatingRowsCount(1);
//Create rows in the table and then cells in the rows
Row row1 = table1.getRows().add();
// Set border for row object
row1.setDefaultCellBorder(new BorderInfo(
BorderSide.Bottom | BorderSide.Top, 1.25F, Color.getBlack()));
// Add columns in first row object
row1.getCells().add("col1");
row1.getCells().add("col2");
row1.getCells().add("col3");
// Create 100 rows with three columns each
for (int i = 0; i <= 100; i++)
{
Row row2 = table1.getRows().add();
row2.getCells().add("ITEM1");
row2.getCells().add("ITEM2");
row2.getCells().add("ITEM3");
}
// Save the resultant PDF
doc.save(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Table_output.pdf");
Overview: Aspose.Pdf for Android
Aspose.Pdf for Android is a PDF document creation & manipulation component that enables Android applications to read, write & manipulate PDF document without using any other third party application. It allows PDF compression options, table creation & manipulation, support for graph objects, extended security controls, custom font handling, add or remove bookmarks, create table of contents, add, update, delete attachments & annotations, import or export PDF form data, print PDF docs & much more.


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.