Create Bookmarks
Create Bookmarks of All Pages
In order to create bookmarks of all the pages, you need to use CreateBookmarks method without any parameters . PdfBookmarkEditor class allows you to create bookmarks of all the pages of a PDF file. First, you need to create an object of PdfBookmarkEditor class and bind the input PDF using BindPdf method. Then, you have to call CreateBookmarks method and save the output PDF file using Save method. The following code snippet shows you how to create Bookmarks.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateBookmarksOfAllPages()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks();
// Create PdfBookmarkEditor
using (var bookmarkEditor = new Aspose.Pdf.Facades.PdfBookmarkEditor())
{
// Bind PDF document
bookmarkEditor.BindPdf(dataDir + "CreateBookmarksAll.pdf");
// Create bookmark of all pages
bookmarkEditor.CreateBookmarks();
// Save PDF document
bookmarkEditor.Save(dataDir + "CreateBookmarksOfAllPages_out.pdf");
}
}
Create Bookmarks of All Pages with Properties
PdfBookmarkEditor class allows you to create bookmarks of all the pages of a PDF file and specify the properties (Color, Bold, Italic). You can do that with the help of CreateBookmarks method. First, you need to create an object of PdfBookmarkEditor class and bind the input PDF using BindPdf method. Then, you have to call CreateBookmarks method and save the output PDF file using Save method. The following code snippet shows you how to create bookmarks of all the pages with properties.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateBookmarksOfAllPagesWithProperties()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks();
// Create PdfBookmarkEditor
using (var bookmarkEditor = new Aspose.Pdf.Facades.PdfBookmarkEditor())
{
// Bind PDF document
bookmarkEditor.BindPdf(dataDir + "CreateBookmarks-PagesProperties.pdf");
// Create bookmark of all pages
bookmarkEditor.CreateBookmarks(System.Drawing.Color.Green, true, true);
// Save PDF document
bookmarkEditor.Save(dataDir + "CreateBookmarks-PagesProperties_out.pdf");
}
}
Create Bookmark of a Particular Page
You can create a bookmark of a particular page in an existing PDF file using CreateBookmarkOfPage method. This method takes two arguments: bookmark title and page number. First, you need to create an object of PdfBookmarkEditor class and bind input PDF file using BindPdf method. Then, you have to call the CreateBookmarkOfPage method and save the output PDF file using Save method. The following code snippet shows you how to create bookmark of a particular page.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateBookmarkOfAParticularPage()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks();
// Create PdfBookmarkEditor
using (var bookmarkEditor = new Aspose.Pdf.Facades.PdfBookmarkEditor())
{
// Bind PDF document
bookmarkEditor.BindPdf(dataDir + "CreateBookmark-Page.pdf");
// Create bookmark of a particular page
bookmarkEditor.CreateBookmarkOfPage("Bookmark Name", 2);
// Save PDF document
bookmarkEditor.Save(dataDir + "CreateBookmark-Page_out.pdf");
}
}
Create Bookmarks of a Range of Pages
PdfBookmarkEditor class allows you to create bookmarks of a range of pages. You can use CreateBookmarkOfPage method with two parameters: bookmark list (the list of the bookmark titles) and page list (the list of the pages to bookmark). First, you need to create an object of PdfBookmarkEditor class and bind the input PDF file using BindPdf method. Then, you have to call CreateBookmarkOfPage method and save the output PDF using Save method. The following code snippet shows you how to create bookmarks of a range of pages.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateBookmarksOfARangeOfPages()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks();
// Create PdfBookmarkEditor
using (var bookmarkEditor = new Aspose.Pdf.Facades.PdfBookmarkEditor())
{
// Bind PDF document
bookmarkEditor.BindPdf(dataDir + "CreateBookmark-Page.pdf");
// Bookmark name list
string[] bookmarkList = { "First" };
// Page list
int[] pageList = { 1 };
// Create bookmark of a range of pages
bookmarkEditor.CreateBookmarkOfPage(bookmarkList, pageList);
// Save PDF document
bookmarkEditor.Save(dataDir + "CreateBookmarkPageRange_out.pdf");
}
}
Add Bookmark in an Existing PDF File
You can add bookmark in an existing PDF file using PdfBookmarkEditor class. In order to create the bookmark, you need to create Bookmark object and set the required attributes of the bookmark. After that, you need to pass the Bookmark object to the CreateBookmarks method of PdfBookmarkEditor class. Finally, you need to save the updated PDF file using Save method. The following code snippet shows you how to add the bookmark in an existing PDF file.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddBookmarkInAnExistingPdfFile()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks();
// Create bookmark
var bookmark = new Aspose.Pdf.Facades.Bookmark();
bookmark.PageNumber = 1;
bookmark.Title = "New Bookmark";
// Create PdfBookmarkEditor
using (var bookmarkEditor = new Aspose.Pdf.Facades.PdfBookmarkEditor())
{
// Bind PDF document
bookmarkEditor.BindPdf(dataDir + "AddBookmark.pdf");
// Create bookmarks
bookmarkEditor.CreateBookmarks(bookmark);
// Save PDF document
bookmarkEditor.Save(dataDir + "AddBookmark_out.pdf");
}
}
Add Child Bookmark in an Existing PDF File
You can add child bookmarks in an existing PDF file using PdfBookmarkEditor class. In order to add child bookmarks, you need to create Bookmark objects. You can add individual Bookmark objects into Bookmarks object. You also need to create a Bookmark object and set its ChildItem property to Bookmarks object. You then need to pass this Bookmark object with ChildItem to the CreateBookmarks method. Finally, you need to save the updated PDF using Save method of the PdfBookmarkEditor class. The following code snippet shows you how to add child bookmarks in an existing PDF file.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddChildBookmarkInAnExistingPdfFile()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks();
// Create bookmarks
var bookmarks = new Aspose.Pdf.Facades.Bookmarks();
var childBookmark1 = new Aspose.Pdf.Facades.Bookmark();
childBookmark1.PageNumber = 1;
childBookmark1.Title = "First Child";
var childBookmark2 = new Aspose.Pdf.Facades.Bookmark();
childBookmark2.PageNumber = 2;
childBookmark2.Title = "Second Child";
bookmarks.Add(childBookmark1);
bookmarks.Add(childBookmark2);
var bookmark = new Aspose.Pdf.Facades.Bookmark();
bookmark.Action = "GoTo";
bookmark.PageNumber = 1;
bookmark.Title = "Parent";
bookmark.ChildItems = bookmarks;
// Create PdfBookmarkEditor class
using (var bookmarkEditor = new Aspose.Pdf.Facades.PdfBookmarkEditor())
{
// Bind PDF document
bookmarkEditor.BindPdf(dataDir + "AddChildBookmark.pdf");
// Create bookmarks
bookmarkEditor.CreateBookmarks(bookmark);
// Save PDF document
bookmarkEditor.Save(dataDir + "AddChildBookmark_out.pdf");
}
}