
Metadata is a business card of a particular digital document 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 format 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
- Edit Metadata of a PDF File
- Get Metadata of a PDF File
- Get XMP Metadata from a PDF File
- Set XMP Metadata in a PDF File
- Customize XMP Metadata Namespace in a PDF File
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:
- Firstly, load a PDF document using the Document class.
- Next, create an instance of the DocumentInfo class with the Document class object as an argument.
- Then, set various properties such as Author, CreationDate, Keywords, Subject, Title, etc.
- 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#.
// This code example demonstrates how to set the basic information of a PDF document. | |
// Open document | |
Document pdfDocument = new Document("C:\\Files\\PDF\\sample.pdf"); | |
// Initializa DocumentInfo object | |
DocumentInfo docInfo = new DocumentInfo(pdfDocument); | |
// Specify document information properties | |
docInfo.Author = "Aspose"; | |
docInfo.CreationDate = DateTime.Now; | |
docInfo.Keywords = "Aspose.Pdf, DOM, API"; | |
docInfo.ModDate = DateTime.Now; | |
docInfo.Subject = "PDF Information"; | |
docInfo.Title = "Setting PDF Document Information"; | |
// Save document | |
pdfDocument.Save("C:\\Files\\PDF\\sample_metadata.pdf"); |

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:
- Firstly, load a PDF document using the Document class.
- Next, create an instance of the DocumentInfo class with the Document class object as an argument.
- 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#.
// This code example demonstrates how to get the basic information of a PDF document. | |
// Open document | |
Document pdfDocument = new Document("C:\\Files\\PDF\\sample_metadata.pdf"); | |
// Get document information | |
DocumentInfo docInfo = pdfDocument.Info; | |
// Show document information | |
Console.WriteLine("Author: {0}", docInfo.Author); | |
Console.WriteLine("Creation Date: {0}", docInfo.CreationDate); | |
Console.WriteLine("Keywords: {0}", docInfo.Keywords); | |
Console.WriteLine("Modify Date: {0}", docInfo.ModDate); | |
Console.WriteLine("Subject: {0}", docInfo.Subject); | |
Console.WriteLine("Title: {0}", docInfo.Title); |
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:
- Firstly, load a PDF document using the Document class.
- 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#.
// This code example demonstrates how to get the XMP metadata of a PDF document. | |
// Open document | |
Document pdfDocument = new Document("C:\\Files\\PDF\\sample_xmp.pdf"); | |
// Show XMP information | |
Console.WriteLine("xmp:CreateDate : " + pdfDocument.Metadata["xmp:CreateDate"]); | |
Console.WriteLine("xmp:Nickname : " + pdfDocument.Metadata["xmp:Nickname"]); | |
Console.WriteLine("xmp:CustomProperty : " + pdfDocument.Metadata["xmp:CustomProperty"]); |
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:
- Firstly, load a PDF document using the Document class.
- Next, set metadata values using the Metadata property.
- 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#.
// This code example demonstrates how to set the XMP metadata of a PDF document. | |
// Open document | |
Document pdfDocument = new Document("C:\\Files\\PDF\\sample.pdf"); | |
// Set properties | |
pdfDocument.Metadata["xmp:CreateDate"] = DateTime.Now; | |
pdfDocument.Metadata["xmp:Nickname"] = "Nickname"; | |
pdfDocument.Metadata["xmp:CustomProperty"] = "Custom Value"; | |
// Save document | |
pdfDocument.Save("C:\\Files\\PDF\\sample_xmp.pdf"); |
Customize XMP Metadata Namespace in a PDF File
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:
- Firstly, load a PDF document using the Document class.
- Next, call the RegisterNamespaceUri method with a prefix and namespace URI as arguments.
- Then, set metadata values using the Metadata property.
- 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#.
// This code example demonstrates how to set the custom namepsace URI in a PDF document. | |
// Open document | |
Document pdfDocument = new Document("C:\\Files\\PDF\\sample.pdf"); | |
// Set properties | |
pdfDocument.Metadata.RegisterNamespaceUri("myown", "http:// myown.xyz.com/xap/1.0/"); | |
pdfDocument.Metadata["myown:ModifyDate"] = DateTime.Now; | |
pdfDocument.Metadata["myown:CreateDate"] = DateTime.Now; | |
pdfDocument.Metadata["myown:DeveloperName"] = "Developer Name"; | |
pdfDocument.Metadata["myown:MyProperty"] = "My Custom Value"; | |
// Save document | |
pdfDocument.Save("C:\\Files\\PDF\\sample_myown.pdf"); |
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.