Created
June 2, 2025 16:13
-
-
Save documentprocessing/3dd2a1b92aa031b7f7ef517fdd92c72b to your computer and use it in GitHub Desktop.
Generate PDF/A Compliant Document in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.lowagie.text.Document; | |
import com.lowagie.text.Font; | |
import com.lowagie.text.FontFactory; | |
import com.lowagie.text.Paragraph; | |
import com.lowagie.text.pdf.PdfAConformanceLevel; | |
import com.lowagie.text.pdf.PdfAWriter; | |
import com.lowagie.text.pdf.PdfReader; | |
import com.lowagie.text.pdf.ICC_Profile; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
public class PdfAExample { | |
public static void main(String[] args) { | |
// Step 1: Create Document with PDF/A settings | |
Document document = new Document(); | |
try (InputStream iccProfileStream = PdfAExample.class.getResourceAsStream("/sRGB_ICC_profile.icc")) { | |
// Step 2: Load ICC profile for color management (required for PDF/A) | |
ICC_Profile icc = ICC_Profile.getInstance(iccProfileStream); | |
// Step 3: Initialize PDF/A writer with conformance level | |
PdfAWriter writer = PdfAWriter.getInstance( | |
document, | |
new FileOutputStream("pdfa-document.pdf"), | |
PdfAConformanceLevel.PDF_A_1B | |
); | |
// Step 4: Set PDF/A metadata and output intent | |
writer.setPdfVersion(PdfAWriter.PDF_VERSION_1_4); | |
writer.createXmpMetadata(); | |
writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc); | |
document.open(); | |
// Step 5: Add content (must use embedded fonts) | |
Font font = FontFactory.getFont( | |
"fonts/FreeSans.ttf", // Path to embedded font (required for PDF/A) | |
BaseFont.IDENTITY_H, | |
BaseFont.EMBEDDED, | |
12 | |
); | |
document.add(new Paragraph("PDF/A-1B Compliant Document", font)); | |
document.add(new Paragraph("This document meets archival standards.", font)); | |
// Step 6: Close resources | |
document.close(); | |
iccProfileStream.close(); | |
System.out.println("PDF/A document created successfully!"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment