Page numbers and PDF/A

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

5th November 2015
admin-marketing

Switch code for this example

PdfA1A.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27500586/itext-page-number-in-header-within-pdf-a
  4.  */
  5. package sandbox.pdfa;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.ExceptionConverter;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.PageSize;
  14. import com.itextpdf.text.Phrase;
  15. import com.itextpdf.text.Rectangle;
  16. import com.itextpdf.text.pdf.BaseFont;
  17. import com.itextpdf.text.pdf.ColumnText;
  18. import com.itextpdf.text.pdf.ICC_Profile;
  19. import com.itextpdf.text.pdf.PdfAConformanceLevel;
  20. import com.itextpdf.text.pdf.PdfAWriter;
  21. import com.itextpdf.text.pdf.PdfContentByte;
  22. import com.itextpdf.text.pdf.PdfDate;
  23. import com.itextpdf.text.pdf.PdfDictionary;
  24. import com.itextpdf.text.pdf.PdfName;
  25. import com.itextpdf.text.pdf.PdfPCell;
  26. import com.itextpdf.text.pdf.PdfPTable;
  27. import com.itextpdf.text.pdf.PdfPageEventHelper;
  28. import com.itextpdf.text.pdf.PdfTemplate;
  29. import com.itextpdf.text.pdf.PdfWriter;
  30.  
  31. import java.io.BufferedReader;
  32. import java.io.File;
  33. import java.io.FileInputStream;
  34. import java.io.FileOutputStream;
  35. import java.io.FileReader;
  36. import java.io.IOException;
  37. import java.util.StringTokenizer;
  38.  
  39. public class PdfA1A {
  40.    
  41.     class Header extends PdfPageEventHelper {
  42.         Font font;
  43.         PdfTemplate t;
  44.         Image total;
  45.        
  46.         @Override
  47.         public void onOpenDocument(PdfWriter writer, Document document) {
  48.             t = writer.getDirectContent().createTemplate(30, 16);
  49.             try {
  50.                 total = Image.getInstance(t);
  51.                 total.setRole(PdfName.ARTIFACT);
  52.                 font =  new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10);
  53.             } catch (DocumentException de) {
  54.                 throw new ExceptionConverter(de);
  55.             } catch (IOException ioe) {
  56.                 throw new ExceptionConverter(ioe);
  57.             }
  58.         }
  59.        
  60.         @Override
  61.         public void onEndPage(PdfWriter writer, Document document) {
  62.             PdfPTable table = new PdfPTable(3);
  63.             try {
  64.                 table.setWidths(new int[]{24, 24, 2});
  65.                 table.setTotalWidth(770);
  66.                 table.getDefaultCell().setFixedHeight(20);
  67.                 table.getDefaultCell().setBorder(Rectangle.BOTTOM);
  68.                 table.addCell(new Phrase("Test", font));
  69.                 table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
  70.                 table.addCell(new Phrase(String.format("Page %d of", writer.getPageNumber()), font));
  71.                 PdfPCell cell = new PdfPCell(total);
  72.                 cell.setBorder(Rectangle.BOTTOM);
  73.                 table.addCell(cell);
  74.                 PdfContentByte canvas = writer.getDirectContent();
  75.                 canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
  76.                 table.writeSelectedRows(0, -1, 36, 30, canvas);
  77.                 canvas.endMarkedContentSequence();
  78.             } catch (DocumentException de) {
  79.                 throw new ExceptionConverter(de);
  80.             }
  81.         }
  82.        
  83.         @Override
  84.         public void onCloseDocument(PdfWriter writer, Document document) {
  85.             ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
  86.                 new Phrase(String.valueOf(writer.getPageNumber()), font),
  87.                 2, 4, 0);
  88.         }
  89.     }
  90.    
  91.     public static final String DEST = "results/pdfa/pdf_a1a.pdf";
  92.     public static final String DATA = "resources/data/united_states.csv";
  93.     public static final String FONT = "resources/fonts/OpenSans-Regular.ttf";
  94.     public static final String BOLD = "resources/fonts/OpenSans-Bold.ttf";
  95.    
  96.     public static void main(String[] args) throws IOException, DocumentException {
  97.         File file = new File(DEST);
  98.         file.getParentFile().mkdirs();
  99.         new PdfA1A().createPdf(DEST);
  100.     }
  101.    
  102.     public void createPdf(String dest) throws IOException, DocumentException {
  103.         Font font = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10);
  104.         Font bold = new Font(BaseFont.createFont(BOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10);
  105.         Document document = new Document(PageSize.A4.rotate());
  106.         PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(dest), PdfAConformanceLevel.PDF_A_1A);
  107.         writer.createXmpMetadata();
  108.         writer.setTagged();
  109.         writer.setPageEvent(new Header());
  110.         document.open();
  111.         document.addLanguage("en-us");
  112.         File file = new File("resources/data/sRGB_CS_profile.icm");
  113.         ICC_Profile icc = ICC_Profile
  114.                 .getInstance(new FileInputStream(file));
  115.         writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
  116.         PdfDictionary parameters = new PdfDictionary();
  117.         parameters.put(PdfName.MODDATE, new PdfDate());
  118.         PdfPTable table = new PdfPTable(9);
  119.         table.setWidthPercentage(100);
  120.         table.setWidths(new int[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
  121.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  122.         String line = br.readLine();
  123.         process(table, line, bold);
  124.         table.setHeaderRows(1);
  125.         while ((line = br.readLine()) != null) {
  126.             process(table, line, font);
  127.         }
  128.         br.close();
  129.         document.add(table);
  130.         document.close();
  131.     }
  132.    
  133.     public void process(PdfPTable table, String line, Font font) {
  134.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  135.         while (tokenizer.hasMoreTokens()) {
  136.             table.addCell(new Phrase(tokenizer.nextToken(), font));
  137.         }
  138.     }
  139. }
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