←Select platform

WriteBookmarks Method

Summary
Writes bookmarks (TOC) to an existing PDF file.
Syntax
C#
C++/CLI
Java
Python
public void WriteBookmarks( 
   IList<PDFBookmark> bookmarks, 
   string destinationFileName 
) 
public void writeBookmarks( 
   java.util.List<PDFBookmark> bookmarks, 
   java.lang.String destinationFileName 
); 
public: 
void WriteBookmarks(  
   IList<PDFBookmark>^ bookmarks, 
   String^ destinationFileName 
)  
def WriteBookmarks(self,bookmarks,destinationFileName): 

Parameters

bookmarks
A list of PDFBookmark objects to write. This must not be null and must be a list of 1 or more items. An empty list (count is zero) will throw an exception.

destinationFileName
Name of the destination PDF file to be created. If the value of this parameter is null then the filename set in FileName will be updated.

Remarks

You can use WriteBookmarks method to create a Table of Contents (TOC) for a PDF file. For more information, refer to PDFBookmark.

You can create a bookmarks list from scratch and populate with the bookmarks required, or you can read the bookmarks of a PDF file using PDFDocument.ParseDocumentStructure with the PDFParseDocumentStructureOptions.Bookmarks included in the options parameter. Modify these bookmarks then write them back to the file.

WriteBookmarks does not clear the existing bookmarks, only adds the new objects. To replace the bookmarks in an existing file with new ones, use ClearBookmarks first to delete any existing objects and then WriteBookmarks to add the new objects.

Note that ClearBookmarks will delete all existing bookmarks and any internal links found in the document.

Example
C#
Java
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:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.BufferedWriter; 
import java.io.Console; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.nio.Buffer; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.StandardOpenOption; 
import java.sql.Date; 
import java.text.SimpleDateFormat; 
import java.time.LocalDateTime; 
import java.util.ArrayList; 
import java.util.List; 
 
import javax.xml.validation.Schema; 
 
import org.apache.lucene.store.Directory; 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.barcode.*; 
import leadtools.codecs.*; 
import leadtools.pdf.*; 
import leadtools.svg.*; 
 
 
public void pdfDocumentParseDocumentStructureExample() { 
   String LEAD_VARS_ImagesDir = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String pdfFileName1 = combine(LEAD_VARS_ImagesDir, "Leadtools.pdf"); 
   String pdfFileName2 = 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(); 
   ArrayList<PDFBookmark> bookmarks = new ArrayList<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.getPageCount(); i++) { 
 
      PDFFilePage page = file.getPages().get(i); 
      PDFBookmark bookmark = new PDFBookmark(); 
      bookmark.setTitle("Goto page " + page.getPageNumber()); 
      bookmark.setBookmarkStyle(PDFBookmarkStyle.PLAIN); 
      bookmark.setLevel(level); 
      bookmark.setTargetPageNumber(page.getPageNumber()); 
      bookmark.setTargetPageFitType(PDFPageFitType.DEFAULT); 
      bookmark.setTargetPosition(new PDFPoint(0, page.getHeight())); 
      bookmark.setTargetZoomPercent(0); 
      bookmarks.add(bookmark); 
 
      level++; 
      if (level > 8) { 
         // Reset levels 
         level = 0; 
      } 
 
   } 
   file.writeBookmarks(bookmarks, pdfFileName2); 
 
   // Create a document for the output file 
   PDFDocument document = new PDFDocument(pdfFileName2); 
   // Now read the bookmarks and internal links in the document 
   document.parseDocumentStructure(PDFParseDocumentStructureOptions.INTERNAL_LINKS.getValue() 
         | PDFParseDocumentStructureOptions.BOOKMARKS.getValue()); 
 
   System.out.printf("%1s bookmarks found:%n", document.getBookmarks().size()); 
   assertTrue(document.getBookmarks().size() == 25); 
   for (PDFBookmark bookmark : document.getBookmarks()) { 
      System.out.printf(" Title: %1s, Level: %2s, Target page: %3s%n", bookmark.getTitle(), bookmark.getLevel(), 
            bookmark.getTargetPageNumber()); 
   } 
 
   System.out.printf("%1s Internal links found:%n", document.getInternalLinks().size()); 
   assertTrue(document.getInternalLinks().size() == 18); 
   for (PDFInternalLink internalLink : document.getInternalLinks()) { 
      System.out.printf(" Source bounds: %1s, Target page: %2s%n", internalLink.getSourceBounds(), 
            internalLink.getTargetPageNumber()); 
   } 
 
} 
Requirements

Target Platforms

See Also

Reference

PDFFile Class

PDFFile Members

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.