C#: Expand or Collapse Bookmarks in PDF

In PDF documents, the functionality to expand or collapse bookmarks helps provide a more organized and easier to navigate layout. When bookmarks are expanded, the entire hierarchy of bookmarks is visible at once, giving a comprehensive view of the document's structure and aiding in comprehension of its flow. Conversely, the option to collapse bookmarks is beneficial for users who prefer a less detailed view, as it allows them to focus on a specific part of the document without being distracted by the entire hierarchy. This article will introduce how to expand or collapse bookmarks in PDF in C# 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

Expand or Collapse a Specific Bookmark in PDF in C#

Spire.PDF for .NET provides the PdfBookmark.ExpandBookmark property to expand or collapse a specified bookmark in PDF. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified bookmark in the PDF file using PdfDocument.Bookmarks[] property.
  • Expand the bookmark by setting the PdfBookmark.ExpandBookmark property to true. Or set it to false to collapses the bookmark.
  • Save the result PDF file using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

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

            //Get a specified bookmark
            PdfBookmark bookmarks = pdf.Bookmarks[3];

            //Expand the bookmark
            bookmarks.ExpandBookmark = true;
            //collapse the bookmark
            //bookmarks.ExpandBookmark = false;


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

C#: Expand or Collapse Bookmarks in PDF

Expand or Collapse All Bookmarks in PDF in C#

You can also iterate through all the bookmarks in a PDF file, and then expand or collapse each bookmark through the PdfBookmark.ExpandBookmark property. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Iterate through all bookmarks in the PDF file.
  • Expand each bookmark by setting the PdfBookmark.ExpandBookmark property to true. Or set it to false to collapses each bookmark.
  • Save the result PDF file using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

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

            //Loop through all bookmarks and expand them
            foreach (PdfBookmark bookmark in pdf.Bookmarks)
            {
               bookmark.ExpandBookmark = true;
            }
            //Save the result file
            pdf.SaveToFile("ExpandAllBookmarks.pdf");
        }
    }
}

C#: Expand or Collapse 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.