Wednesday, 20 April 2022

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a client or colleague is using. This article will show you how to programmatically change the PDF version using Java.

DEPENDENCY

First of all, you’re required to add Spire.Pdf.jar to your Java program. Download the package of Free Spire.PDF for Java from this link, find it in the lib folder. Or if you use Maven, just type the following codes in the pom.xml file to import the JAR file to IntelliJ IDEA.

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>5.4.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.PDF for Java supports changing the version of a PDF document using PdfDocument.getFileInfo().setVersion() method. The detailed steps are listed below.

l  Create a PdfDocument instance.

l  Load a PDF sample document using PdfDocument.loadFromFile() method.

l  Change the PDF version to a specified version using PdfDocument.getFileInfo().setVersion() method.

l  Save the output document to another file using PdfDocument.saveToFile() method.

import com.spire.pdf.*;

public class ChangePdfVersion {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument document = new PdfDocument();

//Load a sample PDF file
document.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.pdf");

//Change the PDF to version 1.7
document.getFileInfo().setVersion(PdfVersion.Version_1_7);

//Save to file
document.saveToFile("output/ChangePdfVersion.pdf", FileFormat.PDF);
document.close();
}
}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...