News Category

Bookmark

Bookmark (6)

By using Acrobat, we can edit the PDF bookmark actions and set the zoom level to "Inherit Zoom". Then no matter which bookmark you click, the PDF page will stay the same size as the previous page you are viewing and it won't be changed. Spire.PDF also enables developers to set the Bookmark actions to inherit zoom by setting PdfDestination.Zoom as 0. This article will show you how to update bookmarks in a PDF document in C#.

Firstly, view the original screenshot of the PDF bookmark property:

How to set inherit zoom property for PDF bookmarks

Here comes to the step of how to use Spire.PDF to set the PDF bookmark actions.

Step 1: Create a new PDF document and load the document from file.

PdfDocument pdfdoc = new PdfDocument();
pdfdoc.LoadFromFile("TheGreatGatsby.pdf");

Step 2: Get bookmarks collections of the PDF file.

PdfBookmarkCollection bookmarks = pdfdoc.Bookmarks;

Step 3: Set Zoom level as 0, which the value is inherit zoom.

foreach (PdfBookmark bookMark in bookmarks)
 {
   //value 1 is the actual size, other value is the customized size.
   bookMark.Destination.Zoom =0;                              
 }

Step 4: Save the document to file.

pdfdoc.SaveToFile("result.pdf");

Effective screenshot after setting the zoom level to Inherit zoom.

How to set inherit zoom property for PDF bookmarks

Full codes:

using Spire.Pdf;
using Spire.Pdf.Bookmarks;


namespace SetInheritZoomProperty
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument pdfdoc = new PdfDocument();
            pdfdoc.LoadFromFile("TheGreatGatsby.pdf");

            PdfBookmarkCollection bookmarks = pdfdoc.Bookmarks;

            foreach (PdfBookmark bookMark in bookmarks)
            {
                bookMark.Destination.Zoom = 0;

            }

            pdfdoc.SaveToFile("result.pdf");
        }
    }
}

In order to distinguish nested bookmarks more clearly, you can set different font style and font color for parent and child bookmarks respectively. In this article, I’ll introduce how to modify bookmarks in the terms of font style, font color and text using Spire.PDF in C#, VB.NET.

Main Steps:

Step 1: Initialize a new object of PdfDocument and load the existing PDF file.

PdfDocument doc = new PdfDocument("Bookmark.Pdf");

Step 2: Get all bookmarks from the test file.

PdfBookmarkCollection bookmarks = doc.Bookmarks;

Step 3: Traverse very first-level bookmark in the bookmark tree. Change the properties of selected or all bookmarks, including title, color and display style.

foreach (PdfBookmark parentBookmark in bookmarks)
{
    //change "Word Bookmarks" to "M BookMark"
    bookmarks[0].Title = "Modified BookMarks";      
    //set the color of the bookmark
    parentBookmark.Color = Color.Black;
    parentBookmark.DisplayStyle = PdfTextStyle.Bold;
    //edit child bookmark of parent bookmark
    EditChildBookmark(parentBookmark);
}

Step 4: In step 3, there is a nested method predefined as below. By invoking this method, we are able to modify child bookmarks of the upper level bookmark. In this example, we have two nested level bookmarks under the first-level bookmark.

static void EditChildBookmark(PdfBookmark parentBookmark)
{
   foreach (PdfBookmark childBookmark in parentBookmark)
   {
        childBookmark.Color = Color.Brown;
        childBookmark.DisplayStyle = PdfTextStyle.Regular;
        EditChild2Bookmark(childBookmark);
    }
}
static void EditChild2Bookmark(PdfBookmark childBookmark)
{
    foreach (PdfBookmark child2Bookmark in childBookmark)
    {           
        child2Bookmark.Color = Color.LightSalmon;
        child2Bookmark.DisplayStyle = PdfTextStyle.Italic; 
    }
}

Step 5: Save and launch the file.

doc.SaveToFile("Result.Pdf");
System.Diagnostics.Process.Start("Result.Pdf");

Screenshot of Effect:

How to Modify Bookmarks in Existing PDF in C#, VB.NET

Entire Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
using System.Drawing;


