We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to insert a file attachment inside Table Cell

I use Aspose.PDF Java

I want to insert a file attachment.
but I don’t know how to attch the file into table cell.

ㅡㅡㅡㅡㅡㅡㅡㅡ
cell1 | fileAttach |
cell2 | fileAttach |
ㅡㅡㅡㅡㅡㅡㅡㅡ

I can’t find sample code.
Can I see the sample code about this topic?

my code

Document doc = new Document(pdfFullPath);
Page page = doc.getPages().add();

row.getCells().add(“Column (” + i + “, 1)”).setAlignment(VerticalAlignment.Center);
row.getCells().add(“Column (” + i + “, 2)”).setAlignment(VerticalAlignment.Center);

Cell fileCell = row.getCells().add();
FileInputStream fis = new FileInputStream(“C:\signTest\watermark.gif”);
FileSpecification fileSpec = new FileSpecification(fis, “attachment.gif”);
FileAttachmentAnnotation annotation = new FileAttachmentAnnotation(page, new com.aspose.pdf.Rectangle(100, 100, 50, 50), fileSpec);
fileCell.getParagraphs().add(annotation);

page.getParagraphs().add(table);

But I can’t see the file annotation and attach file.

@kevinKang

Thank you for contacting support.

Please note that the annotations are page level objects thus can not be added to paragraphs but pages instead. You would need to specify the rectangular coordinates where you want to place an annotation. Below code snippet explains adding a table and placing FileAttachmentAnnotation on suitable position.

Document doc = new Document();
Page page = doc.getPages().add();
// Initializes a new instance of the Table
Table table = new Table();
// Set the table border color as LightGray
table.setBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
// set the border for table cells
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
// add row to table
Row row = table.getRows().add();
Cell textCell = row.getCells().add("Sample Text");
Cell fileCell = row.getCells().add("File Here");
textCell.setAlignment(HorizontalAlignment.Center);
fileCell.setAlignment(HorizontalAlignment.Center);

// Set up a new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(dataDir + "sample.txt", "Sample text file");
FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(page, new com.aspose.pdf.Rectangle(210, 760, 220, 768), fileSpecification);
fileAttachment.setIcon(FileIcon.Paperclip);
page.getAnnotations().add(fileAttachment);
page.getParagraphs().add(table);
doc.save(dataDir + "AttachedFiles.pdf");

Moreover, the basic measuring unit in Aspose.PDF for .NET is point, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points. We have attached generated PDF file for your kind reference. AttachedFiles.pdf