Bookmark Operations with C# and .NET

With SautinSoft.Pdf, you can get, create or edit PDF document bookmarks (outlines) in your C# or VB.NET application.

The following example shows how to delete existing SautinSoft.Pdf.Outlines and create new SautinSoft.Pdf.Outlines in a PDF document.

Полный код

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
    /// <summary>
    /// Bookmarks.
    /// </summary>
    /// <remarks>
    /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/bookmarks.php
    /// </remarks>
    static void Main()
    {
        // Before starting this example, please get a free 100-day trial key:
        // https://sautinsoft.com/start-for-free/

        // Apply the key here:
        // PdfDocument.SetLicense("...");

        using (var document = new PdfDocument())
        {
            document.Pages.Add();
            // Remove all bookmarks.
            document.Outlines.Clear();
            // Get the number of pages.
            int numberOfPages = document.Pages.Count;
            for (int i = 0; i < numberOfPages; i += 10)
            {
                // Add a new outline item (bookmark) at the end of the document outline collection.
                var bookmark = document.Outlines.AddLast(string.Format("PAGES {0}-{1}", i + 1, Math.Min(i + 10, numberOfPages)));
                // Set the explicit destination on the new outline item (bookmark).
                bookmark.SetDestination(document.Pages[i], PdfDestinationViewType.FitRectangle, 0, 0, 100, 100);
                for (int j = 0; j < Math.Min(10, numberOfPages - i); j++)
                    // Add a new outline item (bookmark) at the end of parent outline item (bookmark) and set the explicit destination.
                    bookmark.Outlines.AddLast(string.Format("PAGE {0}", i + j + 1)).SetDestination(document.Pages[i + j], PdfDestinationViewType.FitPage);
            }
            document.PageMode = PdfPageMode.UseOutlines;
            document.Save("Bookmarks.pdf");
        }

        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Bookmarks.pdf") { UseShellExecute = true });
    }
}

Download

Option Infer On

Imports System
Imports SautinSoft.Pdf
Imports System.IO

Friend Class Program
	''' <summary>
	''' Bookmarks.
	''' </summary>
	''' <remarks>
	''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/bookmarks.php
	''' </remarks>
	Shared Sub Main()
		' Before starting this example, please get a free 100-day trial key:
		' https://sautinsoft.com/start-for-free/

		' Apply the key here:
		' PdfDocument.SetLicense("...");

		Using document = New PdfDocument()
			document.Pages.Add()
			' Remove all bookmarks.
			document.Outlines.Clear()
			' Get the number of pages.
			Dim numberOfPages As Integer = document.Pages.Count
			Dim i As Integer = 0
			Do While i < numberOfPages
				' Add a new outline item (bookmark) at the end of the document outline collection.
				Dim bookmark = document.Outlines.AddLast(String.Format("PAGES {0}-{1}", i + 1, Math.Min(i + 10, numberOfPages)))
				' Set the explicit destination on the new outline item (bookmark).
				bookmark.SetDestination(document.Pages(i), PdfDestinationViewType.FitRectangle, 0, 0, 100, 100)
				Dim j As Integer = 0
				Do While j < Math.Min(10, numberOfPages - i)
					' Add a new outline item (bookmark) at the end of parent outline item (bookmark) and set the explicit destination.
					bookmark.Outlines.AddLast(String.Format("PAGE {0}", i + j + 1)).SetDestination(document.Pages(i + j), PdfDestinationViewType.FitPage)
					j += 1
				Loop
				i += 10
			Loop
			document.PageMode = PdfPageMode.UseOutlines
			document.Save("Bookmarks.pdf")
		End Using

		System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Bookmarks.pdf") With {.UseShellExecute = True})
	End Sub
End Class

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.