namespace ModifyBookmarks
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument("Bookmark.Pdf");
            PdfBookmarkCollection bookmarks = doc.Bookmarks;
            foreach (PdfBookmark parentBookmark in bookmarks)
            {
                //change "Word Bookmarks" to "M BookMark"
                bookmarks[0].Title = "Modified BookMarks";
                //set the color of the bookmark
                parentBookmark.Color = Color.Black;
                parentBookmark.DisplayStyle = PdfTextStyle.Bold;
                //edit child bookmark of parent bookmark
                EditChildBookmark(parentBookmark);
            }
            doc.SaveToFile("Result.Pdf");
            System.Diagnostics.Process.Start("Result.Pdf");
        }
        static void EditChildBookmark(PdfBookmark parentBookmark)
        {
            foreach (PdfBookmark childBookmark in parentBookmark)
            {
                childBookmark.Color = Color.Brown;
                childBookmark.DisplayStyle = PdfTextStyle.Regular;
                EditChild2Bookmark(childBookmark);
            }
        }
        static void EditChild2Bookmark(PdfBookmark childBookmark)
        {
            foreach (PdfBookmark child2Bookmark in childBookmark)
            {
                child2Bookmark.Color = Color.LightSalmon;
                child2Bookmark.DisplayStyle = PdfTextStyle.Italic;
            }
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Bookmarks
Imports System.Drawing


Namespace ModifyBookmarks
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument("Bookmark.Pdf")
			Dim bookmarks As PdfBookmarkCollection = doc.Bookmarks
			For Each parentBookmark As PdfBookmark In bookmarks
				'change "Word Bookmarks" to "M BookMark"
				bookmarks(0).Title = "Modified BookMarks"
				'set the color of the bookmark
				parentBookmark.Color = Color.Black
				parentBookmark.DisplayStyle = PdfTextStyle.Bold
				'edit child bookmark of parent bookmark
				EditChildBookmark(parentBookmark)
			Next
			doc.SaveToFile("Result.Pdf")
			System.Diagnostics.Process.Start("Result.Pdf")
		End Sub
		Private Shared Sub EditChildBookmark(parentBookmark As PdfBookmark)
			For Each childBookmark As PdfBookmark In parentBookmark
				childBookmark.Color = Color.Brown
				childBookmark.DisplayStyle = PdfTextStyle.Regular
				EditChild2Bookmark(childBookmark)
			Next
		End Sub
		Private Shared Sub EditChild2Bookmark(childBookmark As PdfBookmark)
			For Each child2Bookmark As PdfBookmark In childBookmark
				child2Bookmark.Color = Color.LightSalmon
				child2Bookmark.DisplayStyle = PdfTextStyle.Italic
			Next
		End Sub
	End Class
End Namespace

PDF bookmarks are very useful to help us locate and link to points that we want to go, especially when the PDF has huge pages. Sometimes we may need to collapse and expand the bookmarks to make them clear. With the help of Spire.PDF for .NET, developers can easily add bookmarks to the PDF file, get the bookmarks in the PDF and even remove the bookmarks we want. This article will show you how to expand and collapse the bookmarks in an existing PDF file in C#.

Please note that the feature of expand and collapse bookmarks only supports in Spire.PDF for .NET V3.2.31 or above. Here comes to the codes:

Step 1: Create a new PDF document and load from the file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Bookmark.pdf");

Step 2: Set BookMarkExpandOrCollapse as false to collapse the bookmarks.

doc.ViewerPreferences.BookMarkExpandOrCollapse = false; //expand is true

Step 3: Save the document to file.

doc.SaveToFile("result.pdf");

Effective screenshot of the collapse the bookmarks in PDF:

Expand/Collapse bookmarks in PDF files in C#

The screenshot of Expand the bookmarks in PDF:

Expand/Collapse bookmarks in PDF files in C#

Full codes:

using Spire.Pdf;

namespace ExpandAndCollapsePDFBookmarks
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Bookmark.pdf");
            doc.ViewerPreferences.BookMarkExpandOrCollapse = false;//expand is true
            doc.SaveToFile("result.pdf");
        }
    }
}

PDF Bookmarks is widely used to help us locate and link to points that we want to go. Spire.PDF for .NET, a powerful PDF component, not only enables developers to add bookmarks, get bookmark information, but also helps us to remove the bookmarks that we want. This article will show you how to delete bookmark from the PDF document in C# easily.

