PDF/A-3

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

31st May 2016
iText PDF

Switch code for this example

PdfA3.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. package com.itextpdf.samples.sandbox.pdfa;
  9.  
  10.  
  11. import com.itextpdf.io.font.PdfEncodings;
  12. import com.itextpdf.kernel.font.PdfFont;
  13. import com.itextpdf.kernel.font.PdfFontFactory;
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
  17. import com.itextpdf.kernel.xmp.XMPException;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.pdfa.PdfADocument;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.*;
  28. import java.util.StringTokenizer;
  29.  
  30. @Category(SampleTest.class)
  31. public class PdfA3 extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/pdfa/pdf_a3.pdf";
  33.     public static final String BOLD = "./src/test/resources/font/OpenSans-Bold.ttf";
  34.     public static final String DATA = "./src/test/resources/data/united_states.csv";
  35.     public static final String FONT = "./src/test/resources/font/OpenSans-Regular.ttf";
  36.  
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new PdfA3().manipulatePdf(DEST);
  42.     }
  43.  
  44.     public void manipulatePdf(String dest) throws IOException, XMPException {
  45.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  46.         PdfFont bold = PdfFontFactory.createFont(BOLD, PdfEncodings.IDENTITY_H);
  47.         InputStream is = new FileInputStream("./src/test/resources/data/sRGB_CS_profile.icm");
  48.         PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_3B,
  49.                 new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is));
  50.  
  51.         Document document = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  52.  
  53.         PdfDictionary parameters = new PdfDictionary();
  54.         parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());
  55.  
  56.         PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc, DATA.getBytes(), "united_states.csv",
  57.                 "united_states.csv", new PdfName("text/csv"), parameters, PdfName.Data, false);
  58.         fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
  59.  
  60.         pdfDoc.addFileAttachment("united_states.csv", fileSpec);
  61.         PdfArray array = new PdfArray();
  62.         array.add(fileSpec.getPdfObject().getIndirectReference());
  63.         pdfDoc.getCatalog().put(new PdfName("AF"), array);
  64.         Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
  65.         table.setWidthPercent(100);
  66.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  67.         String line = br.readLine();
  68.         process(table, line, bold, 10, true);
  69.         while ((line = br.readLine()) != null) {
  70.             process(table, line, font, 10, false);
  71.         }
  72.         br.close();
  73.         document.add(table);
  74.         document.close();
  75.     }
  76.  
  77.     public void process(Table table, String line, PdfFont font, int fontSize, boolean isHeader) {
  78.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  79.         while (tokenizer.hasMoreTokens()) {
  80.             if (isHeader) {
  81.                 table.addHeaderCell(new Paragraph(tokenizer.nextToken()).setFont(font).setFontSize(fontSize));
  82.             } else {
  83.                 table.addCell(new Paragraph(tokenizer.nextToken()).setFont(font).setFontSize(fontSize));
  84.             }
  85.         }
  86.     }
  87. }
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