Skip to main content

Convert and Validate PDF/A and PDF/UA Documents — PDF Document API for Java

  • 7 minutes to read

The PDF Document API allows you to convert PDF documents to PDF/A and PDF/UA, validate documents against these standards, and analyze compliance issues.

PDF/A (ISO 19005)
A PDF standard for long-term archiving. A PDF/A document embeds all resources required for rendering (for example, fonts and color profiles) and does not contain content that depends on external resources or scripts.
PDF/UA (ISO 14289)
A PDF standard for universal accessibility. A PDF/UA document contains a logical structure tree, tagged page content, and settings required by assistive technologies.

Supported Conformance Levels

Conformance Level Conversion Validation
PDF/A-1b, PDF/A-2b, PDF/A-3b Yes Yes
PDF/A-1a, PDF/A-2a, PDF/A-3a No Yes
PDF/UA-1 Yes Yes
PDF/UA-2 No Yes

Note

If you pass an unsupported conformance level to a conversion method, the API does not modify the document and returns a result with the ConversionStatus.ERRORS status.

Convert a Document to PDF/A

Call the PdfDocument.convertToPdfA(compatibility) method to convert a document to PDF/A. The conversion embeds fonts, adds an output intent, and removes content that the standard does not allow (for example, encryption, JavaScript actions, and XFA forms). The API also writes the conformance level to XMP metadata.

The following code snippet converts a document to PDF/A-3b, prints conversion issues, and saves the document if the conversion completes without errors:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Files.createDirectories(Path.of("output"));
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            // Convert the document to PDF/A-3b.
            ConversionResult result = pdfDocument.convertToPdfA(PdfACompatibility.PDF_A3B);

            // Print conversion issues.
            System.out.println("Conversion status: " + result.getStatus());
            for (ComplianceIssue issue : result.getIssues())
                System.out.println(issue.getSeverity() + ": " + issue.getMessage());

            // Save the document if the conversion completed without errors.
            if (result.getStatus() != ConversionStatus.ERRORS) {
                try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                    pdfDocument.save(outputStream);
                }
            }
        }
    }
}

Note

If the document contains digital signatures, the conversion invalidates them. The API saves the converted document as a whole.

Embedded Files

The conformance level determines how the API processes embedded files (attachments):

Conformance Level Embedded Files
PDF/A-1b The API removes embedded files.
PDF/A-2b The API keeps embedded files. The API cannot verify that the embedded files conform to PDF/A and reports the ComplianceRule.EMBEDDED_FILES_NOT_VALIDATED error.
PDF/A-3b The API keeps embedded files of any type (for example, the invoice XML file of a ZUGFeRD invoice).

Convert a Document to PDF/UA

Call the PdfDocument.convertToPdfUA(compatibility) method to convert a document to PDF/UA-1. The conversion specifies document settings required by PDF/UA (for example, the DisplayDocTitle viewer preference and the tagged document flag), adds descriptions to links and form fields, and embeds missing fonts.

Important

The convertToPdfUA method does not create a structure tree and does not tag page content. A SUCCESS conversion status does not guarantee that the document meets PDF/UA requirements. Tag the document and specify its title before conversion. After conversion, call the PdfDocument.validatePdfUA(compatibility) method to check PDF/UA compliance.

The following code snippet loads a tagged document, specifies the document title, converts the document to PDF/UA-1, and saves the document if it passes PDF/UA-1 validation:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Files.createDirectories(Path.of("output"));
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            // Specify the document title that PDF/UA requires.
            pdfDocument.getMetadata().getDocumentInfo().setTitle("Annual Report");

            // Convert the tagged document to PDF/UA-1.
            pdfDocument.convertToPdfUA(PdfUACompatibility.PDF_UA_1);

            // Check whether the converted document meets PDF/UA-1 requirements.
            ValidationResult validation = pdfDocument.validatePdfUA(PdfUACompatibility.PDF_UA_1);
            for (ComplianceIssue issue : validation.getIssues())
                System.out.println(issue.getSeverity() + ": " + issue.getMessage());

            if (validation.getIsValid()) {
                try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                    pdfDocument.save(outputStream);
                }
            }
        }
    }
}

