Set XMP Metadata of an existing PDF
In order to set XMP metadata in a PDF file, you need to create PdfXmpMetadata object and bind the PDF file using BindPdf method. You can use Add method of the PdfXmpMetadata class to add different properties. Finally, call the Save method of the PdfXmpMetadata class. The following code snippet shows you how to add XMP metadata in a PDF file.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddXmpMetadata ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdfFacades_WorkingDocuments ();
// Create PdfXmpMetadata object
using ( var xmpMetaData = new Aspose . Pdf . Facades . PdfXmpMetadata ())
{
// Bind pdf file to the object
xmpMetaData . BindPdf ( dataDir + "SetXMPMetadata.pdf" );
// Add create date
xmpMetaData . Add ( Aspose . Pdf . Facades . DefaultMetadataProperties . CreateDate , DateTime . Now . ToString ());
// Change meta data date
xmpMetaData [ Aspose . Pdf . Facades . DefaultMetadataProperties . MetadataDate ] = DateTime . Now . ToString ();
// Add creator tool
xmpMetaData . Add ( Aspose . Pdf . Facades . DefaultMetadataProperties . CreatorTool , "Creator tool name" );
// Remove modify date
xmpMetaData . Remove ( Aspose . Pdf . Facades . DefaultMetadataProperties . ModifyDate );
// Add user defined property
// Step #1: register namespace prefix and URI
xmpMetaData . RegisterNamespaceURI ( "customNamespace" , "http:// Www.customNameSpaces.com/ns/" );
// Step #2: add user property with the prefix
xmpMetaData . Add ( "customNamespace:UserPropertyName" , "UserPropertyValue" );
// Change user defined property
xmpMetaData [ "customNamespace:UserPropertyName" ] = "UserPropertyValue2" ;
// Save xmp meta data in the pdf file
xmpMetaData . Save ( dataDir + "SetXMPMetadata_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddXmpMetadata ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdfFacades_WorkingDocuments ();
// Create PdfXmpMetadata object
using var xmpMetaData = new Aspose . Pdf . Facades . PdfXmpMetadata ();
// Bind pdf file to the object
xmpMetaData . BindPdf ( dataDir + "SetXMPMetadata.pdf" );
// Add create date
xmpMetaData . Add ( Aspose . Pdf . Facades . DefaultMetadataProperties . CreateDate , DateTime . Now . ToString ());
// Change meta data date
xmpMetaData [ Aspose . Pdf . Facades . DefaultMetadataProperties . MetadataDate ] = DateTime . Now . ToString ();
// Add creator tool
xmpMetaData . Add ( Aspose . Pdf . Facades . DefaultMetadataProperties . CreatorTool , "Creator tool name" );
// Remove modify date
xmpMetaData . Remove ( Aspose . Pdf . Facades . DefaultMetadataProperties . ModifyDate );
// Add user defined property
// Step #1: register namespace prefix and URI
xmpMetaData . RegisterNamespaceURI ( "customNamespace" , "http:// Www.customNameSpaces.com/ns/" );
// Step #2: add user property with the prefix
xmpMetaData . Add ( "customNamespace:UserPropertyName" , "UserPropertyValue" );
// Change user defined property
xmpMetaData [ "customNamespace:UserPropertyName" ] = "UserPropertyValue2" ;
// Save xmp meta data in the pdf file
xmpMetaData . Save ( dataDir + "SetXMPMetadata_out.pdf" );
}