PDF/A-1

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

31st May 2016
iText PDF

Switch code for this example

PdfA1a_images.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.  * This example was written in answer to the following question:
  10.  * http://stackoverflow.com/questions/27294392/itext-create-pdf-a-1a-with-images
  11.  */
  12. package com.itextpdf.samples.sandbox.pdfa;
  13.  
  14.  
  15. import com.itextpdf.io.font.PdfEncodings;
  16. import com.itextpdf.io.image.ImageDataFactory;
  17. import com.itextpdf.kernel.font.PdfFont;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.pdf.*;
  20. import com.itextpdf.kernel.xmp.XMPException;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Image;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.pdfa.PdfADocument;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31. import java.io.FileInputStream;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34.  
  35. @Category(SampleTest.class)
  36. public class PdfA1a_images extends GenericTest {
  37.     public static final float MARGIN_OF_ONE_CM = 28.8f;
  38.     public static final String DEST = "./target/test/resources/sandbox/pdfa/pdf_a1a_images.pdf";
  39.     public static final String FONT = "./src/test/resources/font/OpenSans-Regular.ttf";
  40.     public static final String LOGO = "./src/test/resources/img/hero.jpg";
  41.  
  42.     public static void main(String[] args) throws Exception {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new PdfA1a_images().manipulatePdf(DEST);
  46.     }
  47.  
  48.     public void manipulatePdf(String dest) throws IOException, XMPException {
  49.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  50.         InputStream is = new FileInputStream("./src/test/resources/data/sRGB_CS_profile.icm");
  51.         PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1A,
  52.                 new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is));
  53.         Document doc = new Document(pdfDoc);
  54.         doc.setMargins(MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM);
  55.  
  56.         pdfDoc.getCatalog().setLang(new PdfString("nl-nl"));
  57.         PdfDocumentInfo info = pdfDoc.getDocumentInfo();
  58.         info
  59.                 .setTitle("title")
  60.                 .setAuthor("Author")
  61.                 .setSubject("Subject")
  62.                 .setCreator("Creator")
  63.                 .setKeywords("Metadata, iText, PDF")
  64.                 .setCreator("My program using iText")
  65.                 .addCreationDate();
  66.         pdfDoc.setTagged();
  67.  
  68.         Paragraph element = new Paragraph("Hello World").setFont(font).setFontSize(10);
  69.         doc.add(element);
  70.         Image logoImage = new Image(ImageDataFactory.create(LOGO));
  71.         logoImage.getAccessibilityProperties().setAlternateDescription("Logo");
  72.         doc.add(logoImage);
  73.         doc.close();
  74.     }
  75. }
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