Manage PDF XMP metadata in C++
Contents
[
Hide
]
Use PdfXmpMetadata to add, update, remove, and read XMP properties and register a custom metadata namespace.
The read example expects the standard and custom keys written by AddXmpMetadata. Its outputFile is a text report rather than a modified PDF.
The snippets are independent example functions with their SDK includes and example namespaces. Configure the Aspose.PDF for C++ SDK, supply the input resources referenced by the sample, and choose writable output paths. The examples retain the source field names, page indexes, coordinates, and options.
PdfXmpMetadata workflows
Add XMP metadata
Set standard timestamps and creator tool, remove ModifyDate, and register and update a custom property.
- Create PdfXmpMetadata.
- Bind the PDF input with Facade::BindPdf().
- Add the XMP property with PdfXmpMetadata::Add().
- Remove the XMP property with PdfXmpMetadata::Remove().
- Register the custom namespace with PdfXmpMetadata::RegisterNamespaceURI().
- Update the metadata value with PdfXmpMetadata::idx_set().
- Save the resulting PDF with PdfXmpMetadata::
Save().
#include <Aspose.PDF.Cpp/Data/XMP/XmpValue.h>
#include <Aspose.PDF.Cpp/Facades/DefaultMetadataProperties.h>
#include <Aspose.PDF.Cpp/Facades/PdfXmpMetadata.h>
#include <system/date_time_offset.h>
#include <system/string.h>
namespace Facades::Examples::PDF_XmpMetaData
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void AddXmpMetadata(String inputFile, String outputFile)
{
auto xmp = MakeObject<PdfXmpMetadata>();
xmp->BindPdf(inputFile);
auto timestamp = MakeObject<XmpValue>(DateTimeOffset::get_UtcNow().ToString(u"o"));
xmp->Add(DefaultMetadataProperties::CreateDate, timestamp);
xmp->Add(DefaultMetadataProperties::MetadataDate, timestamp);
xmp->Add(DefaultMetadataProperties::CreatorTool, MakeObject<XmpValue>(u"Creator tool name"));
xmp->Remove(DefaultMetadataProperties::ModifyDate);
xmp->RegisterNamespaceURI(u"customNamespace", u"http://www.customNameSpaces.com/ns/");
xmp->Add(u"customNamespace:UserPropertyName", MakeObject<XmpValue>(u"UserPropertyValue"));
xmp->idx_set(u"customNamespace:UserPropertyName", MakeObject<XmpValue>(u"UserPropertyValue2"));
xmp->Save(outputFile);
}
} // namespace Facades::Examples::PDF_XmpMetaData
Get XMP metadata
Read expected standard and custom XMP keys, print them, and write a text report.
- Create PdfXmpMetadata.
- Bind the PDF input with Facade::BindPdf().
- Read the metadata value with PdfXmpMetadata::idx_get().
- Write the collected values to the output text file.
#include <Aspose.PDF.Cpp/Data/XMP/XmpValue.h>
#include <Aspose.PDF.Cpp/Facades/DefaultMetadataProperties.h>
#include <Aspose.PDF.Cpp/Facades/PdfXmpMetadata.h>
#include <iostream>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/string.h>
namespace Facades::Examples::PDF_XmpMetaData
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void GetXmpMetadata(String inputFile, String outputFile)
{
auto xmp = MakeObject<PdfXmpMetadata>();
xmp->BindPdf(inputFile);
String report;
for (auto key : {DefaultMetadataProperties::CreateDate, DefaultMetadataProperties::MetadataDate,
DefaultMetadataProperties::CreatorTool})
report += xmp->idx_get(key)->ToString() + u"\n";
report += xmp->idx_get(u"customNamespace:UserPropertyName")->ToString() + u"\n";
std::cout << report << std::endl;
System::IO::File::WriteAllText(outputFile, report);
}
} // namespace Facades::Examples::PDF_XmpMetaData