GemBox.Pdf is a .NET component that enables developers to read, write and update Portable Document Format (PDF) files from .NET applications in a simple and efficient way.

Create a page tree in C# and VB.NET

With GemBox.Pdf, you can get, create, remove or reorder PDF document pages in your C# or VB.NET application.

The pages of a PDF document are accessed through a structure known as the page tree, which defines the ordering of pages in the document. The tree structure allows PDF consumer applications, using only limited memory, to quickly open a PDF document containing thousands of pages. For more information, see Document Structure help page.

The following example demonstrates how to create a page tree structure from scratch.

Screenshot
PDF Library for C# / VB.NET Screenshot

See the full code below, use Run Example to execute.

using System;
using System.Globalization;
using System.Text;
using GemBox.Pdf;
using GemBox.Pdf.Filters;
using GemBox.Pdf.Objects;
using GemBox.Pdf.Text;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        PdfDocument document = new PdfDocument();

        // Get a page tree root node.
        PdfPages rootNode = document.Pages;
        // Set page rotation for a whole set of pages.
        rootNode.Rotate = 90;

        // Create a left page tree node.
        PdfPages childNode = rootNode.Kids.AddPages();
        // Overwrite a parent tree node rotation value.
        childNode.Rotate = 0;

        // Create an empty page.
        childNode.Kids.AddPage();
        // Create an empty page and set a page media box value.
        childNode.Kids.AddPage().SetMediaBox(0, 0, 200, 400);

        // Create a right page tree node.
        childNode = rootNode.Kids.AddPages();
        // Set a media box value.
        childNode.SetMediaBox(0, 0, 100, 200);

        // Create an empty page.
        childNode.Kids.AddPage();
        // Create an empty page and overwrite a rotation value.
        childNode.Kids.AddPage().Rotate = 0;

        document.Save("Page Tree.pdf");
        document.Close();
    }
}
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports GemBox.Pdf
Imports GemBox.Pdf.Filters
Imports GemBox.Pdf.Objects
Imports GemBox.Pdf.Text

Module Samples

    Sub Main()

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

        Dim document As PdfDocument = New PdfDocument()

        ' Get a page tree root node.
        Dim rootNode As PdfPages = document.Pages
        ' Set page rotation for a whole set of pages.
        rootNode.Rotate = 90

        ' Create a left page tree node.
        Dim childNode As PdfPages = rootNode.Kids.AddPages()
        ' Overwrite a parent tree node rotation value.
        childNode.Rotate = 0

        ' Create an empty page.
        childNode.Kids.AddPage()
        ' Create an empty page And set a page media box value.
        childNode.Kids.AddPage().SetMediaBox(0, 0, 200, 400)

        ' Create a right page tree node.
        childNode = rootNode.Kids.AddPages()
        ' Set a media box value.
        childNode.SetMediaBox(0, 0, 100, 200)

        ' Create an empty page.
        childNode.Kids.AddPage()
        ' Create an empty page And overwrite a rotation value.
        childNode.Kids.AddPage().Rotate = 0

        document.Save("Page Tree.pdf")
        document.Close()

    End Sub

End Module

Check next sample.