Modifying Outlines and Bookmarks

A PDF's outline and bookmarks are available when merging or appending PDF documents. You can read the existing outlines and bookmarks as you merge existing PDFs and create or modify outlines and bookmarks to the merged document to be outputted.

Refer to the topic, Outlines and Bookmarks, for more information on creating outlines and bookmarks.

Adding Outline

The following is an example of adding an outline to a PDF created using the MergeDocument child class of Document. Note that it contains both code relevant to PdfOutline and Outline. The code first obtains the PdfOutline instance from the PdfDocument and adds it to the MergeDocument instance. The MergeDocument instance then creates a new Outline and adds it to the existing outline. The result is a new PDF with both outlines.

A PdfOutline instance and is read-only. To add or modify an outline you must load the PdfOutline instance into a MergeDocument instance.

PdfDocument pdfDocument = new PdfDocument("outline-example.pdf");
PdfOutline pdfOutline = pdfDocument.Outlines[0];
MergeDocument document = new MergeDocument(pdfDocument);
Outline outline1 = document.Outlines.Add("ChildA");
Outline outline1A = outline1.ChildOutlines.Add("ChildA 1", new XYDestination(2, 0, 0));
document.Draw(outputPath);
Dim pdfDocument As New PdfDocument("outline-example.pdf")
Dim pdfOutline As PdfOutline = pdfDocument.Outlines(0)
Dim document As New MergeDocument(pdfDocument)
Dim outline1 As Outline = document.Outlines.Add("ChildA")
Dim outline1A As Outline = outline1.ChildOutlines.Add("ChildA 1", New XYDestination(2, 0, 0))
document.Draw(outputPath)

adding to outline Figure 1. Modifying an outline.

Adding Bookmarks

Adding bookmarks to an existing PDF page using the PdfDocument and MergeDocument classes along with the Page class.

The following two examples illustrate adding bookmarks. The first example adds a bookmark to a new page while the second adds a bookmark to an existing page.

Adding to new page

PdfDocument pdfDocument = new PdfDocument("bookmark-example.pdf");
MergeDocument document = new MergeDocument(pdfDocument);
Page addedPage = new Page(PageSize.Letter);
document.Pages.Add(addedPage);
addedPage.Elements.Add(new Bookmark("Added Bookmark", 0, 0));
document.Draw(outputPath);
Dim pdfDocument As New PdfDocument("bookmark-example.pdf")
Dim document As New MergeDocument(pdfDocument)
Dim addedPage As New Page(PageSize.Letter)
document.Pages.Add(addedPage)
addedPage.Elements.Add(New Bookmark("Added Bookmark", 0, 0))
document.Draw(outputPath)

Add to existing page

PdfDocument pdfDocument = new PdfDocument("bookmark-example.pdf");
MergeDocument document = new MergeDocument(pdfDocument);
Page page = document.Pages[3];
page.Elements.Add(new Bookmark("Added Bookmark", 0, 0));
document.Draw(outputPath);
Dim pdfDocument As New PdfDocument("bookmark-example.pdf")
Dim document As New MergeDocument(pdfDocument)
Dim page As Page = document.Pages(3)
page.Elements.Add(New Bookmark("Added Bookmark", 0, 0))
document.Draw(outputPath)

Replacing/Remove Outline

If you wish to remove an outline from a PDF document, then you must replace the outline. There are no methods to remove outlines or child outlines. Instead, obtain the original outline from the PDF document. Specify to not retain outlines when creating the MergeDocument instance. Then build a new outline using the old outline loaded into a PdfOutline instance.

Recall that PdfOutline is read only and can only be read, not modified.

The follow example illustrates replacing an outline.

PdfDocument pdfDocument = new PdfDocument("outline-example.pdf");
PdfOutline pdfOutline = pdfDocument.Outlines[0];
MergeOptions options = new MergeOptions();
options.Outlines = false;
MergeDocument document = new MergeDocument(pdfDocument, options);
Outline root = document.Outlines.Add("Replaced-Outline1");
Outline outline1A = root.ChildOutlines.Add("Replaced ChildA 1", new XYDestination(2, 0, 0));
document.Draw(outputPath);
Dim pdfDocument As New PdfDocument("outline-example.pdf")
Dim pdfOutline As PdfOutline = pdfDocument.Outlines(0)
Dim options As New MergeOptions()
options.Outlines = False
Dim document As New MergeDocument(pdfDocument, options)
Dim root As Outline = document.Outlines.Add("Replaced-Outline1")
Dim outline1A As Outline = root.ChildOutlines.Add("Replaced ChildA 1", New XYDestination(2, 0, 0))
document.Draw(outputPath)

adding to outline Figure 2. Replacing an outline.

Modifying Example

You can rename, remove, add, and insert outlines by modifying existing outlines when merging. The following example gets two existing PDFs where outline-b.pdf has an existing outline and merges the documents together and modifies the outline.

PdfDocument docA = new("outline-a.pdf");
PdfDocument docB = new("outline-b.pdf");

//preserve the Bookmarks outline and children
PdfOutline pdfOutB = docB.Outlines[1];

MergeDocument document = new();

//do not preserve outlines
document.Append(docA, MergeOptions.None);
document.Append(docB, MergeOptions.None);

//create new root outline
Outline root = document.Outlines.Add("Merged Document");

//add a child outline as url link
root.ChildOutlines.Add("DynamicPDF Website", new UrlAction("https://www.dynamicpdf.com/"));

//create page number outlines that navigate to page
for (int i = 1; i <= document.Pages.Count; i++)
{
    root.ChildOutlines.Add("Page " + i, new XYDestination(i, 0, 0));
}

//add the title for the bookmarks from outline-B
document.Outlines.Add(pdfOutB.Text);

//loop through bookmarks and assign a new name and target
for(int i = 0; i < pdfOutB.ChildOutlines.Count; i++)
{
    int targetPage = docA.Pages.Count + pdfOutB.ChildOutlines[i].TargetPageNumber;
    document.Outlines[1].ChildOutlines.Add("Bookmark Page " + targetPage, new XYDestination(targetPage, 0,0));    
}
document.Draw(outputPath);
Public Shared Sub ModifyOutline()
    Dim docA As New PdfDocument("outline-a.pdf")
    Dim docB As New PdfDocument("outline-b.pdf")
    Dim pdfOutB As PdfOutline = docB.Outlines(1)
    Dim document As New MergeDocument()
    document.Append(docA, MergeOptions.None)
    document.Append(docB, MergeOptions.None)
    Dim root As Outline = document.Outlines.Add("Merged Document")
    root.ChildOutlines.Add("DynamicPDF Website", New UrlAction("https://www.dynamicpdf.com/"))

    For i As Integer = 1 To document.Pages.Count
        root.ChildOutlines.Add("Page " & i, New XYDestination(i, 0, 0))
    Next

    document.Outlines.Add(pdfOutB.Text)

    For i As Integer = 0 To pdfOutB.ChildOutlines.Count - 1
        Dim targetPage As Integer = docA.Pages.Count + pdfOutB.ChildOutlines(i).TargetPageNumber
        document.Outlines(1).ChildOutlines.Add("Bookmark Page " & targetPage, New XYDestination(targetPage, 0, 0))
    Next

    document.Draw(outputPath)
End Sub

adding to outline Figure 3. Modifying an outline.

In this topic