Page numbers and PDF/A

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

31st May 2016
admin-marketing

Switch code for this example

PdfA1a.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.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27500586/itext-page-number-in-header-within-pdf-a
  11.  */
  12. package com.itextpdf.samples.sandbox.pdfa;
  13.  
  14. import com.itextpdf.io.font.PdfEncodings;
  15. import com.itextpdf.kernel.events.Event;
  16. import com.itextpdf.kernel.events.IEventHandler;
  17. import com.itextpdf.kernel.events.PdfDocumentEvent;
  18. import com.itextpdf.kernel.font.PdfFont;
  19. import com.itextpdf.kernel.font.PdfFontFactory;
  20. import com.itextpdf.kernel.geom.PageSize;
  21. import com.itextpdf.kernel.geom.Rectangle;
  22. import com.itextpdf.kernel.pdf.*;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  25. import com.itextpdf.kernel.xmp.XMPException;
  26. import com.itextpdf.layout.Document;
  27. import com.itextpdf.layout.element.Image;
  28. import com.itextpdf.layout.element.Paragraph;
  29. import com.itextpdf.layout.element.Table;
  30. import com.itextpdf.pdfa.PdfADocument;
  31. import com.itextpdf.samples.GenericTest;
  32. import com.itextpdf.test.annotations.type.SampleTest;
  33.  
  34. import org.junit.experimental.categories.Category;
  35.  
  36. import java.io.*;
  37. import java.util.StringTokenizer;
  38.  
  39. @Category(SampleTest.class)
  40. public class PdfA1a extends GenericTest {
  41.     public static final String DEST = "./target/test/resources/sandbox//pdfa/pdf_a_1a.pdf";
  42.     public static final String BOLD = "./src/test/resources/font/OpenSans-Bold.ttf";
  43.     public static final String DATA = "./src/test/resources/data/united_states.csv";
  44.     public static final String FONT = "./src/test/resources/font/OpenSans-Regular.ttf";
  45.  
  46.     protected PdfFormXObject template;
  47.     protected Image total;
  48.     protected PdfFont font;
  49.     protected PdfFont bold;
  50.  
  51.     public static void main(String[] args) throws Exception {
  52.         File file = new File(DEST);
  53.         file.getParentFile().mkdirs();
  54.         new PdfA1a().manipulatePdf(DEST);
  55.     }
  56.  
  57.     public void manipulatePdf(String dest) throws IOException, XMPException {
  58.         font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  59.         bold = PdfFontFactory.createFont(BOLD, PdfEncodings.IDENTITY_H);
  60.         FileInputStream is = new FileInputStream("./src/test/resources/data/sRGB_CS_profile.icm");
  61.         PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1A,
  62.                 new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is));
  63.  
  64.         Document document = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  65.         pdfDoc.setTagged();
  66.  
  67.         template = new PdfFormXObject(new Rectangle(795, 575, 30, 30));
  68.         PdfCanvas canvas = new PdfCanvas(template, pdfDoc);
  69.         total = new Image(template);
  70.         total.setRole(PdfName.Artifact);
  71.  
  72.         pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new HeaderHandler());
  73.         pdfDoc.getCatalog().setLang(new PdfString("en-us"));
  74.         PdfDictionary parameters = new PdfDictionary();
  75.         parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());
  76.  
  77.         Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
  78.         table.setWidthPercent(100);
  79.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  80.         String line = br.readLine();
  81.         process(table, line, bold, 10, true);
  82.         while ((line = br.readLine()) != null) {
  83.             process(table, line, font, 10, false);
  84.         }
  85.         br.close();
  86.         document.add(table);
  87.  
  88.         canvas.beginText();
  89.         canvas.setFontAndSize(bold, 12);
  90.         canvas.moveText(795, 575);
  91.         canvas.showText(Integer.toString(pdfDoc.getNumberOfPages()));
  92.         canvas.endText();
  93.         canvas.stroke();
  94.  
  95.         document.close();
  96.     }
  97.  
  98.     public void process(Table table, String line, PdfFont font, int fontSize, boolean isHeader) {
  99.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  100.         while (tokenizer.hasMoreTokens()) {
  101.             if (isHeader) {
  102.                 table.addHeaderCell(new Paragraph(tokenizer.nextToken()).setFont(font).setFontSize(fontSize));
  103.             } else {
  104.                 table.addCell(new Paragraph(tokenizer.nextToken()).setFont(font).setFontSize(fontSize));
  105.             }
  106.         }
  107.     }
  108.  
  109.  
  110.     public class HeaderHandler implements IEventHandler {
  111.         @Override
  112.         public void handleEvent(Event event) {
  113.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  114.             PdfPage page = docEvent.getPage();
  115.             int pageNum = docEvent.getDocument().getPageNumber(page);
  116.             PdfCanvas canvas = new PdfCanvas(page);
  117.             canvas.beginText();
  118.             canvas.setFontAndSize(font, 12);
  119.             canvas.beginMarkedContent(PdfName.Artifact);
  120.             canvas.moveText(34, 575);
  121.             canvas.showText("Test");
  122.             canvas.moveText(703, 0);
  123.             canvas.showText(String.format("Page %d of", pageNum));
  124.             canvas.endText();
  125.             canvas.stroke();
  126.             canvas.addXObject(template, 0, 0);
  127.             canvas.endMarkedContent();
  128.             canvas.release();
  129.         }
  130.     }
  131. }
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