spire.pdf Component 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.
Add, modify, and delete a PDF bookmark implementation
First, Add Bookmark
1.1 Add Bookmark
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.
//Create a new PDF documentpdfdocument PDF=Newpdfdocument ();//Add a pagepdfpagebase Page=PDF. Pages.Add ();//Add BookmarkPdfbookmark Bookmark = pdf. Bookmarks.add ("first Page");//sets the page and location to which the bookmark points, (0,0) indicates where the page beginsbookmark. Destination=Newpdfdestination (page); bookmark. Destination.location=NewPointF (0,0);//set the text formatting and color of bookmarksbookmark. Displaystyle=Pdftextstyle.bold;bookmark. Color=Color.Black;//Save DocumentPDF. SaveToFile ("bookmark2.pdf");
1.2 Add 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.
//Create a new PDF documentpdfdocument PDF=Newpdfdocument ();//Add a pagepdfpagebase Page=PDF. Pages.Add (); //Add BookmarkPdfbookmark Bookmark= PDF. Bookmarks.add ("Chapter I. Heat conduction");//set the page and location that the bookmark points tobookmark. Destination=Newpdfdestination (page); bookmark. Destination.location=NewPointF (0,0); //set the text formatting and color of bookmarksbookmark. Displaystyle=Pdftextstyle.bold;bookmark. Color=Color.seagreen;//Add a child bookmarkPdfbookmark Childbookmark= Bookmark. Insert (0,"1.1 Basic knowledge of heat conduction");//set the page and location that the child bookmark points tochildbookmark.destination=Newpdfdestination (page); ChildBookmark.Destination.Location=NewPointF ( -, -);//set text formatting and color for sub-bookmarksChildbookmark.displaystyle=Pdftextstyle.regular;childbookmark.color=Color.Black;//Save DocumentPDF. SaveToFile ("childbookmark.pdf");
1.3 add 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.
//Loading Documentspdfdocument PDF=Newpdfdocument ();pd F. LoadFromFile ("example. pdf"); for(inti =0; I < PDF. Pages.count; i++){ //Add BookmarkPdfbookmark Bookmark= PDF. Bookmarks.add (string. Format ("Chapter {0}", i+1)); //set the page and location that the bookmark points tobookmark. Destination=Newpdfdestination (PDF. Pages[i]); Bookmark. Destination.location=NewPointF (0,0); //set the text formatting and color of bookmarksbookmark. Displaystyle=Pdftextstyle.bold; Bookmark. Color=Color.Black;}//Save DocumentPDF. 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 Modify an existing bookmark content
//Loading Documentspdfdocument PDF=Newpdfdocument ();pd F. LoadFromFile ("bookmark2.pdf");//get a list of bookmarkspdfbookmarkcollection Bookmarks=PDF. Bookmarks;//get first BookmarkPdfbookmark Bookmark= bookmarks[0];//Modify the page that the bookmark points tobookmark. Destination=NewPdfdestination (document. pages[1]); //Modify the text formatting and color of a bookmarkbookmark. Displaystyle=Pdftextstyle.bold;bookmark. Color=Color.green;//Modify the title of a bookmarkbookmark. Title="Modify";//Save DocumentPDF. SaveToFile ("modifybookmark.pdf");
2.2 Insert new book sign in to existing bookmark list
//Loading Documentspdfdocument PDF=Newpdfdocument ();pd F. LoadFromFile ("bookmark2.pdf"); //Insert a new book check in the specified location (where the third bookmark is inserted)Pdfbookmark Bookmark= PDF. Bookmarks.insert (2,"New Chapter III"); //set the page and location that the bookmark points tobookmark. Destination=NewPdfdestination (document. pages[1]); bookmark. Destination.location=NewPointF (0, -); //Save DocumentPDF. 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.
//Loading Documentspdfdocument PDF=Newpdfdocument ();pd F. LoadFromFile ("bookmark2.pdf");//get a list of bookmarkspdfbookmarkcollection Bookmarks=document. Bookmarks;//Delete First Bookmarkbookmarks. RemoveAt (0);//Save DocumentPDF. SaveToFile ("deletebookmark.pdf");
Transferred from: http://www.cnblogs.com/Yesi/p/7251214.html
C # operations PDF