Edit Metadata of PDF Files using Java

The metadata of a document contains basic information about the document in the form of properties such as title, author, subject, keywords, etc. The Extensible Metadata Platform (XMP) is an XML-based standard for storing document metadata as a key/value pair. We can add, edit or read the document information and XMP metadata of a PDF document programmatically. In this article, we will learn how to edit PDF properties and metadata using Java.

The following topics shall be covered in this article:

Java API to Edit PDF Properties and Metadata

To edit PDF properties and metadata information, we will be using Aspose.PDF for Java API. It allows us to generate, modify, convert, render, secure, and print supported documents without using Adobe Acrobat. Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>22.3</version>
</dependency>

Edit PDF Properties using Java

We can edit PDF document information using the PdfFileInfo class that represents the meta-information of a PDF document. We can set various pre-defined properties by following the steps given below:

  1. Firstly, load a PDF document using the PdfFileInfo class.
  2. Set various properties such as Author, Creator, Keywords, Subject, Title, etc.
  3. Finally, save the PDF file using the saveNewInfo() method with the output file path as an argument.

The following code sample shows how to edit meta properties of a PDF file using Java.

Edit Meta Properties of a PDF File in Java.

Edit Meta Properties of a PDF File in Java.

Read PDF Properties using Java

We can read the basic information of a PDF document by following the steps given below:

  1. Firstly, load a PDF document using the PdfFileInfo class.
  2. Finally, show the document information by reading the values of meta properties.

The following code sample shows how to get meta properties of a PDF file using Java.

Subject :PDF Information
Title :Editing Metadata
Keywords :Aspose.Pdf, DOM, API
Creator :Aspose
Creation Date :D:20170612160123-04'00'
Modification Date :D:20220405214422+05'00'
Is Valid PDF :true
Is Encrypted :false

Get XMP Metadata of a PDF File in Java

We can read the XMP metadata of a PDF document by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Finally, read metadata using the get_Item() method of the Metadata class and extract the information.

The following code sample shows how to get XMP metadata of a PDF file using Java.

xmp:CreateDate : 2022-04-05T10:05:24.4
xmp:Nickname : Nickname
xmp:CustomProperty : Custom Value

Set XMP Metadata in a PDF File in Java

We can set the XMP metadata in a PDF file by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, set metadata values using the set_Item() method of the Metadata class.
  3. Finally, save the PDF file using the Document.save() method with the output file path as an argument.

The following code sample shows how to set XMP metadata of a PDF file using Java.

Customize XMP Metadata Namespace in a PDF File

We can set the customized namespace URI instead of defined XMP specifications in a PDF file. For this purpose, the API provides the registerNamespaceUri method in the Metadata class. We can create a new metadata namespace with a prefix by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, call the registerNamespaceUri() method with a prefix and namespace URI as arguments.
  3. Then, set metadata values using the set_Item() method.
  4. Finally, save the PDF file using the Document.Save() method with the output file path as an argument.

The following code sample shows how to set custom metadata namespace in a PDF file using Java.

We can read the customized XMP metadata properties by following the steps mentioned earlier.

NamespaceUri: http:// myown.xyz.com/xap/1.0/
myown:ModifyDate: 2022-04-05T10:18:45.9
myown:CreateDate: 2022-04-05T10:18:45.9
myown:DeveloperName: Developer Name
myown:MyProperty: My Custom Value

Get a Free API License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • add/ edit the basic information of a PDF document using Java;
  • set/ get the XMP metadata in a PDF file using Java;
  • set custom metadata namespace URI with a prefix.

Besides, you can learn more about Aspose.PDF for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also