DocumentStructure Object

Summary

Manages the structure of the document.

Syntax
TypeScript
JavaScript
function lt.Document.DocumentStructure 
class lt.Document.DocumentStructure() 

Remarks

DocumentStructure manages the structure of the document. This includes the bookmarks that represents the table of content. It can be accessed through the Structure property of LEADDocument.

Not all document types contain support for document structure. For these types of documents (such as TIF file or Text document), the value of IsStructureSupported of Document will be false and the Structure is null and should not be used. The rest of the discussion of this section is related to when IsStructureSupported is true.

When the structure is supported (for example, PDF documents), the value of IsStructureSupported is true and Structure is a valid object can be used.

Loading the document structure can take some time, therefore, it is not read automatically when a new LEADDocument is created from a disk or URL document. This is done so that the document is loaded as quick as possible and to not make the user wait if the bookmarks or table of content is not needed in your application.

To parse the document for bookmarks and table of content, use the Parse method while the value of ParseBookmarks is true (the default value). Usually you can call this method once after the LEADDocument is loaded. The IsParsed property can be used to check if the structure has been parsed.

If Parse is not called or the value of ParseBookmarks is false, then Bookmarks is an empty list.

When Parse is called and ParseBookmarks is true (the default value), the Bookmarks list will be updated with all the bookmarks and table of content items found in the original document. You can then read and modify these bookmarks if needed. If the value of ParseBookmarks is set to false, then Parse will not try to parse the bookmarks in the document.

Parse will also parse the document for any links found in the pages if the value of ParsePageLinks is true (the default value), these links can be obtain through the DocumentPage.GetLinks method. If the value of ParsePageLinks is set to false, then Parse will not try to parse the links found in the pages.

Setting ParseBookmarks or ParsePageLinks to false can help with increasing the speed of the Parse method if the user is not interested in one or the other.

Example

This example shows how to parse the structure (bookmarks) of a PDF document.

Start with the example in LEADDocument and replace the example function call to the function below.

TypeScript
JavaScript
var url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
log("Loading document..."); 
lt.Document.DocumentFactory.loadFromUri(url, null) 
   .done(function (doc) { 
      log("Done. Showing the structure..."); 
 
      if (!doc.isStructureSupported) { 
         // This document does not support table of content or links 
         log("Structure not supported"); 
         return; 
      } 
 
      if (doc.structure.isParsed) { 
         // Show them 
         showBookmarks(doc.structure.bookmarks); 
      } 
      else { 
         // We support it, but we don't have it parsed yet 
         doc.structure.parse() 
            .done(function (doc) { 
               log("Document structure was parsed succesfully"); 
               showBookmarks(doc.structure.bookmarks); 
            }) 
            .fail(showServiceError); 
      } 
 
   }) 
   .fail(showServiceError); 
 
 
function showBookmarks(bookmarks) { 
   var ul = makeBookmarkList(bookmarks); 
   var exampleButton = document.getElementById("exampleButton"); 
   exampleButton.parentNode.appendChild(ul); 
} 
 
function makeBookmarkList(bookmarks) { 
   if (bookmarks && bookmarks.length) { 
      var ul = document.createElement("ul"); 
      for (var i = 0; i < bookmarks.length; i++) { 
         var bookmark = bookmarks[i]; 
         var li = document.createElement("li"); 
         var span = document.createElement("span"); 
         span.innerHTML = bookmark.title + " (Page " + bookmark.target.pageNumber + ")"; 
         li.appendChild(span); 
         if (bookmark.children.length) 
            li.appendChild(makeBookmarkList(bookmark.children)); 
         ul.appendChild(li); 
      } 
      return ul; 
   } 
   return null; 
} 
class LEADDocument_StructurExample { 
    private el: HTMLElement; 
 
    public constructor() { 
        lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt", "EVAL", null); 
    } 
 
    public run(buttonID: string) { 
        this.el = document.getElementById(buttonID); 
        this.el.onclick = this.loadDocument; 
    } 
 
    loadDocument = () => { 
        const url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
        log("Loading document..."); 
 
        lt.Document.DocumentFactory.loadFromUri(url, null) 
        .done((doc: lt.Document.LEADDocument) => { 
           log("Done. Showing the structure..."); 
 
           if (!doc.isStructureSupported) { 
              // This document does not support table of content or links 
              log("Structure not supported"); 
              return; 
           } 
 
           if (doc.structure.isParsed) { 
              // Show them 
              this.showBookmarks(doc.structure.bookmarks); 
           } 
           else { 
              // We support it, but we don't have it parsed yet 
              doc.structure.parse() 
                 .done((doc) => { 
                    log("Document structure was parsed succesfully"); 
                    this.showBookmarks(doc.structure.bookmarks); 
                 }) 
                 .fail(showServiceError); 
           } 
 
        }) 
        .fail(showServiceError); 
    } 
 
    showBookmarks = (bookmarks: lt.Document.DocumentBookmark[]) => { 
        const ul = this.makeBookmarkList(bookmarks); 
        this.el.parentNode.appendChild(ul); 
    } 
 
    makeBookmarkList = (bookmarks: lt.Document.DocumentBookmark[]) =>{ 
        if (bookmarks && bookmarks.length) { 
            const ul = document.createElement("ul"); 
            for (let i = 0; i < bookmarks.length; i++) { 
               const bookmark = bookmarks[i]; 
               const li = document.createElement("li"); 
               const span = document.createElement("span"); 
               span.innerHTML = bookmark.title + " (Page " + bookmark.target.pageNumber + ")"; 
               li.appendChild(span); 
               if (bookmark.children.length) 
                  li.appendChild(this.makeBookmarkList(bookmark.children)); 
               ul.appendChild(li); 
            } 
            return ul; 
         } 
         return null; 
    } 
} 

Requirements

Target Platforms

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

Leadtools.Document Assembly