Work with PDF file metadata in C++

This page covers the four supplied examples for reading file information, registering an XMP namespace prefix, updating standard properties, and adding XMP values.

Use DocumentInfo for fields such as author, title, subject, keywords, and dates. Use Metadata for XMP properties and wrap C++ values in XmpValue.

Configure your project to include and link Aspose.PDF for C++, supply an input PDF, and create output directories before calling the examples. The snippets omit the project-specific pdf-file-metadata.h include. The reading function prints information without saving a file; each writing function accepts its own output path.

Get PDF file information

Read standard document information and print the values without modifying the PDF.

  1. Open the input PDF as a Document.
  2. Access its DocumentInfo through get_Info().
  3. Read Author, CreationDate, Keywords, ModDate, Subject, and Title.
  4. Print the values to the console.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/DocumentInfo.h>

using namespace System;
using namespace Aspose::Pdf;

void GetPdfFileInformation(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto docInfo = document->get_Info();

    std::cout << "Author: " << docInfo->get_Author() << std::endl;
    std::cout << "Creation Date: " << docInfo->get_CreationDate().ToString() << std::endl;
    std::cout << "Keywords: " << docInfo->get_Keywords() << std::endl;
    std::cout << "Modify Date: " << docInfo->get_ModDate().ToString() << std::endl;
    std::cout << "Subject: " << docInfo->get_Subject() << std::endl;
    std::cout << "Title: " << docInfo->get_Title() << std::endl;
}

Set XMP metadata with a namespace prefix

Register the xmp prefix for the Adobe XMP namespace and add a modification timestamp.

  1. Open the input PDF as a Document.
  2. Access Metadata and call RegisterNamespaceUri() with the xmp prefix and namespace URI.
  3. Get the current DateTimeOffset and format it with the round-trip o format.
  4. Wrap the formatted timestamp in XmpValue and call Add() for xmp:ModifyDate.
  5. Save the updated PDF with Save().
#include <iostream>
#include <system/date_time_offset.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Metadata.h>
#include <Aspose.PDF.Cpp/Data/XMP/XmpValue.h>

using namespace System;
using namespace Aspose::Pdf;

void SetPrefixMetadata(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    document->get_Metadata()->RegisterNamespaceUri(u"xmp", u"http://ns.adobe.com/xap/1.0/");
    document->get_Metadata()->Add(u"xmp:ModifyDate", MakeObject<XmpValue>(DateTimeOffset::get_Now().ToString(u"o")));
    document->Save(outputFile);

    std::cout << "Prefix metadata saved to " << outputFile << std::endl;
}

Set PDF file information

Update the document information fields and save a separate output PDF.

The example assigns the current time to both CreationDate and ModDate, replacing any existing creation date. Adapt those assignments if you need to retain the original date.

  1. Open the input PDF as a Document and access its DocumentInfo.
  2. Get the current time with DateTime::get_Now().
  3. Set Author, CreationDate, Keywords, ModDate, Subject, and Title.
  4. Set Producer and Creator.
  5. Save the PDF with Save() and print the output path.
#include <iostream>
#include <system/date_time.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/DocumentInfo.h>

using namespace System;
using namespace Aspose::Pdf;

void SetFileInformation(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto docInfo = document->get_Info();
    auto now = DateTime::get_Now();

    docInfo->set_Author(u"Aspose");
    docInfo->set_CreationDate(now);
    docInfo->set_Keywords(u"Aspose.Pdf, DOM, API");
    docInfo->set_ModDate(now);
    docInfo->set_Subject(u"PDF Information");
    docInfo->set_Title(u"Setting PDF Document Information");
    docInfo->set_Producer(u"Custom producer");
    docInfo->set_Creator(u"Custom creator");
    document->Save(outputFile);

    std::cout << "File information saved to " << outputFile << std::endl;
}

Set XMP property values

Add the sample creation date, nickname, and custom property to the PDF metadata.

The example retains the supplied xmp:CustomProperty name. Treat it as sample application data rather than a standard XMP property.

  1. Open the input PDF as a Document.
  2. Access Metadata through get_Metadata().
  3. Wrap the current DateTimeOffset round-trip string in XmpValue and add xmp:CreateDate.
  4. Add xmp:Nickname and xmp:CustomProperty using XmpValue strings.
  5. Save the updated PDF with Save() and print the output path.
#include <iostream>
#include <system/date_time_offset.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Metadata.h>
#include <Aspose.PDF.Cpp/Data/XMP/XmpValue.h>

using namespace System;
using namespace Aspose::Pdf;

void SetXmpMetadata(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    document->get_Metadata()->Add(u"xmp:CreateDate", MakeObject<XmpValue>(DateTimeOffset::get_Now().ToString(u"o")));
    document->get_Metadata()->Add(u"xmp:Nickname", MakeObject<XmpValue>(u"Nickname"));
    document->get_Metadata()->Add(u"xmp:CustomProperty", MakeObject<XmpValue>(u"Custom Value"));
    document->Save(outputFile);

    std::cout << "XMP metadata saved to " << outputFile << std::endl;
}