C# code tutorial

PDF Creating & Inserting PDF to Image PDF Digital Signature PDF Extracting PDF Processing & Editing PDF Protecting PDF Chart PDF Barcode

Popular .NET SDK


How to create structured bookmarks in Visual Studio C# programming. Easy to add, delete, update outlines in PDF document.

PDF bookmarks are a handy and necessary tool when it comes to reading PDF files. It's interactive index that can instantly transport the reader to certain pages, sections, individual elements within the documents, and even external documents or media files.

Bookmarks allow users to quickly skip to other locations in the PDF, to navigate through the pages, and to get a quick glance at the PDF content similar as the quick jump to the webpage desired section.

.NET PDF bookmark creator library allows developers to insert and add outline to PDF document. For further needs, you can merge and split PDF with bookmarks. Here, we will introduce you how to create PDF pages as root bookmarks & sub bookmarks, and then add bookmark to PDF using C# code.

How to Create Root and Sub Point of Bookmarks in C#

Use C# code below to create a new page as root bookmark, and use "Opened" property to make it expand or collapse.

//Create new PDF document
PdfDocument document = new PdfDocument();

//Creaet the first root page
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var text = "Root 1";
var textFont = new XFont("Times New Roman", 30f, XFontStyle.Regular);
var location = new XPoint(50, 100);
gfx.DrawString(text, textFont, XBrushes.Black, location);

// Create the root 1 bookmark 
var outline = document.Outlines.Add(text, page);
outline.Opened = true;

Use C# code below to create some pages as sub bookmarks.

// Create some sub pages for root1
for (var i = 1; i <= 3; i++)
{
    page = document.AddPage();
    gfx = XGraphics.FromPdfPage(page);
    text = "Sub Page " + i;
    gfx.DrawString(text, textFont, XBrushes.Black, location);

    // Create a sub bookmark.
    outline.Outlines.Add(text, page, true);
}
Complete C# Code for Adding a PDF Bookmark

Please directly copy the complete C# code below to quickly create and insert a bookmark into PDF file.

//Create new PDF document
PdfDocument document = new PdfDocument();

//Creaet the first root page
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var text = "Root 1";
var textFont = new XFont("Times New Roman", 30f, XFontStyle.Regular);
var location = new XPoint(50, 100);
gfx.DrawString(text, textFont, XBrushes.Black, location);

// Create the root 1 bookmark 
var outline = document.Outlines.Add(text, page);
outline.Opened = true;

// Create some sub pages for root1
for (var i = 1; i <= 3; i++)
{
    page = document.AddPage();
    gfx = XGraphics.FromPdfPage(page);
    text = "Sub Page " + i;
    gfx.DrawString(text, textFont, XBrushes.Black, location);

    // Create a sub bookmark.
    outline.Outlines.Add(text, page, true);
}

//Creaet the second root page
page = document.AddPage();
gfx = XGraphics.FromPdfPage(page);
text = "Root 2";
gfx.DrawString(text, textFont, XBrushes.Black, location);

// Create the root 2 bookmark with bold style.
outline = document.Outlines.Add(text, page, false, PdfOutlineStyle.Bold);

// Create some sub pages for root2
for (var i = 1; i <= 3; i++)
{
    page = document.AddPage();
    gfx = XGraphics.FromPdfPage(page);
    text = "Sub Page " + i;
    gfx.DrawString(text, textFont, XBrushes.Black, location);

    // Create a sub bookmark.
    outline.Outlines.Add(text, page, false);
}

document.Save("bookmark.pdf");

More Excel tutorial