Essential PDF是一款专门用于操作处理PDF文档的.NET控件,该产品包含在
Syncfusion公司下Asp.NET、Asp.NET MVC、SilverLight、WinRT以及WinForm等平台下的界面套包里,开发人员如果对该产品感兴趣可以下载
Essential Studio for Windows Forms等套包进行安装后就可以使用该产品了,这篇文章主要介绍怎么利用Essential PDF对PDF文件中的书签进行插入、删除以及修改等,具体可以参考下面的详细代码:
1.为PDF文件添加书签
//Creates a new document.
PdfDocument document = new PdfDocument();
//Adds a page.
PdfPage page = document.Pages.Add();
//Creates document bookmarks.
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
//Sets the destination page.
bookmark.Destination = new PdfDestination(page);
//Sets the destination location.
bookmark.Destination.Location = new PointF(20, 20);
//Sets the text style and color.
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;
//Saves and closes the PDF document.
document.Save("Output.pdf");
document.Close(True);
2.为PDF文件中存在的书签添加子项
/Creates a new document.
PdfDocument document = new PdfDocument();
//Adds a page.
PdfPage page = document.Pages.Add();
//Creates bookmark.
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
//Sets the destination page.
bookmark.Destination = new PdfDestination(page);
//Sets the destination location.
bookmark.Destination.Location = new PointF(20, 20);
//Adds the child bookmark
PdfBookmark childBookmark = bookmark.Insert(0,"heading 1");
childBookmark.Destination = new PdfDestination(page);
childBookmark.Destination.Location = new PointF(400, 300);
childBookmark.Destination.Zoom = 2F;
//Sets the text style and color.
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;
//Saves and closes the PDF document.
document.Save("Output.pdf");
document.Close(True);
3.为PDF文件插入书签
//Creates a new document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Inserts a new bookmark in the existing bookmark collection.
PdfBookmark bookmark = document.Bookmarks.Insert(1, "New Page 2");
//Sets the destination page and location.
bookmark.Destination = new PdfDestination(document.Pages[1]);
bookmark.Destination.Location = new PointF(0, 300);
//Saves and closes the PDF document.
document.Save("Output.pdf");
document.Close(True);
4.删除PDF文件中的书签
//Loads the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Gets all the bookmarks.
PdfBookmarkBase bookmarks = document.Bookmarks;
//Removes bookmark by bookmark name.
bookmarks.Remove("Page 1");
//Removes bookmark by index.
bookmarks.RemoveAt(1);
//Saves and closes the document.
document.Save("Output.pdf");
document.Close(True);
5.修改PDF文件中的书签
//Loads the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Gets all the bookmarks.
PdfBookmarkBase bookmarks = document.Bookmarks;
//Gets the first bookmark and changes the properties of the bookmark.
PdfLoadedBookmark bookmark = bookmarks[0] as PdfLoadedBookmark;
bookmark.Destination = new PdfDestination(document.Pages[1]);
bookmark.Color = Color.Green;
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Title = "Changed title";
//Saves the document
document.Save("Output.pdf");
document.Close(True);