PDF/A-3

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

7th November 2015
admin-marketing

Switch code for this example

PdfA3.java
  1. package sandbox.pdfa;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Font;
  6. import com.itextpdf.text.PageSize;
  7. import com.itextpdf.text.Phrase;
  8. import com.itextpdf.text.pdf.BaseFont;
  9. import com.itextpdf.text.pdf.ICC_Profile;
  10. import com.itextpdf.text.pdf.PdfAConformanceLevel;
  11. import com.itextpdf.text.pdf.PdfAWriter;
  12. import com.itextpdf.text.pdf.PdfArray;
  13. import com.itextpdf.text.pdf.PdfDate;
  14. import com.itextpdf.text.pdf.PdfDictionary;
  15. import com.itextpdf.text.pdf.PdfFileSpecification;
  16. import com.itextpdf.text.pdf.PdfName;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileOutputStream;
  23. import java.io.FileReader;
  24. import java.io.IOException;
  25. import java.util.StringTokenizer;
  26.  
  27. import sandbox.WrapToTest;
  28.  
  29. @WrapToTest
  30. public class PdfA3 {
  31.     public static final String DEST = "results/pdfa/pdf_a.pdf";
  32.     public static final String DATA = "resources/data/united_states.csv";
  33.     public static final String FONT = "resources/fonts/OpenSans-Regular.ttf";
  34.     public static final String BOLD = "resources/fonts/OpenSans-Bold.ttf";
  35.    
  36.     public static void main(String[] args) throws IOException, DocumentException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new PdfA3().createPdf(DEST);
  40.     }
  41.    
  42.     public void createPdf(String dest) throws IOException, DocumentException {
  43.         Font font = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10);
  44.         Font bold = new Font(BaseFont.createFont(BOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10);
  45.         Document document = new Document(PageSize.A4.rotate());
  46.         PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(dest), PdfAConformanceLevel.PDF_A_3B);
  47.         writer.createXmpMetadata();
  48.         document.open();
  49.         File file = new File("resources/data/sRGB_CS_profile.icm");
  50.         ICC_Profile icc = ICC_Profile
  51.                 .getInstance(new FileInputStream(file));
  52.         writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
  53.         PdfDictionary parameters = new PdfDictionary();
  54.         parameters.put(PdfName.MODDATE, new PdfDate());
  55.         PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
  56.                 writer, DATA,
  57.                 "united_states.csv", null, "text/csv", parameters, 0);
  58.         fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
  59.         writer.addFileAttachment("united_states.csv", fileSpec);
  60.         PdfArray array = new PdfArray();
  61.         array.add(fileSpec.getReference());
  62.         writer.getExtraCatalog().put(new PdfName("AF"), array);
  63.         PdfPTable table = new PdfPTable(9);
  64.         table.setWidthPercentage(100);
  65.         table.setWidths(new int[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
  66.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  67.         String line = br.readLine();
  68.         process(table, line, bold);
  69.         table.setHeaderRows(1);
  70.         while ((line = br.readLine()) != null) {
  71.             process(table, line, font);
  72.         }
  73.         br.close();
  74.         document.add(table);
  75.         document.close();
  76.     }
  77.    
  78.     public void process(PdfPTable table, String line, Font font) {
  79.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  80.         while (tokenizer.hasMoreTokens()) {
  81.             table.addCell(new Phrase(tokenizer.nextToken(), font));
  82.         }
  83.     }
  84. }
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