Edit Metadata of PDF Files using C#

Metadata is a business card of a particular digital doc­ument that consists of a set of properties. These properties contain basic information about the document such as title, author, subject, keywords, etc. Extensible Metadata Platform (XMP) is an XML-based for­mat that allows saving the document metadata in a key/value pair. We can add/edit document information and XMP metadata in a PDF document programmatically. In this article, we will learn how to edit the metadata of PDF files using C#.

The following topics shall be covered in this article:

C# API to Edit Metadata of PDF Files

To edit metadata information in a PDF document, we will be using Aspose.PDF for .NET API. It allows us to generate, modify, convert, render, secure and print supported documents without using Adobe Acrobat. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Pdf

Edit Metadata of a PDF File in C#

We can edit PDF document information using the DocumentInfo class that represents the meta-information of a PDF document. We can set various pre-defined properties by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the DocumentInfo class with the Document class object as an argument.
  3. Then, set various properties such as Author, CreationDate, Keywords, Subject, Title, etc.
  4. Finally, save the PDF file using the Document.Save() method with the output file path as an argument.

The following code sample shows how to edit metadata of a PDF file using C#.

Edit Metadata of a PDF File in C#.

Edit Metadata of a PDF File in C#.

Get Metadata of a PDF File using C#

We can read the basic information of a PDF document ****by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the DocumentInfo class with the Document class object as an argument.
  3. Finally, show the document information by reading the values of metadata properties.

The following code sample shows how to get metadata of a PDF file using C#.

Author: Aspose
Creation Date: 2/9/2022 9:47:00 AM
Keywords: Aspose.Pdf, DOM, API
Modify Date: 2/9/2022 9:47:00 AM
Subject: PDF Information
Title: Setting PDF Document Information

Get XMP Metadata of a PDF File using C#

We can read the XMP metadata of a PDF document ****by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Finally, read the Metadata property and extract the information.

The following code sample shows how to get XMP metadata of a PDF file using C#.

xmp:CreateDate: 2022-02-09T08:57:00.7+05:00
xmp:Nickname: Nickname
xmp:CustomProperty: Custom Value

Set XMP Metadata in a PDF File using C#

We can set the XMP metadata in a PDF file using the Metadata property of the Document class by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, set metadata values using the Metadata property.
  3. Finally, save the PDF file using the Document.Save() method with the output file path as an argument.

The following code sample shows how to set XMP metadata of a PDF file using C#.

Customize XMP Metadata Namespace in a PDF File

Adobe XMP Specification defines the following four (4) core namespaces that we normally use:

  1. Dublin Core with namespace URI as “http://purl.org/dc/elements/1.1/” and its preferred namespace prefix is ‘dc’.
  2. The XMP with namespace URI as http://ns.adobe.com/xap/1.0/ and its preferred namespace prefix is ‘xmp’.
  3. XMP Rights Management with namespace URI as http://ns.adobe.com/xap/1.0/rights/ and its preferred namespace prefix is ‘xmpRights’.
  4. XMP Media Management with namespace URI as http://ns.adobe.com/xap/1.0/mm/ and its preferred namespace prefix is ‘xmpMM’.

We can also set the customized namespace URI instead of defined XMP specifications in a PDF file. For this purpose, the API provides the RegisterNamespaceUri method in the Metadata class. We can create a new metadata namespace with a prefix by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, call the RegisterNamespaceUri method with a prefix and namespace URI as arguments.
  3. Then, set metadata values using the Metadata property.
  4. Finally, save the PDF file using the Document.Save() method with the output file path as an argument.

The following code sample shows how to set custom metadata namespace in a PDF file using C#.

We can read the customized XMP metadata properties by following the steps mentioned earlier.

myown:ModifyDate: 2022-02-09T10:38:26.8+05:00
myown:CreateDate: 2022-02-09T10:38:26.8+05:00
myown:DeveloperName: Developer Name
myown:MyProperty: My Custom Value

Get a Free API License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • add/ edit the basic information of a PDF documents using C#;
  • set/ get the XMP metadata in a PDF file using C#;
  • set custom metadata namespace URI with a prefix.

Besides, you can learn more about Aspose.PDF for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also