Itext - How to clone pages with acrofields?

I am writing a Java program that prints Bingo Cards PDF files. Each page is one map. To simplify for me, I created a PDF template file with acrofields, so the program only needs to create a copy of this template, fill in the acrofields with numbers, and then smooth it. At the moment I can create 1 bingo card. I want to have multiple pages (thus multiple maps) in one PDF file. But I have no idea how to do this. I read that PDFStamper is associated with one and only one PDFReader object. Is there a way that I can do this without resorting to creating multiple PDF files and combining them into one (I did this the last time, and I found it very slow) Thanks in advance!

+7
source share
2 answers

It's time to figure it out. This is not the most efficient coding method, but here, essentially, what it does is:

  • create document
  • for each page using acrofield:
  • copy your template
  • fill the form
  • smooth the shape
  • add page

Here is my implementation that you can try and modify to suit your needs:

private void createPdf() throws Exception { Document doc = new Document(); PdfSmartCopy copy = new PdfSmartCopy(doc, new FileOutputStream("result.pdf")); doc.open(); PdfReader reader; PdfStamper stamper; AcroFields form; ByteArrayOutputStream baos; for(int i = 0; i < getTotalPages(); i++) { copyPdf(i); reader = new PdfReader(String.format("%d%s", i, "template.pdf")); baos = new ByteArrayOutputStream(); stamper = new PdfStamper(reader, baos); form = stamper.getAcroFields(); //methods to fill forms stamper.setFormFlattening(true); stamper.close(); reader = new PdfReader(baos.toByteArray()); copy.addPage(copy.getImportedPage(reader, 1)); } doc.close(); } private void copyPdf(int currentPage) throws Exception { PdfReader reader = new PdfReader("timesheet.pdf"); Document doc = new Document(); File file = new File(String.format("%d%s", currentPage, "template.pdf")); file.deleteOnExit(); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(file)); stamper.close(); } 

The copyPdf () method creates temporary files that are used to fill out the form without affecting the entire document. If you find a better way to do this, let me know.

In addition, I found that on an Intel Mac based on a Windows Computer, Mac Mac speeds it up much faster.

If you do not mind getting the iText reference, I would recommend Bruno Lowagie "iText in Action, Second Edition". This is a great book and very useful.

+13
source

So, here is the code without using the Zach "copyPdf" method, like Mark Storer and MaxArt, suggested:

 private void createPdf() throws Exception { Document doc = new Document(); PdfSmartCopy copy = new PdfSmartCopy(doc, new FileOutputStream("result.pdf")); doc.open(); PdfReader mainReader = new PdfReader("timesheet.pdf"); PdfReader reader; PdfStamper stamper; AcroFields form; ByteArrayOutputStream baos; for(int i = 0; i < getTotalPages(); i++) { reader = new PdfReader(mainReader); baos = new ByteArrayOutputStream(); stamper = new PdfStamper(reader, baos); AcroFields form = stamper.getAcroFields(); //methods to fill forms stamper.setFormFlattening(true); stamper.close(); reader = new PdfReader(baos.toByteArray()); copy.addPage(copy.getImportedPage(reader, 1)); } doc.close(); } 
+1
source

All Articles