Edit Metadata of PDF Files using Java

The metadata of a document contains basic information such as title, author, subject, and keywords. The Extensible Metadata Platform (XMP) is an XML‑based standard for storing metadata as key/value pairs. Using Java, we can add, edit, or read both document information and XMP metadata of a PDF programmatically. This article shows how to edit PDF properties and metadata using Java.

The following topics are covered:

Java API to Edit PDF Properties and Metadata

To edit PDF properties and metadata, we use Aspose.PDF for Java API. It lets you generate, modify, convert, render, secure, and print supported documents without Adobe Acrobat. Download the JAR or add the following pom.xml configuration to a Maven‑based Java project.

<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 with the PdfFileInfo class, which represents a PDF’s meta‑information. Follow these steps:

  1. Load a PDF document using the PdfFileInfo class.
  2. Set properties such as Author, Creator, Keywords, Subject, Title, etc.
  3. Save the PDF with the saveNewInfo() method, providing the output path.

The code sample below demonstrates 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

To read basic PDF information:

  1. Load the PDF with the PdfFileInfo class.
  2. Display the document information by accessing the meta properties.

The following code 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

To read XMP metadata:

  1. Load the PDF using the Document class.
  2. Retrieve metadata with the get_Item() method of the Metadata class.

The code sample below 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

To set XMP metadata:

  1. Load the PDF with the Document class.
  2. Use the set_Item() method of the Metadata class to assign values.
  3. Save the PDF using the Document.save() method.

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

Customize XMP Metadata Namespace in a PDF File

You can define a custom namespace URI instead of the default XMP specifications. Use the registerNamespaceUri method in the Metadata class. Follow these steps:

  1. Load the PDF with the Document class.
  2. Call registerNamespaceUri() with a prefix and namespace URI.
  3. Set metadata values using the set_Item() method.
  4. Save the PDF with Document.Save().

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

You can read the customized XMP metadata properties using the same approach as before.

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 limits by requesting a free temporary license.

Conclusion

In this article, we learned how to:

  • add or edit basic PDF information using Java;
  • set and retrieve XMP metadata in a PDF file;
  • define a custom metadata namespace URI with a prefix.

For more details, see the Aspose.PDF for Java documentation. If you have questions, visit the forum.

See Also