Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Took me a while to figure this out. It's not the most efficient way to code, but here's essentially what it does:</p> <ul> <li>create a document</li> <li>for each page(s) with an acrofield:</li> <li>copy your template</li> <li>fill the form</li> <li>flatten the form</li> <li>add the page</li> </ul> <p>Here's my implementation that you can try and modify to fit your needs:</p> <pre><code>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 &lt; 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(); } </code></pre> <p>The <code>copyPdf()</code> method creates temporary files that are used to allow form filling without affecting the entire document. If you find a more efficient way to do this, let me know.</p> <p>Also, I've found that on Intel Based Mac vs Windows Computer, the Mac completes this much faster. </p> <p>If you're not opposed to getting a reference book for iText, I would recommend "iText in Action, Second Edition" by Bruno Lowagie. It is a great book and very helpful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload