Editing PDF metadata with Nutrient Java SDK
PDF documents contain metadata that provides information about the document, such as the title, author, creation date, and more. This metadata is crucial for document management systems, search functionality, and maintaining document provenance. Being able to programmatically edit this metadata enables organizations to standardize document properties, update author information, or add custom metadata fields.
The Extensible Metadata Platform (XMP) is Adobe’s standard for embedding metadata in files. It provides a common framework for storing standardized and custom properties, making it essential for professional document workflows and digital asset management.
For organizations managing large document repositories, the ability to edit PDF metadata programmatically becomes essential for maintaining consistent document properties, improving searchability, and ensuring compliance with metadata standards.
Streamlining metadata management with our Java SDK
What makes this feature particularly valuable is how effortlessly it can be implemented using our SDK. With just a few lines of code, developers can read, modify, and export PDF metadata. Whether you’re building a document management system or adding metadata editing functionality to an existing application, our SDK delivers comprehensive metadata manipulation capabilities right out of the box.
Preparing the project
Start by specifying a package name and importing the required classes:
package io.nutrient.Sample;
import io.nutrient.sdk.*;import io.nutrient.sdk.exceptions.NutrientException;import io.nutrient.sdk.editors.*;import java.io.File;import java.io.FileWriter;import java.io.IOException;
Then create the main function and specify that it can throw exceptions. This allows for proper error handling in production environments:
public class PDFMetadataEditor { public static void main(String[] args) throws NutrientException, IOException {
Once the inherent setup from a Java application is done, focus on what’s specific to our SDK.
The first step is to initialize the SDK by registering the license. This needs to be done only once during the application’s lifetime and must occur before executing any additional logic:
License.registerKey("");
Working with PDF metadata
With the SDK successfully initialized, you can begin working with the classes it offers. This guide concentrates on the Document
and PdfEditor
classes. You can initialize Document
using a try-with-resources statement(opens in a new tab), which ensures proper lifecycle management of the document instance.
The SDK supports multiple integration methods, enabling flexibility in how you connect with your application. The source file can be specified either via a file path or a stream. This guide uses a file path as the source:
try (Document document = Document.open("input.pdf")) {
Create a PdfEditor
instance to modify the document:
PdfEditor editor = PdfEditor.edit(document);
Modifying metadata properties
Now you can update the metadata fields. The SDK provides access to standard PDF metadata properties like author, title, subject, and keywords:
editor.getMetadata().setAuthor("New author value"); editor.getMetadata().setTitle("New title value");
Exporting XMP metadata
The SDK also enables you to export the XMP metadata as XML. This is useful for inspection, backup, or integration with other systems:
String xmpMetadata = editor.getMetadata().getXMP();
try (FileWriter writer = new FileWriter("output_metadata.xml")) { writer.write(xmpMetadata); }
Saving the modified PDF
After making all desired metadata changes, save the PDF with the updated properties:
editor.saveAs("output.pdf"); editor.close(); } }}
That’s it! You’ve successfully modified PDF metadata and exported XMP data.
Error handling
Nutrient Java SDK handles errors with exception handling. The methods in this guide can throw NutrientException
for SDK-related errors and IOException
for file operations. This helps with troubleshooting and implementing error handling logic.
Conclusion
That’s all it takes to edit PDF metadata using Nutrient Java SDK! With just a few lines of code, you can read, modify, and export document metadata, enabling powerful document management capabilities in your applications. You can also download this ready-to-use sample package, fully configured to help you dive into the Java SDK right away.