[SerializableAttribute()]
public struct PDFBookmark
The PDFBookmark structure contains the properties of a single PDF bookmark. The collection of the bookmarks in a PDF file constitutes the Table of Contents (TOC) of the document. Typically, external viewers will load all the bookmarks of a document and generate this TOC. The viewer then allows the user to click on any entry in the TOC and perform the action of the bookmark.
The bookmarks and internal links of a PDF have a document (global) scope and are not part of any page. As a result, to read the bookmarks of a document, use the PDFDocument.ParseDocumentStructure method. Then access the PDFDocument.Bookmarks and PDFDocument.InternalLinks collections.
The bookmark has the following properties to represent its appearance:
Title: This is the text to appear to the user
Level: The bookmark indentation level. Level 0 is a root bookmark, level 1 is a child of the previous bookmark, level 2 is a child of the child and so on.
BookmarkStyle: The bookmark font style
The action of a bookmark consists of the following:
TargetPageNumber: This the 1-based integer to go to. External viewers should scroll to this page when this bookmark is activated
TargetPosition: The position in PDF units (1/72 of an inch and bottom-left) of the target page to scroll to. External viewers should scroll to this position when the bookmark is activated
TargetZoomPercent: The zoom percent (100 equals to no zoom) of the target page. External viewers should zoom to this value when the bookmark is activated
TargetPageFitType: The page fit type of the target page. External viewers should set the page view properties based on this value when this bookmark is activated
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Controls;
using Leadtools.Pdf;
using Leadtools.Svg;
using Leadtools.WinForms;
public void PDFDocumentParseDocumentStructureExample()
{
string pdfFileName1 = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf");
string pdfFileName2 = Path.Combine(LEAD_VARS.ImagesDir, @"Bookmarks.pdf");
// Create a version of the source file with a few bookmarks
PDFFile file = new PDFFile(pdfFileName1);
// Load the pages
file.Load();
List<PDFBookmark> bookmarks = new List<PDFBookmark>();
// We will bookmarks for each page, cascading levels:
// Goto page 1
// Goto page 2
// Goto page 3
// Goto page 4
int level = 0;
for (int i = 0; i < file.Pages.Count; i++)
{
PDFFilePage page = file.Pages[i];
PDFBookmark bookmark = new PDFBookmark();
bookmark.Title = "Goto page " + page.PageNumber.ToString();
bookmark.BookmarkStyle = PDFBookmarkStyle.Plain;
bookmark.Level = level;
bookmark.TargetPageNumber = page.PageNumber;
bookmark.TargetPageFitType = PDFPageFitType.Default;
bookmark.TargetPosition = new PDFPoint(0, page.Height);
bookmark.TargetZoomPercent = 0;
bookmarks.Add(bookmark);
level++;
if (level > 8)
{
// Reset levels
level = 0;
}
}
file.WriteBookmarks(bookmarks, pdfFileName2);
// Create a document for the output file
using (PDFDocument document = new PDFDocument(pdfFileName2))
{
// Now read the bookmarks and internal links in the document
document.ParseDocumentStructure(PDFParseDocumentStructureOptions.InternalLinks | PDFParseDocumentStructureOptions.Bookmarks);
Console.WriteLine("{0} bookmarks found:", document.Bookmarks.Count);
foreach (PDFBookmark bookmark in document.Bookmarks)
{
Console.WriteLine(" Title: {0}, Level: {1}, Target page: {2}", bookmark.Title, bookmark.Level, bookmark.TargetPageNumber);
}
Console.WriteLine("{0} Internal links found:", document.InternalLinks.Count);
foreach (PDFInternalLink internalLink in document.InternalLinks)
{
Console.WriteLine(" Source bounds: {0}, Target page: {1}", internalLink.SourceBounds, internalLink.TargetPageNumber);
}
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}