Write on previous page

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

25th November 2015

Switch code for this example

 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/33873180/itext-pdf-add-text-in-absolute-position-on-top-of-the-1st-page
package sandbox.columntext;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

 * @author iText
public class WriteOnFirstPage {
    public static final String DEST = "results/columntext/write_on_first_page.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        new WriteOnFirstPage().createPdf(DEST);
    public void createPdf(String dest) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate message = cb.createTemplate(523, 50);
        Image header = Image.getInstance(message);
        for (int i = 0; i < 100; i++) {
            document.add(new Paragraph("test"));
        ColumnText ct = new ColumnText(message);
        ct.setSimpleColumn(new Rectangle(0, 0, 523, 50));
                new Paragraph(
                        String.format("There are %s pages in this document", writer.getPageNumber())));
        // step 5

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