C#/VB.NET: Set or Get PDF Properties

PDF properties are metadata that provide additional information about a PDF file. Typically, these properties include, but are not limited to, the title of the document, the author, keywords, subject and the application that created the document. When there are a large number of PDF files, adding properties is essential as it can make the files easily retrievable. In this article, you will learn how to programmatically set or get PDF properties using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Set the Properties of a PDF File in C# and VB.NET

Basic PDF document properties such as title, author, subject and keywords make it easier for users to search or retrieve specific documents later on. The following are the detailed steps on how to set these properties using Spire.PDF for .NET.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get PDF properties using PdfDocument.DocumentInformation property, and then set values for specific document properties such as title, subject and author through Title, Subject and Author properties of PdfDocumentInformation class.
  • Save the result PDF file using PdfDocument.SaveToFile () method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PDFProperties
{
    class Properties
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF document
            pdf.LoadFromFile("input.pdf");

            //Set the title
            pdf.DocumentInformation.Title = "PDF (Portable Document Format)";

            //Set the author
            pdf.DocumentInformation.Author = "E-iceblue";

            //Set the subject
            pdf.DocumentInformation.Subject = "Set PDF Properties";

            //Set the keywords
            pdf.DocumentInformation.Keywords = "NET PDF, Properties, Document";

            //Set the producer name
            pdf.DocumentInformation.Producer = "Spire.PDF";

            //Save the result document
            pdf.SaveToFile("PdfProperties.pdf");
        }
    }
}

C#/VB.NET: Set or Get PDF Properties

Get the Properties of a PDF File in C# and VB.NET

To get specific PDF properties, you can use the corresponding properties under the PdfDocumentInformation class. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a StringBuilder instance.
  • Get PDF properties using PdfDocument.DocumentInformation property, and then get specific document properties such as title, author, keyword using properties under PdfDocumentInformation class.
  • Append the extracted properties to the StringBuilder instance using StringBuilder.Append() method.
  • Write the StringBuilder to a TXT file using File.WriteAllText() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.IO;
using System.Text;

namespace GetPdfProperties

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF document 
            pdf.LoadFromFile("PdfProperties.pdf");

            //Create a StringBuilder instance
            StringBuilder content = new StringBuilder();

            //Get the PDF document properties and append them in the StringBuilder
            content.Append("Title: " + pdf.DocumentInformation.Title + "\r\n");
            content.Append("Author: " + pdf.DocumentInformation.Author + "\r\n");
            content.Append("Subject: " + pdf.DocumentInformation.Subject + "\r\n");
            content.Append("Keywords: " + pdf.DocumentInformation.Keywords + "\r\n");
            content.Append("PDF Producer: " + pdf.DocumentInformation.Producer + "\r\n");

            //Write the StringBuilder to a TXT file
            File.WriteAllText("GetPDFProperties.txt", content.ToString());
        }
    }
} 

C#/VB.NET: Set or Get PDF Properties

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.