0
votes

I am using PDFBox 1.2.1 in Java and I am trying to use single page pdf document which has an acro form in it as a template for making multiple page target pdf.

PDDocument sourceDocument = PDDocument.load(fileStream);
PDDocument targetDocument = new PDDocument();
PDDocumentCatalog sourceDocCatalog = sourceDocument.getDocumentCatalog();
PDAcroForm acroFormFromSource = sourceDocCatalog.getAcroForm();
targetDocument.getDocumentCatalog().setAcroForm(acroFormFromSource);
PDPage templatePdfPage = (PDPage) sourceDocument.getDocumentCatalog().getAllPages().get(0);

  for (int i = 0; i < 5; i++) {

    targetDocument.addPage(templatePdfPage);
    PDDocumentCatalog targetDocumentsDocumentCatalog = targetDocument.getDocumentCatalog();
    PDAcroForm acroForm = targetDocumentsDocumentCatalog.getAcroForm();
    acroForm.getField("Text1").setValue("Car " + i);
  }

Unfortunately the generated target pdf contains 5 pages but every page has Text1 field with same value "Car 4". So every page is the same acro form. Is it somehow possible to generate new unique acro form for every page or is there other possible solution for my use case?

1

1 Answers

0
votes

I think the problem is that you're using the same Java object acroFormFromSource for all the pages, so when you set the "Text1" field on page 4 (the last page of pages 0 through 4) it's setting it for all 5 pages.

I think you need to make a new copy the original PDAcroForm for each page. I suspect the easiest way to make a copy is to use the copy constructor of CosDictionary (COSDictionary( COSDictionary dict )). Be aware that this makes a shallow copy, though!