First, check the PDF document with bookmarks.

Bookmark

Here comes to the steps of how to remove the bookmarks in C#.

  • Download Spire.PDF for .NET and install it correctly. The Spire.PDF installation is clean, professional and wrapped up in a MSI installer.
  • Add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
  • Check the code snippet of how to delete the bookmarks. With Spire.PDF.NET, we can remove both the particular bookmark and remove all the bookmarks at one time.
using Spire.Pdf;

namespace DeletePDFBookmark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PDF document and load from the file.
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"D:\Bookmark.pdf");

            //remove the first bookmark
            document.Bookmarks.RemoveAt(0);

            //remove all bookmarks
            document.Bookmarks.Clear();

            //save the document to file.
            document.SaveToFile("result.pdf");
        }
    }
}

Please check the effective screenshots:

Remove particular bookmark

Remove all Bookmarks

Bookmark is a helpful feature of PDF, especially for PDF documents with lots of pages. By clicking on a bookmark, the reader can jump to the corresponding location of the document quickly. A series of organized bookmarks can be used as contents as well. This article demonstrates how to get bookmarks from PDF documents 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

Get the Bookmarks of a PDF Document

The detailed steps are as follows:

  • Create a PdfDocument instance.
  • Load a PDF document from disk using PdfDocument.LoadFromFile() method.
  • Get bookmarks collection in the PDF document using PdfDocument.Bookmarks property.
  • Get the bookmarks’ content and save to a TXT file using custom method GetBookmarks().
  • C#
  • VB.NET
using System;
using System.IO;
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

            //Load a PDF document from disk
            pdf.LoadFromFile(@"D:\testp\test.pdf");

            //Get bookmark collection of the Pdf document
            PdfBookmarkCollection bookmarks = pdf.Bookmarks;

            //Get the bookmarks and save them to a TXT file
            String result = "GetPdfBookmarks.txt";
            GetBookmarks(bookmarks, result);

        }

        public static void GetBookmarks(PdfBookmarkCollection bookmarks, string result)
        {
            //Create an object of StringBuilder
            StringBuilder content = new StringBuilder();

            //Get PDF bookmarks’ information
            if (bookmarks.Count > 0)
            {
                content.AppendLine("Pdf bookmarks:");
                foreach (PdfBookmark parentBookmark in bookmarks)
                {
                    //Get the title
                    content.AppendLine(parentBookmark.Title);

                    //Get the text style
                    string textStyle = parentBookmark.DisplayStyle.ToString();
                    content.AppendLine(textStyle);
                    GetChildBookmark(parentBookmark, content);
                }
            }

            //Save to a TXT file
            File.WriteAllText(result, content.ToString());
        }
        public static void GetChildBookmark(PdfBookmark parentBookmark, StringBuilder content)
        {
            if (parentBookmark.Count > 0)
            {
                foreach (PdfBookmark childBookmark in parentBookmark)
                {
                    //Get the title
                    content.AppendLine(childBookmark.Title);

                    //Get the text style
                    string textStyle = childBookmark.DisplayStyle.ToString();
                    content.AppendLine(textStyle);
                    GetChildBookmark(childBookmark, content);
                }
            }

        }
    }
}

C#/VB.NET: Get Bookmarks of PDF Documents

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.

PDF bookmarks are navigational aids that allow users to quickly locate and jump to specific sections or pages in a PDF document. Through a simple click, users can arrive at the target location, which eliminates the need to manually scroll or search for specific content in a lengthy document. In this article, you will learn how to programmatically add, modify and remove bookmarks in PDF 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 

Add Bookmarks to a PDF Document in C# and VB.NET

Spire.PDF for .NET provides the PdfDocument.Bookmarks.Add() method to add bookmarks to a PDF document. After a bookmark is created, you can also use the PdfBookmark.Add() method to add sub-bookmarks for it.  The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all pages in the PDF file to add bookmarks and set their styles.
  • Add a parent bookmark to the document using PdfDocument.Bookmarks.Add() method.
  • Create a PdfDestination object and set the destination of the parent bookmark using PdfBookmark.Action property.
  • Set the text color and style of the parent bookmark.
  • Add a sub-bookmark to the parent bookmark using PdfBookmark.Add() method.
  • Set the destination, text color, and text style of the sub-bookmark.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Actions;
