Set PDF Document Properties using Java

Document properties are some descriptive information such as title, author, subject, key words, creation date, creator and producer, etc. This article will introduce how to set PDF document properties with a 3rd party free API — Free Spire.XLS for Java.

Import JAR dependency (2 methods)
● Download the Free Spire.PDF for Java and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

● You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Relevant code snippet:

import com.spire.pdf.PdfDocument;

import java.util.Date;

public class SetDocumentProperties {
public static void main(String[] args)
{
//Instantiate a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF document
pdf.loadFromFile("input.pdf");

//Set title
pdf.getDocumentInformation().setTitle("Test Document");
//Set author
pdf.getDocumentInformation().setAuthor("Andrew");
//Set subject
pdf.getDocumentInformation().setSubject("Document Properties Setting");
//Set key words
pdf.getDocumentInformation().setKeywords("Document Properties, PDF");
//Set creation date
pdf.getDocumentInformation().setCreationDate(new Date());
//set creator
pdf.getDocumentInformation().setCreator("Free Spire.PDF for Java");
//set producer
pdf.getDocumentInformation().setProducer("Free Spire.PDF for Java");

//save the document
pdf.saveToFile("SetProperties.pdf");
}
}

Sharing Java Code