DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 98 posts at DZone. View Full User Profile

Create FloatingBox Object, Control Its Position & Add Table in It inside Android Apps

03.12.2014
| 3073 views |
  • submit to reddit
// Java code for for Managing TextFragment in FloatingBox

// Instantiate Document object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
// Create FloatingBox object
FloatingBox aBox = new FloatingBox(40, 60);
// Set left position for FloatingBox
aBox.setLeft(40);
// Set Top position for FloatingBox
aBox.setTop(80);
// Set the vertical alignment for FloatingBox
aBox.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Bottom);
// Set Horizontal alignment for FloatingBox
aBox.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
// Add text fragment to paragraphs collection of FloatingBox
aBox.getParagraphs().add(new TextFragment("main text"));
// Set border for FloatingBox
aBox.setBorder(new BorderInfo(15 /*BorderSide.Box*/));
// Set background color for FloatingBox
aBox.setBackgroundColor(com.aspose.pdf.Color.getYellow());
// Add FloatingBox to paragraphs collection of page object
page.getParagraphs().add(aBox);
// Set background of PDF page
page.setBackground(com.aspose.java.awt.Color.LIGHT_GRAY);
// Save the PDF document
doc.save(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FloatingBox.pdf");

// Java code for Adding Table inside FloatingBox

// Instantiate Document object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
// Create FloatingBox object
FloatingBox aBox = new FloatingBox(200, 100);
// Trim the contents exceeding FloatingBox dimensions
aBox.isExtraContentClip(true);
// Specify if you need to repeat the FloatingBox
aBox.isNeedRepeating(false);
// Set left position for FloatingBox
aBox.setLeft(40);
// Set top position for FloatingBox
aBox.setTop(80);
// Set the vertical alignment for FloatingBox
aBox.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Bottom);
// Set horizontal alignment for FloatingBox
aBox.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
// Set border for FloatingBox
aBox.setBorder(new BorderInfo(15 /*BorderSide.Box*/, com.aspose.pdf.Color.getBlack()));
// Set background color for FloatingBox
aBox.setBackgroundColor(com.aspose.pdf.Color.getYellow());
// Add FloatingBox to paragraphs collection of page object
page.getParagraphs().add(aBox);

// Instantiate Table object
com.aspose.pdf.Table table = new com.aspose.pdf.Table();
// Set width of table columns
table.setColumnWidths("100 100");

// Create a loop object
for (int i = 1; i <= 20; i++)
{
    // Create a new row object and add it to table object
    Row row = table.getRows().add();
    // Add cell to cells collection of newly added row object
    row.getCells().add("Columns 1, line " + i);
    // Add another cell object
    row.getCells().add("Columns 2, line " + i);
}
// Add table to paragraphs collection of FloatingBox
aBox.getParagraphs().add(table);
// Save the PDF document
doc.save(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FloatingBox.pdf");
This technical tip shows how developers can create a simple FloatingBox object and then add a TextFragment to its paragraphs collection inside their Android application. As a sub-class of Paragraph, a FloatingBox is a box that contains one or more paragraphs. It can be positioned by a developer but not by the page render engine. There are methods which can be used to control the position of a floating box. The positioning of FloatingBox is absolute rather than relative. A code snippet depicts the behavior of adding table inside FloatingBox is also provided this post. The First code snippet shows how to create a simple FloatingBox object and then add a TextFragment to its paragraphs collection. As table is a paragraph level object, so it can also be added to the FloatinBox class' paragraphs collection. The 2nd part of the code snippet depicts the behavior of adding table inside FloatingBox.