PDF/A-1

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

7th November 2015
admin-marketing

Switch code for this example

PdfA1a_images.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27294392/itext-create-pdf-a-1a-with-images
  4.  * It will only work starting with iText 5.5.4
  5.  */
  6. package sandbox.pdfa;
  7.  
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.ICC_Profile;
  16. import com.itextpdf.text.pdf.PdfAConformanceLevel;
  17. import com.itextpdf.text.pdf.PdfAWriter;
  18. import com.itextpdf.text.pdf.PdfName;
  19. import com.itextpdf.text.pdf.PdfString;
  20.  
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import sandbox.WrapToTest;
  26.  
  27. @WrapToTest
  28. public class PdfA1a_images {
  29.     final float MARGIN_OF_ONE_CM = 28.8f;
  30.     public static final String DEST = "results/pdfa/pdf_a1a_images.pdf";
  31.     public static final String FONT = "resources/fonts/OpenSans-Regular.ttf";
  32.     public static final String LOGO = "resources/images/hero.jpg";
  33.    
  34.     public static void main(String[] args) throws IOException, DocumentException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new PdfA1a_images().createPdf(DEST);
  38.     }
  39.    
  40.     public void createPdf(String dest) throws IOException, DocumentException {
  41.         Font font = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 10);
  42.         Document document = new com.itextpdf.text.Document(PageSize.A4,
  43.                 MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM);
  44.         PdfAWriter writer = PdfAWriter.getInstance(document,
  45.                 new FileOutputStream(DEST), PdfAConformanceLevel.PDF_A_1A);
  46.         document.addAuthor("Author");
  47.         document.addSubject("Subject");
  48.         document.addLanguage("nl-nl");
  49.         document.addCreationDate();
  50.         document.addCreator("Creator");
  51.         document.addTitle("title");
  52.         writer.setTagged();
  53.         writer.createXmpMetadata();
  54.         document.open();
  55.         File file = new File("resources/data/sRGB_CS_profile.icm");
  56.         ICC_Profile icc = ICC_Profile
  57.                 .getInstance(new FileInputStream(file));
  58.         writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
  59.         Paragraph element = new Paragraph("Hello World", font);
  60.         document.add(element);
  61.         Image logoImage = Image.getInstance(LOGO);
  62.         logoImage.setAccessibleAttribute(PdfName.ALT, new PdfString("Logo"));
  63.         document.add(logoImage);
  64.         document.close();
  65.     }
  66. }
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