DEV Community

AnnaGraz
AnnaGraz

Posted on

Java Convert PDF to PDFA

PDF/A is a variation of the PDF format that ensures a document can be reproduced exactly the same way, regardless of what software is used. Basically, all of the information necessary for displaying the document and its elements in the same manner is embedded in the file—which ensure your documents safe, accessible, and secure for the long term. This article will demonstrate how to convert PDF to PDF/A. Spire.PDF for java supports to convert PDF to PdfA1A, PDF to PdfA1B, PDF to PdfA2A, PDF to PdfA2B, PDF to PdfA3A, and PDF to PdfA3B.

Part 1: Understanding the Spire.PDF Library

To convert PDF to PDF/A, we will make use of the Spire.PDF library. Spire.PDF for Java is a PDF API that enables Java applications to read, write and save PDF documents without installing Adobe Acrobat on the machine.

Before we can use the Spire.PDF to save PDF as PDF/A, we need to add its dependency to our Java project. We can do this by adding the following dependency to our Maven project:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>9.7.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Part 2: Writing the Java Code

Once we have added the Spire.PDF for java dependency to our project, we can start writing the Java code to save PDF as PDF/A.
Create a PdfStandardsConverter instance, and pass in a sample PDF file as a parameter.

  • Convert the sample file to PdfA1A conformance level using PdfStandardsConverter.toPdfA1A() method.
  • Convert the sample file to PdfA1B conformance level using PdfStandardsConverter. toPdfA1B() method.
  • Convert the sample file to PdfA2A conformance level using PdfStandardsConverter. toPdfA2A() method.
  • Convert the sample file to PdfA2B conformance level using PdfStandardsConverter. toPdfA2B() method.
  • Convert the sample file to PdfA3A conformance level using PdfStandardsConverter. toPdfA3A() method.
  • Convert the sample file to PdfA3B conformance level using PdfStandardsConverter. toPdfA3B() method.
import com.spire.pdf.conversion.PdfStandardsConverter;

public class ConvertPdfToPdfA {
    public static void main(String[] args) {

        //Create a PdfStandardsConverter instance, and pass in a sample file as a parameter
        PdfStandardsConverter converter = new PdfStandardsConverter("sample.pdf");

        //Convert to PdfA1A
        converter.toPdfA1A("output/ToPdfA1A.pdf");

        //Convert to PdfA1B
        converter.toPdfA1B("output/ToPdfA1B.pdf");

        //Convert to PdfA2A
        converter.toPdfA2A( "output/ToPdfA2A.pdf");

        //Convert to PdfA2B
        converter.toPdfA2B("output/ToPdfA2B.pdf");

        //Convert to PdfA3A
        converter.toPdfA3A("output/ToPdfA3A.pdf");

        //Convert to PdfA3B
        converter.toPdfA3B("output/ToPdfA3B.pdf");
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In conclusion, it is easy to save PDF to PDF/A by using Spire.PDF for java. Spire.PDF also supports to convert PDF to other formats, such as PDF to image, PDF to Word, PDF to Excel, PDF to PowerPoint and PDF to SVG.

Top comments (0)