How to Set Border Style, Margins & Padding of Table in PDF Files in Android Apps
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 Java, 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.
//your code here...//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");
Url: http://www.aspose.com/android/pdf-component.aspx
Language: Java | User: Sheraz Khan | Created: Aug 6, 2015 | Tags: Set Border Style in PDF file Margins set Padding of Table docs Android API Edit Add the table paragraphs collection