Use basic PDF objects for currently unsupported PDF features in C# and VB.NET

Objects are the building blocks of a PDF document. Their primary purpose is to allow the reader applications to control low-level data in a PDF file.

With GemBox.Pdf, you can use basic PDF objects for currently unsupported PDF features in your C# or VB.NET application.

The PDF Objects supported by GemBox.Pdf are:

Additionally, GemBox.Pdf defines one more basic object type: PdfIndirectObject.

The following example shows how you can use a page-piece dictionary to hold private application data.

PDF basic objects created with GemBox.Pdf C#/VB.NET library
Screenshot of basic PDF objects created with GemBox.Pdf library
Upload your file (Drag file here)
using System;
using System.Globalization;
using GemBox.Pdf;
using GemBox.Pdf.Objects;
using GemBox.Pdf.Text;

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

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            // Get document's trailer dictionary.
            var trailer = document.GetDictionary();
            // Get document catalog dictionary from the trailer.
            var catalog = (PdfDictionary)((PdfIndirectObject)trailer[PdfName.Create("Root")]).Value;

            // Either retrieve "PieceInfo" entry value from document catalog or create a page-piece dictionary and set it to document catalog under "PieceInfo" entry.
            PdfDictionary pieceInfo;
            var pieceInfoKey = PdfName.Create("PieceInfo");
            var pieceInfoValue = catalog[pieceInfoKey];
            switch (pieceInfoValue.ObjectType)
            {
                case PdfBasicObjectType.Dictionary:
                    pieceInfo = (PdfDictionary)pieceInfoValue;
                    break;
                case PdfBasicObjectType.IndirectObject:
                    pieceInfo = (PdfDictionary)((PdfIndirectObject)pieceInfoValue).Value;
                    break;
                case PdfBasicObjectType.Null:
                    pieceInfo = PdfDictionary.Create();
                    catalog[pieceInfoKey] = PdfIndirectObject.Create(pieceInfo);
                    break;
                default:
                    throw new InvalidOperationException("PieceInfo entry must be dictionary.");
            }

            // Create page-piece data dictionary for "GemBox.Pdf" conforming product and set it to page-piece dictionary.
            var data = PdfDictionary.Create();
            pieceInfo[PdfName.Create("GemBox.Pdf")] = data;

            // Create a private data dictionary that will hold private data that "GemBox.Pdf" conforming product understands.
            var privateData = PdfDictionary.Create();
            data[PdfName.Create("Data")] = privateData;

            // Set "Title" and "Version" entries to private data.
            privateData[PdfName.Create("Title")] = PdfString.Create(ComponentInfo.Title);
            privateData[PdfName.Create("Version")] = PdfString.Create(ComponentInfo.Version);

            // Specify date of the last modification of "GemBox.Pdf" private data (required by PDF specification).
            data[PdfName.Create("LastModified")] = PdfString.Create(DateTimeOffset.Now);

            document.Save("Basic Objects.pdf");
        }
    }
}
Imports System
Imports System.Globalization
Imports GemBox.Pdf
Imports GemBox.Pdf.Objects
Imports GemBox.Pdf.Text

Module Program

    Sub Main()

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

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

            ' Get document's trailer dictionary.
            Dim trailer = document.GetDictionary()
            ' Get document catalog dictionary from the trailer.
            Dim catalog = CType((CType(trailer(PdfName.Create("Root")), PdfIndirectObject)).Value, PdfDictionary)

            ' Either retrieve "PieceInfo" entry value from document catalog or create a page-piece dictionary and set it to document catalog under "PieceInfo" entry.
            Dim pieceInfo As PdfDictionary
            Dim pieceInfoKey = PdfName.Create("PieceInfo")
            Dim pieceInfoValue = catalog(pieceInfoKey)
            Select Case pieceInfoValue.ObjectType

                Case PdfBasicObjectType.Dictionary
                    pieceInfo = CType(pieceInfoValue, PdfDictionary)

                Case PdfBasicObjectType.IndirectObject
                    pieceInfo = CType((CType(pieceInfoValue, PdfIndirectObject)).Value, PdfDictionary)

                Case PdfBasicObjectType.Null
                    pieceInfo = PdfDictionary.Create()
                    catalog(pieceInfoKey) = PdfIndirectObject.Create(pieceInfo)

                Case Else
                    Throw New InvalidOperationException("PieceInfo entry must be dictionary.")
            End Select

            ' Create page-piece data dictionary for "GemBox.Pdf" conforming product and set it to page-piece dictionary.
            Dim data = PdfDictionary.Create()
            pieceInfo(PdfName.Create("GemBox.Pdf")) = data

            ' Create a private data dictionary that will hold private data that "GemBox.Pdf" conforming product understands.
            Dim privateData = PdfDictionary.Create()
            data(PdfName.Create("Data")) = privateData

            ' Set "Title" and "Version" entries to private data.
            privateData(PdfName.Create("Title")) = PdfString.Create(ComponentInfo.Title)
            privateData(PdfName.Create("Version")) = PdfString.Create(ComponentInfo.Version)

            ' Specify date of the last modification of "GemBox.Pdf" private data (required by PDF specification).
            data(PdfName.Create("LastModified")) = PdfString.Create(DateTimeOffset.Now)

            document.Save("Basic Objects.pdf")
        End Using
    End Sub
End Module

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