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.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document
Document document = new Document(dataDir + "GetFileInfo.pdf");
// Get document information
DocumentInfo docInfo = document.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);
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:
- Create a DocumentInfo object.
- Set the values of the properties.
- Save the updated document using the Document class’ Save method.
The following code snippet shows you how to set PDF file information.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document
Document document = new Document(dataDir + "SetFileInfo.pdf");
// Specify document information
DocumentInfo docInfo = new DocumentInfo(document);
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 output document
document.Save(dataDir + "SetFileInfo_out.pdf");
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:
- Create a Document object and open the input PDF file.
- Get the file’s metadata using the Metadata property.
The following code snippet shows you how to get metadata from the PDF file.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document
Document document = new Document(dataDir + "GetXMPMetadata.pdf");
// Get properties
Console.WriteLine(document.Metadata["xmp:CreateDate"]);
Console.WriteLine(document.Metadata["xmp:Nickname"]);
Console.WriteLine(document.Metadata["xmp:CustomProperty"]);
Set XMP Metadata in a PDF File
Aspose.PDF allows you to set metadata in a PDF file. To set metadata:
- Create a Document object.
- Set metadata values using the Metadata property.
- 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.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document
Document document = new Document(dataDir + "SetXMPMetadata.pdf");
// Set properties
document.Metadata["xmp:CreateDate"] = DateTime.Now;
document.Metadata["xmp:Nickname"] = "Nickname";
document.Metadata["xmp:CustomProperty"] = "Custom Value";
// Save document
document.Save(dataDir + "SetXMPMetadata_out.pdf");
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.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document
Document document = new Document(dataDir + "SetXMPMetadata.pdf");
document.Metadata.RegisterNamespaceUri("xmp", "http:// Ns.adobe.com/xap/1.0/"); // Xmlns prefix was removed
document.Metadata["xmp:ModifyDate"] = DateTime.Now;
// Save document
document.Save(dataDir + "SetPrefixMetadata_out.pdf");