1
votes

I am creating a new PDF that will contain a compilation of other documents.

These other documents can be word/excel/images/PDF's.

I am hoping to add all of this content to cells in a table, which is added to the document - this gives me the goodness of automatically adding pages, positioning elements in a cell rather than a page and allowing me an easier life at keeping content in the same order as i supply (such as img, doc, pdf, img, pdf etc)

Adding images to the table is simple enough.

I am converting the word/excel docs to PDF image streams. I'm also reading in the existing PDF's as a stream.

Adding these to a new PDF is simple enough - by way of adding a template to the PdfContent byte.

What I am trying to do though is add these PDF's to cells in a table, which are then added to the doc.

Is this possible?

1
Use Image.GetInstance(PdfTemplate template). - Paulo Soares

1 Answers

2
votes

Please download chapter 6 of my book. It contains two variations on what you are trying to do:

This is a code snippet:

// step 1
Document document = new Document();
// step 2
PdfWriter writer
    = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
PdfReader reader = new PdfReader(MovieTemplates.RESULT);
int n = reader.getNumberOfPages();
PdfImportedPage page;
PdfPTable table = new PdfPTable(2);
for (int i = 1; i <= n; i++) {
    page = writer.getImportedPage(reader, i);
    table.getDefaultCell().setRotation(-page.getRotation());
    table.addCell(Image.getInstance(page));
}
document.add(table);
// step 5
document.close();
reader.close();

The pages are imported as PdfImportedPage objects, and then wrapped inside an Image so that we can add them to a PdfPTable.