Read and write PDF properties in C# and VB.NET

With GemBox.Pdf, you can read and write document properties (document information) from PDF files in your C# or VB.NET application. The following example shows how you can set the Document Information Dictionary using the PdfDocument.Info properties of an existing PDF document.

Document properties created in PDF file with C# / VB.NET
Screenshot of document properties in a PDF file
Upload your file (Drag file here)
using System;
using GemBox.Pdf;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            // Get document properties.
            var info = document.Info;

            // Update document properties.
            info.Title = "My Title";
            info.Author = "My Author";
            info.Subject = "My Subject";
            info.Creator = "My Application";

            // Update producer and date information, and disable their overriding.
            info.Producer = "My Producer";
            info.CreationDate = new DateTime(2023, 1, 1, 12, 0, 0);
            info.ModificationDate = new DateTime(2023, 1, 1, 12, 0, 0);
            document.SaveOptions.UpdateProducerInformation = false;
            document.SaveOptions.UpdateDateInformation = false;

            document.Save("Document Properties.pdf");
        }
    }
}
Imports System
Imports GemBox.Pdf

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")

            ' Get document properties.
            Dim info = document.Info

            ' Update document properties.
            info.Title = "My Title"
            info.Author = "My Author"
            info.Subject = "My Subject"
            info.Creator = "My Application"

            ' Update producer and date information, and disable their overriding.
            info.Producer = "My Producer"
            info.CreationDate = New DateTime(2023, 1, 1, 12, 0, 0)
            info.ModificationDate = New DateTime(2023, 1, 1, 12, 0, 0)
            document.SaveOptions.UpdateProducerInformation = False
            document.SaveOptions.UpdateDateInformation = False

            document.Save("Document Properties.pdf")
        End Using
    End Sub
End Module

Custom Properties

Besides the properties available in PdfDocumentInformation, you can also add custom properties. The following example shows how you can add or update custom properties in a PDF file.

Custom properties created in PDF file with C# / VB.NET
Screenshot of custom properties in a PDF file
Upload your file (Drag file here)
using GemBox.Pdf;
using GemBox.Pdf.Objects;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            // Get document properties dictionary.
            var infoDictionary = document.Info.GetOrAddDictionary();

            // Create or update custom properties.
            infoDictionary[PdfName.Create("Custom Name 1")] = PdfString.Create("My Value 1");
            infoDictionary[PdfName.Create("Custom Name 2")] = PdfString.Create("My Value 2");

            document.Save("Custom Properties.pdf");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Objects

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")

            ' Get document properties dictionary.
            Dim infoDictionary = document.Info.GetOrAddDictionary()

            ' Create or update custom properties.
            infoDictionary(PdfName.Create("Custom Name 1")) = PdfString.Create("My Value 1")
            infoDictionary(PdfName.Create("Custom Name 2")) = PdfString.Create("My Value 2")

            document.Save("Custom Properties.pdf")
        End Using
    End Sub
End Module

XMP Metadata

Adobe's Extensible Metadata Platform (XMP) is an XML-based labeling technology that allows you to specify general information about the document. With GemBox.Pdf, you can access that information using the PdfDocument.Metadata property.

Besides the document itself, any individual document component (e.g., PdfObject) may specify its metadata information. For example, PdfPage.Metadata, PdfImage.Metadata, and PdfOutline.Metadata.

The following example shows how to create XMP metadata in a PDF file.

Upload your file (Drag file here)
using System;
using System.Xml.Linq;
using GemBox.Pdf;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            var metadata = document.Metadata;

            var xmp = XNamespace.Get("http://ns.adobe.com/xap/1.0/");
            var dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");
            var rdf = XNamespace.Get("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            var xml = XNamespace.Xml;

            metadata.Add(new XElement(xmp + "CreatorTool", "GemBox.Pdf for .NET"));
            metadata.Add(new XElement(xmp + "CreateDate", DateTime.Now));

            // Define the document title in multiple languages.
            metadata.Add(new XElement(dc + "title",
                new XElement(rdf + "Alt",
                    new XElement(rdf + "li", new XAttribute(xml + "lang", "x-default"), "My Title"),
                    new XElement(rdf + "li", new XAttribute(xml + "lang", "en"), "My Title"),
                    new XElement(rdf + "li", new XAttribute(xml + "lang", "es"), "Mi Título"),
                    new XElement(rdf + "li", new XAttribute(xml + "lang", "fr"), "Mon Titre"))));

            document.Save("XMP Metadata.pdf");
        }
    }
}
Imports System
Imports System.Xml.Linq
Imports GemBox.Pdf

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")

            Dim metadata = document.Metadata

            Dim xmp = XNamespace.Get("http://ns.adobe.com/xap/1.0/")
            Dim dc = XNamespace.Get("http://purl.org/dc/elements/1.1/")
            Dim rdf = XNamespace.Get("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
            Dim xml = XNamespace.Xml

            metadata.Add(New XElement(xmp + "CreatorTool", "GemBox.Pdf for .NET"))
            metadata.Add(New XElement(xmp + "CreateDate", DateTime.Now))

            ' Define the document title in multiple languages.
            metadata.Add(New XElement(dc + "title",
                New XElement(rdf + "Alt",
                    New XElement(rdf + "li", New XAttribute(xml + "lang", "x-default"), "My Title"),
                    New XElement(rdf + "li", New XAttribute(xml + "lang", "en"), "My Title"),
                    New XElement(rdf + "li", New XAttribute(xml + "lang", "es"), "Mi Título"),
                    New XElement(rdf + "li", New XAttribute(xml + "lang", "fr"), "Mon Titre"))))

            document.Save("XMP Metadata.pdf")
        End Using
    End Sub
End Module

Note that if the same information is defined with Document Information Dictionary and XMP metadata, only the metadata information will be displayed in Adobe's Document Properties dialog.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy