We need to import existing multiple PDFs into a single new PDF. Part of codes work similar to the sample codes in section 6.2.1 of iText in Action 2nd edition:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(
document, new FileOutputStream(RESULT));
document.open();
PdfPTable table = new PdfPTable(2);
PdfReader reader = new PdfReader(MovieTemplates.RESULT);
int n = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
page = writer.getImportedPage(reader, i);
table.addCell(Image.getInstance(page));
}
document.add(table);
document.close();
However, we just realized when dealing with fill-able PDFs with annotations (in our case, those PDF already have data filled), all the filled data are lost in new PDF.
We found the answer in the same section of the book:
It's important to understand the difference between resources needed to render the content of a page and the interactive features of a page. In general, these features are called annotations. They include links, text annotations, and form fields. Annotations aren't part of the content stream. They aren't listed in the resources dictionary of the page, but in the annotation dictionary. These interactive features aren't copied when using
PdfImportedPage
, which means that all interactivity is lost when copying a page with thegetImportedPage()
method of thePdfWriter
class.
But what is the solution to keep those annotations?