A document can conform to both PDF/A and PDF/UA standards. To create a document that conforms to both standards, do the following:

  1. Call the convertToPdfUA method.
  2. Call the convertToPdfA method on the same document.

Analyze Conversion Results

Conversion methods return a ConversionResult object. Call the ConversionResult.getStatus() method to get the conversion status:

Status Description
SUCCESS The conversion completed. The conversion result does not contain errors or warnings.
WARNINGS The conversion completed with warnings (for example, the API removed annotations or encryption).
ERRORS The API did not convert the document, or the converted document may not conform to the standard.

Call the ConversionResult.getIssues() method to get changes that the API made and issues that the API cannot fix. Each issue is a ComplianceIssue object.

Use the following methods to get issue details:

Method Description
getRule() Returns the compliance rule that the issue concerns.
getSeverity() Returns the issue severity.
getMessage() Returns the issue description.
getPageIndex() Returns the index of the page that contains the issue.

The ComplianceRule enumeration lists rules that conversion and validation methods report.

Validate a Document

Call the PdfDocument.validatePdfA(compatibility) or PdfDocument.validatePdfUA(compatibility) method to check whether a document conforms to the specified conformance level. Both methods return a ValidationResult object. Use the following methods to get the validation result:

Method Description
getIsValid() Returns whether the document conforms to the standard.
getIssues() Returns compliance issues that the validation found.

Note

If the document contains errors, it fails validation. Warnings and information messages do not affect the validation result.

The API validates the document as saved, including pending changes and signatures. Validation also checks the conformance level declared in XMP metadata. If the declared level differs from the target conformance level, the validation result contains the ComplianceRule.CONFORMANCE_CLAIM_MISMATCH error.

The following code snippet validates a document against PDF/A-3b and prints compliance issues:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            ValidationResult result = pdfDocument.validatePdfA(PdfACompatibility.PDF_A3B);
            System.out.println("PDF/A-3b compliant: " + result.getIsValid());

            for (ComplianceIssue issue : result.getIssues()) {
                String location = issue.getPageIndex() >= 0
                        ? "page " + (issue.getPageIndex() + 1)
                        : "document";
                System.out.println(issue.getSeverity() + " (" + location + "), "
                        + issue.getRule() + ": " + issue.getMessage());
            }
        }
    }
}

Validation Options

If the API saves the document as an incremental update (for example, a loaded document that contains signatures), validation checks the resulting document.

To process the entire document before validation, do the following:

  1. Create a ValidationOptions object.
  2. Call the ValidationOptions.setForcePreProcessing(true) method.
  3. Pass the object to the PdfDocument.validatePdfA(compatibility, options) or PdfDocument.validatePdfUA(compatibility, options) method.

Warning

If you call the setForcePreProcessing(true) method, the next call to the PdfDocument.save method rewrites the entire document. A full rewrite invalidates existing signatures.

Output Intents

An output intent specifies the color characteristics of the intended output device (an ICC profile). PDF/A requires an output intent if the document uses device-dependent colors.

If the document does not contain PDF/A output intent, the convertToPdfUA method adds an output intent with the sRGB IEC61966-2.1 profile. The convertToPdfA method adds an output intent if the document uses device-dependent colors: a CMYK profile (CGATS21_CRPC1) if the document uses only CMYK colors, and the sRGB IEC61966-2.1 profile otherwise.

Call the PdfDocument.getOutputIntents() method to access output intents of a document.

The following code snippet converts a document to PDF/A-2b and prints the output intents of the converted document:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            pdfDocument.convertToPdfA(PdfACompatibility.PDF_A2B);

            for (OutputIntent intent : pdfDocument.getOutputIntents()) {
                System.out.println("Subtype: " + intent.getSubtype());
                System.out.println("Output condition: " + intent.getOutputConditionIdentifier());
                System.out.println("Color components: " + intent.getComponentsCount());
            }
        }
    }
}

Declare Conformance Without Conversion

DocumentMetadata.setPdfACompatibility(value) and DocumentMetadata.setPdfUACompatibility(value) methods write the conformance level to XMP metadata and do not modify document content. Call these methods if the document already meets the standard’s requirements (for example, if you create a tagged document in code). Call a validation method to check conformance.

Refer to the following help topic for additional information: Manage PDF Document Properties and XMP Metadata.

See Also