Work with PDF File Metadata in Java

Aspose.PDF for Java provides two main ways to work with metadata:

  • The DOM API through Document, DocumentInfo, and document.getMetadata().
  • The facade API through PdfFileInfo.

Get PDF file information

Use this example when you need to read standard document information fields such as author, title, subject, or keywords.

  1. Open the source PDF Document.
  2. Access the DocumentInfo object.
  3. Read the required metadata fields and output their values.
public static void getPdfFileInformation(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        DocumentInfo docInfo = document.getInfo();

        System.out.println("Author: " + docInfo.getAuthor());
        System.out.println("Creation Date: " + docInfo.getCreationDate());
        System.out.println("Keywords: " + docInfo.getKeywords());
        System.out.println("Modify Date: " + docInfo.getModDate());
        System.out.println("Subject: " + docInfo.getSubject());
        System.out.println("Title: " + docInfo.getTitle());
    }
}

Set metadata with a namespace prefix

Use this example when you need to add or update an XMP property by using a registered namespace prefix.

  1. Open the source PDF Document.
  2. Register the required XMP namespace and add the metadata item.
  3. Save the updated document.
public static void setPrefixMetadata(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getMetadata().registerNamespaceUri("xmp", "http://ns.adobe.com/xap/1.0/");
        document.getMetadata().addItem("xmp:ModifyDate", OffsetDateTime.now().toString());
        document.save(outputFile.toString());
    }
    System.out.println("Prefix metadata saved to " + outputFile);
}

Update document information fields

Use this example when you want to write standard PDF file properties such as author, title, producer, or creation date.

  1. Open the source PDF Document.
  2. Access DocumentInfo and assign new metadata values.
  3. Save the document with the updated file information.
public static void setFileInformation(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        DocumentInfo docInfo = document.getInfo();
        Date now = new Date();

        docInfo.setAuthor("Aspose");
        docInfo.setCreationDate(now);
        docInfo.setKeywords("Aspose.Pdf, DOM, API");
        docInfo.setModDate(now);
        docInfo.setSubject("PDF Information");
        docInfo.setTitle("Setting PDF Document Information");
        docInfo.setProducer("Custom producer");
        docInfo.setCreator("Custom creator");

        document.save(outputFile.toString());
    }
    System.out.println("File information saved to " + outputFile);
}

Set XMP metadata properties

Use this example when you need to store additional XMP entries, including custom metadata values.

  1. Open the source PDF Document.
  2. Add the required XMP metadata items through document.getMetadata().
  3. Save the output file.
public static void setXmpMetadata(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getMetadata().addItem("xmp:CreateDate", OffsetDateTime.now().toString());
        document.getMetadata().addItem("xmp:Nickname", "Nickname");
        document.getMetadata().addItem("xmp:CustomProperty", "Custom Value");
        document.save(outputFile.toString());
    }
    System.out.println("XMP metadata saved to " + outputFile);
}