Working with PDF File Metadata in C#

The following code snippet also work with Aspose.PDF.Drawing library.

Get PDF File Information

In order to get file specific information of a PDF file, you first need to get the DocumentInfo object using Info property of the Document object. Once the DocumentInfo object is retrieved, you can get the values of the individual properties. The following code snippet shows you how to get PDF file information.

Set PDF File Information

Aspose.PDF for .NET allows you to set file-specific information for a PDF, information like author, creation date, subject, and title. To set this information:

  1. Create a DocumentInfo object.
  2. Set the values of the properties.
  3. Save the updated document using the Document class’ Save method.

The following code snippet shows you how to set PDF file information.

Get XMP Metadata from PDF File

Aspose.PDF allows you to access a PDF file’s XMP metadata. To get a PDF file’s metadata:

  1. Create a Document object and open the input PDF file.
  2. Get the file’s metadata using the Metadata property.

The following code snippet shows you how to get metadata from the PDF file.

Set XMP Metadata in a PDF File

Aspose.PDF allows you to set metadata in a PDF file. To set metadata:

  1. Create a Document object.
  2. Set metadata values using the Metadata property.
  3. Save the updated document using the Save method of the Document object.

The following code snippet shows you how to set metadata in a PDF file.

Insert Metadata with Prefix

Some developers need to create a new metadata namespace with a prefix. The following code snippet shows how to insert metadata with prefix.

Working with PDF 2.0 Metadata

Starting from version 22.11, Aspose.PDF supports saving documents as PDF 2.0. According to the PDF 2.0 specification, certain fields in the Info Dictionary are deprecated:

  • Title
  • Author
  • Creator
  • Producer
  • Subject
  • Keywords

Only CreationDate and ModDate remain in the Info Dictionary for PDF 2.0 documents. All deprecated fields are automatically stored in XMP metadata using the xmp: prefix.

Aspose.PDF provides a unified API for working with metadata. You can use the familiar DocumentInfo class to set metadata properties, and the library will automatically handle the storage location based on the PDF version:

  • PDF 1.x: Metadata is stored in the Info Dictionary
  • PDF 2.0: Deprecated fields (Title, Author, Creator, Producer, Subject, Keywords) are automatically transferred to XMP metadata, while only CreationDate and ModDate remain in the Info Dictionary

This means you can continue using the same code for setting metadata regardless of the target PDF version.

Set Metadata in PDF 2.0 Using DocumentInfo (Unified API)

The unified API allows you to use the familiar DocumentInfo class to set metadata. When saving as PDF 2.0, the deprecated fields are automatically transferred to XMP metadata.

Get Metadata from PDF 2.0 Using XMP

For PDF 2.0 documents, you can directly access metadata using the Metadata property with the xmp: prefix.

Set Metadata in PDF 2.0 Using XMP Directly

You can also set metadata directly using the Metadata property with XmpValue objects.

Convert PDF 1.x to PDF 2.0 with Metadata

You can convert an existing PDF 1.x document to PDF 2.0 format while setting metadata. During conversion, the deprecated Info Dictionary fields will be automatically transferred to XMP metadata.