Repeating parts of a form

This is a code example of iText PDF, discover more.

6th November 2015
admin-marketing

Switch code for this example

AddExtraPage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/26853894/continue-field-output-on-second-page-with-itextsharp
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.AcroFields;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfImportedPage;
  17. import com.itextpdf.text.pdf.PdfReader;
  18. import com.itextpdf.text.pdf.PdfStamper;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class AddExtraPage {
  27.  
  28.     public static final String SRC = "resources/pdfs/stationery.pdf";
  29.     public static final String DEST = "results/acroforms/more_than_one_page.pdf";
  30.    
  31.     public static void main(String[] args) throws DocumentException, IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new AddExtraPage().manipulatePdf(SRC, DEST);
  35.     }
  36.  
  37.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  38.         PdfReader reader = new PdfReader(src);
  39.         Rectangle pagesize = reader.getPageSize(1);
  40.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  41.         Paragraph p = new Paragraph();
  42.         p.add(new Chunk("Hello "));
  43.         p.add(new Chunk("World", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));
  44.         AcroFields form = stamper.getAcroFields();
  45.         Rectangle rect = form.getFieldPositions("body").get(0).position;
  46.         int status;
  47.         PdfImportedPage newPage = null;
  48.         ColumnText column = new ColumnText(stamper.getOverContent(1));
  49.         column.setSimpleColumn(rect);
  50.         int pagecount = 1;
  51.         for (int i = 0; i < 100; ) {
  52.             i++;
  53.             column.addElement(new Paragraph("Hello " + i));
  54.             column.addElement(p);
  55.             status = column.go();
  56.             if (ColumnText.hasMoreText(status)) {
  57.                 newPage = loadPage(newPage, reader, stamper);
  58.                 triggerNewPage(stamper, pagesize, newPage, column, rect, ++pagecount);
  59.             }
  60.         }
  61.         stamper.setFormFlattening(true);
  62.         stamper.close();
  63.         reader.close();
  64.     }
  65.    
  66.     public PdfImportedPage loadPage(PdfImportedPage page, PdfReader reader, PdfStamper stamper) {
  67.         if (page == null) {
  68.             return stamper.getImportedPage(reader, 1);
  69.         }
  70.         return page;
  71.     }
  72.    
  73.     public void triggerNewPage(PdfStamper stamper, Rectangle pagesize, PdfImportedPage page, ColumnText column, Rectangle rect, int pagecount) throws DocumentException {
  74.         stamper.insertPage(pagecount, pagesize);
  75.         PdfContentByte canvas = stamper.getOverContent(pagecount);
  76.         canvas.addTemplate(page, 0, 0);
  77.         column.setCanvas(canvas);
  78.         column.setSimpleColumn(rect);
  79.         column.go();
  80.     }
  81. }
AddExtraTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28590487/adding-table-to-existing-pdf-on-the-same-page-itext
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.ColumnText;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class AddExtraTable {
  23.  
  24.     public static final String SRC = "resources/pdfs/form.pdf";
  25.     public static final String DEST = "results/acroforms/add_extra_table.pdf";
  26.    
  27.     public static void main(String[] args) throws DocumentException, IOException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new AddExtraTable().manipulatePdf(SRC, DEST);
  31.     }
  32.  
  33.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  34.         PdfReader reader = new PdfReader(src);
  35.         Rectangle pagesize = reader.getPageSize(1);
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         AcroFields form = stamper.getAcroFields();
  38.         form.setField("Name", "Jennifer");
  39.         form.setField("Company", "iText's next customer");
  40.         form.setField("Country", "No Man's Land");
  41.         PdfPTable table = new PdfPTable(2);
  42.         table.addCell("#");
  43.         table.addCell("description");
  44.         table.setHeaderRows(1);
  45.         table.setWidths(new int[]{ 1, 15 });
  46.         for (int i = 1; i
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now