DEV Community

Cover image for How to Read, Update, and Clear PDF Document Properties in Java
Dheeraj Malik
Dheeraj Malik

Posted on

How to Read, Update, and Clear PDF Document Properties in Java

PDF files can contain information beyond what's displayed on their pages, including the document title, author, subject, keywords, and creation date. These properties can be viewed in PDF readers and are sometimes used to organize or identify documents.

You may need to check these details, correct outdated information, or clear certain properties before sharing a PDF.

This article covers how to read, update, and clear common PDF document properties using Java.

1. Add the Maven Dependency

First, add a PDF processing library to your Java project. If you're using Maven, include the following repository and dependency in your pom.xml file:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.10.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

The examples below use an existing PDF named report.pdf. Each example is a standalone Java program.

2. Read PDF Document Properties in Java

To retrieve information such as the document title, author, or keywords, use PdfDocument.getDocumentInformation().

This method returns a PdfDocumentInformation object, which provides access to the PDF's standard document properties.

Here's a complete example:

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentInformation;

public class ReadPDFProperties {

    public static void main(String[] args) {

        PdfDocument pdf = new PdfDocument();

        try {
            // Load the PDF document
            pdf.loadFromFile("report.pdf");

            // Access the document properties
            PdfDocumentInformation info =
                    pdf.getDocumentInformation();

            // Read and display the properties
            System.out.println("Title: " + info.getTitle());
            System.out.println("Author: " + info.getAuthor());
            System.out.println("Subject: " + info.getSubject());
            System.out.println("Keywords: " + info.getKeywords());

            System.out.println("Creator: " + info.getCreator());
            System.out.println("Producer: " + info.getProducer());

            System.out.println("Creation Date: "
                    + info.getCreationDate());
            System.out.println("Modification Date: "
                    + info.getModificationDate());

        } finally {
            // Release document resources
            pdf.close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The program prints the available properties to the console.

Most fields are straightforward, but two are worth distinguishing:

  • Creator: The application used to create the original document.
  • Producer: The software or component that generated the PDF.

For example, if a document was written in Microsoft Word and later exported to PDF, the creator and producer information may refer to different applications.

Also, not every PDF contains all these properties. Some values may be empty or null, depending on how the file was created.

3. Update PDF Document Properties in Java

Document properties may need to be updated when a report is finalized or its descriptive information changes.

Suppose a project report has an outdated title and author. You can update these fields directly without recreating the document's page content.

The PdfDocumentInformation class provides methods such as setTitle(), setAuthor(), setSubject(), and setKeywords() for this purpose.

The following example updates several properties and sets the modification date to the current time.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentInformation;

import java.util.Date;

public class UpdatePDFProperties {

    public static void main(String[] args) {

        PdfDocument pdf = new PdfDocument();

        try {
            // Load the PDF document
            pdf.loadFromFile("report.pdf");

            // Access the document properties
            PdfDocumentInformation info =
                    pdf.getDocumentInformation();

            // Update the document title
            info.setTitle("Q3 2026 Project Report");

            // Update the author
            info.setAuthor("Project Management Team");

            // Update the subject
            info.setSubject(
                    "Quarterly Project Progress and Risk Analysis");

            // Update the keywords
            info.setKeywords(
                    "project report, progress, risk analysis");

            // Set the modification date
            info.setModificationDate(new Date());

            // Save the updated PDF
            pdf.saveToFile("report-updated.pdf");

            System.out.println("PDF properties updated.");

        } finally {
            // Release document resources
            pdf.close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The program creates report-updated.pdf with the new property values.

Here's what the main methods change:

Method Property
setTitle() Document title
setAuthor() Author
setSubject() Subject or description
setKeywords() Keywords
setModificationDate() Modification date

Using new Date() sets the modification date to the current system time.

One important distinction: changing a PDF's document properties does not change the text displayed on its pages.

For example, setting the title to "Q3 2026 Project Report" won't automatically update a heading on the first page. The document's metadata and visible page content are stored separately.

4. Clear Common PDF Document Properties in Java

Sometimes a PDF contains document properties that are no longer relevant, such as an old author name, internal project keywords, or information about the original editing application.

If you want to clear these fields, you can set their values to empty strings.

The following example clears the title, author, subject, keywords, and creator fields.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentInformation;

public class ClearPDFProperties {

    public static void main(String[] args) {

        PdfDocument pdf = new PdfDocument();

        try {
            // Load the PDF document
            pdf.loadFromFile("report.pdf");

            // Access the document properties
            PdfDocumentInformation info =
                    pdf.getDocumentInformation();

            // Clear selected document properties
            info.setTitle("");
            info.setAuthor("");
            info.setSubject("");
            info.setKeywords("");
            info.setCreator("");

            // Save the modified PDF
            pdf.saveToFile("report-cleared-properties.pdf");

            System.out.println(
                    "Selected PDF properties have been cleared.");

        } finally {
            // Release document resources
            pdf.close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The resulting file is saved as report-cleared-properties.pdf.

Here, the property setters assign empty strings to the selected fields. This clears their values without changing the PDF's visible page content.

However, clearing these fields is not the same as removing all metadata from a PDF.

PDFs may also contain XMP metadata, custom properties, and other embedded information. Those are not necessarily affected by the setters used above.

In addition, the PDF processing software may write or update certain properties when saving the document.

If metadata removal is required for privacy or compliance purposes, these operations alone are not sufficient. You'll need to inspect the output file and check for other metadata and potentially sensitive content.

5. Considerations

Standard Document Properties vs. XMP Metadata

PDF files can store common properties, such as titles and authors, in a document information dictionary. They may also contain XMP metadata, which stores structured information about the document.

These two sources can contain overlapping information, but updating one doesn't necessarily mean the other will be updated.

The examples in this article focus on common properties accessed through PdfDocumentInformation. If you need to inspect or change XMP metadata, that requires additional handling.

PDF Dates Are Different from File System Dates

The creation and modification dates stored inside a PDF are not the same as the timestamps managed by your operating system.

For example, copying an older PDF to another folder may change its file system timestamps without changing the creation date recorded in its internal document properties.

When using dates for document tracking or archiving, make sure you're reading the appropriate source.

Save and Check the Output File

Reading document properties doesn't modify the PDF, but updating or clearing them requires saving the document.

All examples that make changes save the result to a new file, leaving the original unchanged.

After saving, it's worth reopening the output PDF and checking its properties. Some fields may be added or updated during the save process, so the final file should be inspected rather than assuming every change was preserved exactly as requested.

Top comments (0)