This article mainly introduces the C # Add, modify and delete the PDF bookmark Instance code, small series think very good, now share to everyone, also to make a reference. Let's take a look at it with a little knitting.
Sometimes when we read a PDF document, we encounter a situation where the number of pages in a PDF document is more, but there is no bookmark, so we can not quickly understand the bookmark according to the content of the document, or click the bookmark to quickly jump to the corresponding location, but only a page by page, very distressed. I believe that developers will have such functional requirements when developing and using PDF-related applications, so in this article I'll explain how to use programming (C # ) and Spire.pdf components to bookmark PDF documents (including adding bookmarks to existing documents and adding sub-bookmarks), as well as modifying and deleting specified bookmarks in existing PDF documents.
spire.pdf Components Overview
Spire.pdf is a professional PDF component for creating, editing, processing, and reading PDF documents in. NET Applications. Supports rich PDF document processing operations such as PDF document merging/splitting, converting (such as HTML to pdf,pdf to image, etc.), printing (including silent printing), compressing, adding/modifying/deleting bookmarks, adding comments, security settings (including digital signatures), creating and populating fields, picture insertion and extraction, Text extraction and highlighting, and so on. It does not rely on Adobe Acrobat, so you do not need to install Adobe Reader or other similar components to run your environment. The component is divided into commercial and free (not trial version) two, the general personal use or operation of PDF documents no more than 10 pages, you can use the free version.
There are many ways to install it, including the official website and the NuGet methods that developers like and use most often. Enter the following PowerShell command in the NuGet package Manager console in Visual Studio, and the component's DLLs are automatically referenced to the project:
Pm> Install-package Spire.pdf
Add, modify, and delete a PDF bookmark implementation
First, add bookmarks
1.1 Adding bookmarks
In Spire.pdf, each PDF document has a bookmark list (pdfbookmarkcollection). We can get the list by pdfdocument the Bookmarks property of the object, and then add the bookmark to the list by using the Add () method.
// New PDF document
PdfDocument pdf = new PdfDocument ();
// Add page
PdfPageBase page = pdf.Pages.Add ();
// Add bookmark
PdfBookmark bookmark = pdf.Bookmarks.Add ("First page");
// Set the page and position to which the bookmark points, (0,0) indicates the starting position of the page
bookmark.Destination = new PdfDestination (page);
bookmark.Destination.Location = new PointF (0, 0);
// Set bookmark text format and color
bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Black;
// Save the document
pdf.SaveToFile ("Bookmark2.pdf");
1.2 Adding a child bookmark
The way to add a child bookmark and add a normal bookmark is basically the same, but the normal bookmark is added directly to the bookmark list of the document, and the child bookmark is added to the list of parent bookmarks.
// New PDF document
PdfDocument pdf = new PdfDocument ();
// Add page
PdfPageBase page = pdf.Pages.Add ();
// Add bookmark
PdfBookmark bookmark = pdf.Bookmarks.Add ("Chapter 1 Thermal Conduction");
// Set the page and location to which the bookmark points
bookmark.Destination = new PdfDestination (page);
bookmark.Destination.Location = new PointF (0, 0);
// Set bookmark text format and color
bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.SeaGreen;
// Add child bookmark
PdfBookmark childBookmark = bookmark.Insert (0, "1.1 Basic Heat Transfer Knowledge");
// Set the page and position to which the child bookmark points
childBookmark.Destination = new PdfDestination (page);
childBookmark.Destination.Location = new PointF (400, 300);
// Set the text format and color of the sub bookmark
childBookmark.DisplayStyle = PdfTextStyle.Regular;
childBookmark.Color = Color.Black;
// Save the document
pdf.SaveToFile ("ChildBookmark.pdf");
1.3 Adding bookmarks to an existing document
In addition to adding bookmarks to new PDF documents, we can also bookmark existing PDF documents. The method of loading PDF Documents Besides LoadFromFile, there are loadfromstream (from stream loading), loadfromhtml (loading from HTML), etc., can choose the appropriate loading mode according to their own needs.
// Load the document
PdfDocument pdf = new PdfDocument ();
pdf.LoadFromFile ("example.pdf");
for (int i = 0; i <pdf.Pages.Count; i ++)
{
// Add bookmark
PdfBookmark bookmark = pdf.Bookmarks.Add (string.Format ("Chapter {0}", i + 1));
// Set the page and location to which the bookmark points
bookmark.Destination = new PdfDestination (pdf.Pages [i]);
bookmark.Destination.Location = new PointF (0, 0);
// Set bookmark text format and color
bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Black;
}
// Save the document
pdf.SaveToFile ("Bookmark2.pdf");
2. Modify Bookmarks
Spire.pdf supports a variety of bookmark modifications, such as modifying the contents of an existing bookmark, inserting a new book into an existing bookmark list, inserting a child bookmark into an existing bookmark, and so on. Here I choose to modify the bookmark content and insert the new book into the existing bookmark list for introduction.
2.1 Modifying existing bookmark content
// Load the document
PdfDocument pdf = new PdfDocument ();
pdf.LoadFromFile ("Bookmark2.pdf");
// Get bookmark list
PdfBookmarkCollection bookmarks = pdf.Bookmarks;
// Get the first bookmark
PdfBookmark bookmark = bookmarks [0];
// Modify the page pointed by the bookmark
bookmark.Destination = new PdfDestination (document.Pages [1]);
// Modify the text format and color of the bookmark
bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Green;
// Modify the title of the bookmark
bookmark.Title = "Modify";
// Save the document
pdf.SaveToFile ("ModifyBookmark.pdf");
2.2 Inserting a new book sign in to an existing bookmark list
// Load the document
PdfDocument pdf = new PdfDocument ();
pdf.LoadFromFile ("Bookmark2.pdf");
// Insert a new bookmark to the specified position (the third bookmark is inserted here)
PdfBookmark bookmark = pdf.Bookmarks.Insert (2, "New Chapter Three");
// Set the page and location pointed by the bookmark
bookmark.Destination = new PdfDestination (document.Pages [1]);
bookmark.Destination.Location = new PointF (0, 300);
// Save the document
pdf.SaveToFile ("InsertBookmark.pdf");
3. Delete Bookmarks
When you delete a bookmark, you can use the ordinal of the bookmark, or you can use the name of the bookmark. Here I'm using the serial number method.
// Load the document
PdfDocument pdf = new PdfDocument ();
pdf.LoadFromFile ("Bookmark2.pdf");
// Get bookmark list
PdfBookmarkCollection bookmarks = document.Bookmarks;
// Delete the first bookmark
bookmarks.RemoveAt (0);
// Save the document
pdf.SaveToFile ("DeleteBookmark.pdf");