C# Add, modify, delete PDF bookmarks

Sometimes we encounter such a situation when reading PDF documents: PDF documents have many pages, but there are no bookmarks, so we cannot quickly understand the content of the document according to the bookmarks, nor can we click the bookmarks to quickly jump to the corresponding It is very distressing that you can only read page by page. I believe that developers will also have such functional requirements when developing applications related to PDF, so in this article, I will introduce how to use programming (C#) and Spire.PDF components to add bookmarks to PDF documents ( including adding bookmarks to existing documents and adding sub-bookmarks), as well as modifying and deleting specified bookmarks in existing PDF documents.

add bookmark

1.1  Add bookmarks

In Spire.PDF, each PDF document has a bookmark list (PdfBookmarkCollection). We can get this list through the Bookmarks property of the PdfDocument object, and then add bookmarks to the list through the Add() method.

【C#】

//Create a 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 pointed to by the bookmark, (0,0) indicates the start position of the page

bookmark.Destination = new PdfDestination(page);
bookmark.Destination.Location = new PointF(0, 0);

//Set the text format and color of the bookmark

bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Black;

// save the document

pdf.SaveToFile("Bookmark2.pdf");

 

 


 

1.2  Add sub bookmarks

Adding a sub-bookmark is basically the same as adding a normal bookmark. The difference is that the normal bookmark is directly added to the bookmark list of the document, while the sub-bookmark is added to the list of parent bookmarks.

【C#】

 

//Create a new PDF document

PdfDocument pdf = new PdfDocument();

//add page

PdfPageBase page = pdf.Pages.Add();

// add bookmark

PdfBookmark bookmark = pdf.Bookmarks.Add("Chapter 1 Heat Conduction");

//Set the page and location the bookmark points to

bookmark.Destination = new PdfDestination(page);
bookmark.Destination.Location = new PointF(0, 0);
 
//Set the text format and color of the bookmark

bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.SeaGreen;

//add sub bookmark

PdfBookmark childBookmark = bookmark.Insert(0, "1.1 Basic knowledge of heat conduction");

//Set the page and location pointed to by the sub-bookmark

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");

 

 

Sub bookmarks added result:



 

1.3  Add bookmarks to existing documents

In addition to adding bookmarks to newly created PDF documents, we can also add bookmarks to existing PDF documents. In addition to LoadFromFile, there are LoadFromStream (loading from stream), LoadFromHTML (loading from HTML), etc., to load PDF documents. You can choose the corresponding loading method according to your own needs.

【C#】

 

// 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("第{0}章", i+1));

    //Set the page and location the bookmark points to

    bookmark.Destination = new PdfDestination(pdf.Pages[i]);
    bookmark.Destination.Location = new PointF(0, 0);

    //Set the text format and color of the bookmark

    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 modification methods, such as modifying the content of existing bookmarks, inserting new bookmarks into the existing bookmark list, inserting sub-bookmarks into existing bookmarks, etc. Here I choose to modify the bookmark content and insert a new bookmark into the existing bookmark list for introduction.

2.1 Modify existing bookmarks

【C#】

// 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 to by the bookmark

bookmark.Destination = new PdfDestination(document.Pages[1]);

//Modify the text format and color of bookmarks

bookmark.DisplayStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Green;

//Modify the title of the bookmark

bookmark.Title = "Modify";

// save the document

pdf.SaveToFile("ModifyBookmark.pdf");

Modified result:



 

 

2.2  Insert a new bookmark into the existing bookmark list

【C#】

// load the document

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Bookmark2.pdf");

//Insert a new bookmark to the specified position (the position of the third bookmark is inserted here)

PdfBookmark bookmark = pdf.Bookmarks.Insert(2, "Add a third chapter");

//Set the page and location the bookmark points to

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 deleting a bookmark, you can use the bookmark's serial number or the bookmark's name. Here I am using the serial number method.

【C#】

// 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");

 



 The above bloggers only summarize some functions of operating PDF bookmarks. If you need to learn more about other functions, please refer to: http://e-iceblue.cn/Introduce/Spire-PDF-NET.html . If you have any questions, please leave a message to the blogger. Welcome to reprint, please indicate the source when reprinting!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326078995&siteId=291194637