Editing PDF metadata with Nutrient Java SDK
Use PDF metadata editing to standardize document properties and improve search workflows.
Common use cases include:
- Updating title and author fields
- Normalizing metadata across large document sets
- Exporting metadata for audit or integration workflows
This guide also shows how to export Extensible Metadata Platform (XMP) metadata as XML.
Download sampleHow Nutrient helps
Nutrient Java SDK provides metadata APIs for reading, updating, and exporting PDF metadata.
The SDK enables you to:
- Update standard properties such as title and author
- Export XMP as XML
- Integrate metadata updates into existing Java workflows
Preparing the project
Start by specifying a package name and importing the required classes:
package io.nutrient.Sample;
import io.nutrient.sdk.Document;import io.nutrient.sdk.exceptions.NutrientException;import io.nutrient.sdk.editors.PdfEditor;import java.io.File;import java.io.FileWriter;import java.io.IOException;Create the main method and declare checked exceptions used in this sample:
public class PDFMetadataEditor { public static void main(String[] args) throws NutrientException, IOException {Working with PDF metadata
Open the PDF in a try-with-resources statement(opens in a new tab) so Java closes resources after processing.
In this sample, you open from a file path. The SDK also supports streams:
input.pdfmust exist in the current working directory. If the file is located elsewhere, use an absolute path (for example,C:\\docs\\input.pdf).
try (Document document = Document.open("input.pdf")) {Create a PdfEditor instance to update document metadata:
PdfEditor editor = PdfEditor.edit(document);Modifying metadata properties
Update metadata fields such as author and title:
editor.getMetadata().setAuthor("New author value"); editor.getMetadata().setTitle("New title value");Exporting XMP metadata
Export XMP metadata as XML for inspection, backup, or integration:
String xmpMetadata = editor.getMetadata().getXMP();
try (FileWriter writer = new FileWriter("output_metadata.xml")) { writer.write(xmpMetadata); }Saving the modified PDF
Save the modified PDF after metadata updates:
editor.saveAs("output.pdf"); editor.close(); } }}Error handling
The sample can throw:
NutrientExceptionfor SDK and document-processing issuesIOExceptionfor file I/O operations
In production code:
- Catch these exceptions.
- Return clear error messages.
- Log failure details for debugging.
Conclusion
Use this workflow to edit PDF metadata:
- Open the document with try-with-resources.
- Create a
PdfEditor. - Update metadata fields such as author and title.
- Export XMP metadata with
getXMP(). - Save the modified PDF with
saveAs().
For related document workflows, refer to the Java SDK guides.
Download this ready-to-use sample package.