using Spire.Pdf.Bookmarks;
using Spire.Pdf.General;

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

            //Load a sample PDF file
            pdf.LoadFromFile("template.pdf");

            //Loop through the pages in the PDF file
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                PdfPageBase page = pdf.Pages[i];

                //Add a bookmark
                PdfBookmark bookmark = pdf.Bookmarks.Add(string.Format("Bookmark-{0}", i + 1));

                //Set the destination page and location of the bookmark
                PdfDestination destination = new PdfDestination(page, new PointF(0, 0));
                bookmark.Action = new PdfGoToAction(destination);

                //Set the text color and style of the bookmark
                bookmark.Color = new PdfRGBColor(Color.Black);
                bookmark.DisplayStyle = PdfTextStyle.Bold;

                //Add a child bookmark
                PdfBookmark childBookmark = bookmark.Add(string.Format("Sub-Bookmark-{0}", i + 1));

                //Set the destination page and location of the child bookmark
                PdfDestination childDestination = new PdfDestination(page, new PointF(0, 100));
                childBookmark.Action = new PdfGoToAction(childDestination);

                //Set the text color and style of the child bookmark
                childBookmark.Color = new PdfRGBColor(Color.Brown);
                childBookmark.DisplayStyle = PdfTextStyle.Italic;
            }

            // Save the result file
            pdf.SaveToFile("AddBookmarks.pdf");
        }
    }
}

C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

Edit Bookmarks in a PDF Document in C# and VB.NET

If you need to update the existing bookmarks, you can use the methods of PdfBookmark class to rename the bookmarks and change their text color, text style. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified bookmark using PdfDocument.Bookmarks[] property.
  • Change the title of the bookmark using PdfBookmark.Title property.
  • Change the font color of the bookmark using PdfBookmark.Color property.
  • Change the text style of the bookmark using PdfBookmark.DisplayStyle property.
  • Change the text color and style of the sub-bookmark using the above methods.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
using System.Drawing;


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

            //Load a sample PDF file
            pdf.LoadFromFile("AddBookmarks.Pdf");

            //Get the first bookmark
            PdfBookmark bookmark = pdf.Bookmarks[0];
            
            //Change the title of the bookmark
            bookmark.Title = "Modified BookMark";

            //Change text color and style of the bookmark
            bookmark.Color = Color.Red;
            bookmark.DisplayStyle = PdfTextStyle.Italic;

            //Edit sub-bookmarks of the first bookmark
            foreach (PdfBookmark childBookmark in bookmark)
            {
                childBookmark.Color = Color.Blue;
                childBookmark.DisplayStyle = PdfTextStyle.Bold;
            }
        
            //Save the result file
            pdf.SaveToFile("EditBookmarks.Pdf");
        }
    }
}

C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

Delete Bookmarks from a PDF Document in C# and VB.NET

Spire.PDF for .NET allows to remove a specified bookmark as well as all bookmarks in a PDF file. Furthermore, removing only a specified sub-bookmark can also be achieved. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Get the first bookmark using PdfDocument.Bookmarks[] property.
  • Remove a specified sub-bookmark of the first bookmark using PdfBookmark.RemoveAt() method.
  • Remove a specified bookmark including its sub-bookmarks using PdfDocument.Bookmarks.RemoveAt() method. Or you can remove all bookmarks in the PDF file using PdfDocument.Bookmarks.Clear() method.
  • Save the document using PdfDocument.saveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

            //Load a sample PDF file
            pdf.LoadFromFile("AddBookmarks.Pdf");

            //Get the first bookmark
            PdfBookmark bookmark = pdf.Bookmarks[0];

            //Delete the first sub-bookmark of the first bookmark
            bookmark.RemoveAt(0);

            //Delete the second bookmark including its sub-bookmarks
            pdf.Bookmarks.RemoveAt(1);

            //Delete all bookmarks
            //pdf.Bookmarks.Clear();

            //Save the result file
            pdf.SaveToFile("DeleteBookmarks.pdf");
        }
    }
}

C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

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.