Hi, Its a function that create a TOC pdf.
ArrayList folders = new ArrayList();
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
Page page = doc.getPages().add();
// Instantiate a table object
com.aspose.pdf.Table tab = new com.aspose.pdf.Table();
tab.setLeft(20);
// Add the table in paragraphs collection of the desired section
page.getParagraphs().add(tab);
// Set with column widths of the table
tab.setColumnAdjustment(ColumnAdjustment.Customized );
tab.setColumnWidths("40% 20% 40%");
// Set default cell border using BorderInfo object
tab.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.05F));
// Set table border using another customized BorderInfo object
tab.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.05F));
// Create MarginInfo object and set its left, bottom, right and top margins
com.aspose.pdf.MarginInfo margin = new com.aspose.pdf.MarginInfo();
margin.setTop(3f);
margin.setLeft(3f);
margin.setRight(3f);
margin.setBottom(3f);
// Set the default cell padding to the MarginInfo object
tab.setDefaultCellPadding(margin);
tab.getMargin().setTop(30);
//Create Bold table header row
com.aspose.pdf.Row row1 = tab.getRows().add();
TextFragment boldtext= new TextFragment("File Name");
boldtext.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
com.aspose.pdf.Cell cell2 = row1.getCells().add();
cell2.getParagraphs().add(boldtext);
boldtext= new TextFragment("File Id");
boldtext.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
cell2 = row1.getCells().add();
cell2.getParagraphs().add(boldtext);
boldtext= new TextFragment("File Description");
boldtext.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
cell2 = row1.getCells().add();
cell2.getParagraphs().add(boldtext);
//To repeat header in the beginning of each page
tab.setRepeatingRowsCount(1);
for(BinderElement ele :elements) {
com.aspose.pdf.Row row2 = tab.getRows().add();
String fileName = ele.getFilePath().split("/")[ele.getFilePath().split("/").length - 1];
handleBinderElementFolderRow(ele.getFilePath(), fileName, row2, true);
handleBinderElementFileRow(ele, ele.getFilePath(), row2, false);
}
private void handleBinderElementFolderRow(String filePath, String fileName, com.aspose.pdf.Row row2, Boolean isRoot) {
if(!isRoot) {
String decodeURL1 = decodeValue(filePath.substring(0, filePath.length() - fileName.length() - 1));
decodeURL1 = decodeURL1.replace("/", "\\");
TextFragment textFragment = new TextFragment(decodeURL1);
textFragment.setHyperlink(new FileHyperlink(decodeURL1));
com.aspose.pdf.Cell cell2 = row2.getCells().add();
cell2.setBackgroundColor(com.aspose.pdf.Color.getLightGray());
cell2.getParagraphs().add(textFragment);
//Folder Row all cells are merged
createRowWithNoBordersWithSpan(row2);
} else {
TextFragment textFragment = new TextFragment("Root Folder");
com.aspose.pdf.Cell cell2 = row2.getCells().add();
cell2.getParagraphs().add(textFragment);
cell2.setBackgroundColor(com.aspose.pdf.Color.getLightGray());
//Folder Row all cells are merged
createRowWithNoBordersWithSpan(row2);
}
}
private void handleBinderElementFileRow(BinderElement ele, String filePath, com.aspose.pdf.Row row2, boolean isRelatedObject) {
com.aspose.pdf.Cell cell2 = row2.getCells().add();
String decodeURL1 = decodeValue(filePath);
TextFragment textFragment = new TextFragment(decodeValue(filePath.split("/")[filePath.split("/").length - 1]));
** textFragment.setHyperlink(new FileHyperlink(decodeURL1));**
cell2.getParagraphs().add(textFragment);
//Check if comment null, and add to cell
if(!isRelatedObject)
addBinderElementCellData(ele.getDocumentNumber(), row2);
else
row2.getCells().add();
addBinderElementCellData(ele.getComment(), row2);
}