Write on previous page

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

31st May 2016
iText PDF

Switch code for this example

WriteOnFirstPage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/33873180/itext-pdf-add-text-in-absolute-position-on-top-of-the-1st-page
  11.  */
  12. package com.itextpdf.samples.sandbox.columntext;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Image;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import java.io.File;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. @Category(SampleTest.class)
  32. public class WriteOnFirstPage extends GenericTest {
  33.     public static final String DEST
  34.             = "./target/test/resources/sandbox/columntext/write_on_first_page.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new WriteOnFirstPage().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  45.         Document doc = new Document(pdfDoc);
  46.         PdfFormXObject template = new PdfFormXObject(new Rectangle(523, 50));
  47.         PdfCanvas templateCanvas = new PdfCanvas(template, pdfDoc);
  48.  
  49.         doc.add(new Image(template));
  50.         for (int i = 0; i < 100; i++) {
  51.             doc.add(new Paragraph("test"));
  52.         }
  53.  
  54.         templateCanvas
  55.                 .beginText()
  56.                 .setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 12)
  57.                 .showText(String.format("There are %s pages in this document", pdfDoc.getNumberOfPages()))
  58.                 .endText()
  59.                 .stroke();
  60.  
  61.         doc.close();
  62.     }
  63. }
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