DocumentDocuments Object

Summary

Represents a collection of LEADDocument objects.

Syntax
TypeScript
JavaScript
function lt.Document.DocumentDocuments 
   extends LeadCollection 
class lt.Document.DocumentDocuments 
   extends LeadCollection 
Remarks

DocumentDocuments manages the children of the document. It can be accessed through the Documents property of LEADDocument.

DocumentDocuments derives from LeadCollection. You can use any of the collection methods to iterate through the documents. This collection is read-only however and you cannot add, remove or change the items. Instead, use Pages to add or remove pages that belong to a separate document to this one. The LEADDocument.Documents collection will automatically gets updated to reflect what child documents are currently held in the document.

Here is an example that shows what happens to LEADDocument.Documents when pages are added and removed:

  1. When document (virtualDocument is created, LEADDocument.Documents is an empty collection

  2. The user creates a new empty page using CreatePage of virtualDocument and then adds it to the LEADDocument.Pages collection. The LEADDocument.Documents collection will still be an empty collection since the page original owner is the same document (in other words, DocumentPage.Document is virtualDocument

  3. The user add a page from another document (document1) into LEADDocument.Pages of virtualDocument. The LEADDocument.Documents collection will contain a single item: document1

  4. The user add another page from document1 into LEADDocument.Pages of virtualDocument. The LEADDocument.Documents collection will still contain the single item document1 since both pages has the same owner

  5. The user add a page from document2 into LEADDocument.Pages of virtualDocument. The LEADDocument.Documents collection will contain two items document1 and document2

  6. The user removes this last page from virtualDocument. The LEADDocument.Documents collection will be containing the single item document1

  7. The user removes all the pages from virtualDocument. The LEADDocument.Documents collection will be to being empty

When DocumentFactory.SaveToCache is called, the document will store the IDs of all the child documents.

When DocumentFactory.LoadFromCache is called, the document will try to re-load all the child documents and automatically set them in this collection. If any document fails to load or is no longer available in the cache, then all its pages are removed automatically and are nor loaded.

When the document is disposed, the value of AutoDisposeDocuments is examined, and if its true, then all the child documents are disposed as well.

Example
Create.ts
DocumentHelper.ts
Create.js
DocumentHelper.js
Create.html
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
 
export class DocumentFactory_CreateExample { 
   public constructor() { 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
      DocumentHelper.initFactory(); 
   } 
 
   private urls = [ 
      "https://demo.leadtools.com/images/pdf/leadtools.pdf", 
      "https://demo.leadtools.com/images/tiff/barcodes.tif" 
   ]; 
   private childDocs = []; 
   private childDocCount = this.urls.length; 
   private processedCount = 0; 
   private loadedCount = 0; 
 
   public run = (buttonID: string) => { 
      const exampleButton = document.getElementById(buttonID); 
      exampleButton.onclick = this.loadDocument; 
   } 
 
   loadDocument = () => { 
      // Load this one 
      const index = this.processedCount; 
      const loadOptions = new lt.Document.LoadDocumentOptions(); 
      DocumentHelper.log("Loading " + this.urls[index]); 
      lt.Document.DocumentFactory.loadFromUri(this.urls[index], loadOptions) 
         .done((childDoc) => { 
            // Finished, save the document and load next 
            this.loadedCount++; 
            this.childDocs.push(childDoc); 
         }) 
         .fail(DocumentHelper.showServiceError) 
         .always(() => { 
            this.processedCount++; 
            if (this.processedCount >= this.childDocCount) { 
               // All children loaded, if we did not have an error continue with the example 
               if (this.loadedCount === this.processedCount) { 
                  DocumentHelper.log("All documents loaded", this.childDocs); 
                  this.createDocument(); 
               } 
            } 
            else { 
               this.loadDocument(); 
            } 
         }); 
   } 
 
   logDocument = (doc) => DocumentHelper.log("Name: " + doc.name + " Pages: " + doc.pages.count + " Documents: " + doc.documents.count, doc); 
 
   createDocument = () => { 
      // Create a new document 
      const createOptions = new lt.Document.CreateDocumentOptions(); 
      const doc = lt.Document.DocumentFactory.create(createOptions); 
      doc.name = "Virtual"; 
      // Should have 0 pages and documents 
      this.logDocument(doc); 
 
      // Add page 2 and 3 from the first child document (PDF) 
      let childDoc = this.childDocs[0]; 
      doc.pages.add(childDoc.pages.item(2)); 
      doc.pages.add(childDoc.pages.item(3)); 
 
      // Add an empty page (8.5 by 11 inches at 300 DPI) 
      const docPage = doc.pages.createPage(lt.LeadSizeD.create(lt.Document.LEADDocument.unitsPerInch * 8.5, lt.Document.LEADDocument.unitsPerInch * 11), 300); 
      doc.pages.add(docPage); 
 
      // Add page 1 from the second child document (TIFF) 
      childDoc = this.childDocs[1]; 
      doc.pages.add(childDoc.pages.item(0)); 
 
      // Should have 4 pages and 2 documents (the PDF and the TIFF) 
      DocumentHelper.log("Pages added"); 
      this.logDocument(doc); 
 
      // Save the ID so we can load it 
      const documentId = doc.documentId; 
 
      // Now save this 'parent' document into the cache 
      lt.Document.DocumentFactory.saveToCache(doc) 
         .done(() => { 
            DocumentHelper.log("Document saved to cache at " + documentId) 
 
            // Tell this document to dispose all children 
            doc.autoDisposeDocuments = true; 
            // And dispose it 
            doc.dispose(); 
 
            // Now, re-load this document from the cache 
            lt.Document.DocumentFactory.loadFromCache(documentId) 
               .done((doc) => { 
                  // Should have 5 pages and 2 documents (the PDF and the TIFF) 
                  DocumentHelper.log("Document loaded from cache"); 
                  this.logDocument(doc); 
 
                  // Delete first page 
                  doc.pages.removeAt(0); 
                  // Delete the last page 
                  doc.pages.removeAt(doc.pages.count - 1); 
 
                  // Should have 3 pages and 1 document (the PDF only) 
                  DocumentHelper.log("After removing first and last pages..."); 
                  this.logDocument(doc); 
 
                  // Delete this document from the cache 
                  // Note: This does not delete the 'children' from the cache 
                  lt.Document.DocumentFactory.deleteFromCache(documentId); 
               }) 
               .fail(DocumentHelper.showServiceError); 
         }) 
         .fail(DocumentHelper.showServiceError); 
   } 
} 
export class DocumentHelper { 
   static showServiceError = (jqXHR, statusText, errorThrown) => { 
      alert("Error returned from service. See the console for details."); 
      const serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
      console.error(serviceError); 
   } 
 
   static log = (message: string, data?: any) => { 
      const outputElement = document.getElementById("output"); 
      if (outputElement) { 
         const time = (new Date()).toLocaleTimeString(); 
         const textElement = document.createElement("p"); 
         textElement.innerHTML = (outputElement.childElementCount + 1) + " [" + time + "]: " + message; 
         outputElement.insertBefore(textElement, outputElement.firstChild); 
      } 
 
      if (!data) 
         console.log(message); 
      else 
         console.log(message, data); 
   } 
 
   static initFactory = () => { 
      // To communicate with the DocumentsService, it must be running! 
      // Change these parameters to match the path to the service. 
      lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
      lt.Document.DocumentFactory.servicePath = ""; 
      lt.Document.DocumentFactory.serviceApiPath = "api"; 
   } 
} 
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
 
export class DocumentFactory_CreateExample { 
   constructor() { 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
      DocumentHelper.initFactory(); 
   } 
 
   urls = [ 
      "https://demo.leadtools.com/images/pdf/leadtools.pdf", 
      "https://demo.leadtools.com/images/tiff/barcodes.tif" 
   ]; 
   childDocs = []; 
   childDocCount = this.urls.length; 
   processedCount = 0; 
   loadedCount = 0; 
 
   run = (buttonID) => { 
      const exampleButton = document.getElementById(buttonID); 
      exampleButton.onclick = this.loadDocument; 
   } 
 
   loadDocument = () => { 
      // Load this one 
      const index = this.processedCount; 
      const loadOptions = new lt.Document.LoadDocumentOptions(); 
      DocumentHelper.log("Loading " + this.urls[index]); 
      lt.Document.DocumentFactory.loadFromUri(this.urls[index], loadOptions) 
         .done((childDoc) => { 
            // Finished, save the document and load next 
            this.loadedCount++; 
            this.childDocs.push(childDoc); 
         }) 
         .fail(DocumentHelper.showServiceError) 
         .always(() => { 
            this.processedCount++; 
            if (this.processedCount >= this.childDocCount) { 
               // All children loaded, if we did not have an error continue with the example 
               if (this.loadedCount === this.processedCount) { 
                  DocumentHelper.log("All documents loaded", this.childDocs); 
                  this.createDocument(); 
               } 
            } 
            else { 
               this.loadDocument(); 
            } 
         }); 
   } 
 
   logDocument = (doc) => DocumentHelper.log("Name: " + doc.name + " Pages: " + doc.pages.count + " Documents: " + doc.documents.count, doc); 
 
   createDocument = () => { 
      // Create a new document 
      const createOptions = new lt.Document.CreateDocumentOptions(); 
      const doc = lt.Document.DocumentFactory.create(createOptions); 
      doc.name = "Virtual"; 
      // Should have 0 pages and documents 
      this.logDocument(doc); 
 
      // Add page 2 and 3 from the first child document (PDF) 
      let childDoc = this.childDocs[0]; 
      doc.pages.add(childDoc.pages.item(2)); 
      doc.pages.add(childDoc.pages.item(3)); 
 
      // Add an empty page (8.5 by 11 inches at 300 DPI) 
      const docPage = doc.pages.createPage(lt.LeadSizeD.create(lt.Document.LEADDocument.unitsPerInch * 8.5, lt.Document.LEADDocument.unitsPerInch * 11), 300); 
      doc.pages.add(docPage); 
 
      // Add page 1 from the second child document (TIFF) 
      childDoc = this.childDocs[1]; 
      doc.pages.add(childDoc.pages.item(0)); 
 
      // Should have 4 pages and 2 documents (the PDF and the TIFF) 
      DocumentHelper.log("Pages added"); 
      this.logDocument(doc); 
 
      // Save the ID so we can load it 
      const documentId = doc.documentId; 
 
      // Now save this 'parent' document into the cache 
      lt.Document.DocumentFactory.saveToCache(doc) 
         .done(() => { 
            DocumentHelper.log("Document saved to cache at " + documentId) 
 
            // Tell this document to dispose all children 
            doc.autoDisposeDocuments = true; 
            // And dispose it 
            doc.dispose(); 
 
            // Now, re-load this document from the cache 
            lt.Document.DocumentFactory.loadFromCache(documentId) 
               .done((doc) => { 
                  // Should have 5 pages and 2 documents (the PDF and the TIFF) 
                  DocumentHelper.log("Document loaded from cache"); 
                  this.logDocument(doc); 
 
                  // Delete first page 
                  doc.pages.removeAt(0); 
                  // Delete the last page 
                  doc.pages.removeAt(doc.pages.count - 1); 
 
                  // Should have 3 pages and 1 document (the PDF only) 
                  DocumentHelper.log("After removing first and last pages..."); 
                  this.logDocument(doc); 
 
                  // Delete this document from the cache 
                  // Note: This does not delete the 'children' from the cache 
                  lt.Document.DocumentFactory.deleteFromCache(documentId); 
               }) 
               .fail(DocumentHelper.showServiceError); 
         }) 
         .fail(DocumentHelper.showServiceError); 
   } 
} 
export class DocumentHelper { 
   static showServiceError = (jqXHR, statusText, errorThrown) => { 
      alert("Error returned from service. See the console for details."); 
      const serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
      console.error(serviceError); 
   } 
 
   static log = (message, data) => { 
      const outputElement = document.getElementById("output"); 
      if (outputElement) { 
         const time = (new Date()).toLocaleTimeString(); 
         const textElement = document.createElement("p"); 
         textElement.innerHTML = (outputElement.childElementCount + 1) + " [" + time + "]: " + message; 
         outputElement.insertBefore(textElement, outputElement.firstChild); 
      } 
 
      if (!data) 
         console.log(message); 
      else 
         console.log(message, data); 
   } 
 
   static initFactory = () => { 
      // To communicate with the DocumentsService, it must be running! 
      // Change these parameters to match the path to the service. 
      lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
      lt.Document.DocumentFactory.servicePath = ""; 
      lt.Document.DocumentFactory.serviceApiPath = "api"; 
   } 
} 
<!doctype html> 
<html lang="en"> 
<title>Document Example | Create</title> 
 
<head> 
   <script src="https://code.jquery.com/jquery-2.2.4.min.js" 
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
 
   <script src="../../LT/Leadtools.js"></script> 
   <script src="../../LT/Leadtools.Controls.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Engine.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Designers.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Rendering.Javascript.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Automation.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Main.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Color.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Core.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Effects.js"></script> 
   <script src="../../LT/Leadtools.Document.js"></script> 
   <script src="../../LT/Leadtools.Document.Viewer.js"></script> 
   <link rel="stylesheet" type="text/css" href="../../css/examples.css"> 
 
   <!-- All demo files are bundled and appended to the window --> 
   <script src="../../bundle.js" type="text/javascript"></script> 
</head> 
 
<body> 
   <div> 
      <button type="button" id="exampleButton">Run Example</button> 
   </div> 
   <div id="output"></div> 
   <div> 
      <img id="img" /> 
   </div> 
</body> 
 
<script> 
   window.onload = () => { 
      const example = new window.examples.DocumentFactory.Create(); 
      example.run("exampleButton"); 
   }; 
</script> 
</html> 
Requirements

Target Platforms

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

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