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.
Zach
source share