create Method

Summary

Creates a new, empty virtual document.

Syntax
TypeScript
JavaScript
create = function( 
   options 
) 

Parameters

options

Options to use when creating the document. Cannot be null.

Return Value

A Promise object that can resolve successfully to a LEADDocument object, or fail if the LEADDocument cannot be returned because it does not exist with that documentId.

Remarks

Creates a new, empty virtual document ready to be filled with pages from other documents.

This method will add items with the key "Created", "Accessed", and "Modified" to Metadata with values equal to the current date and time. The newly created LEADDocument object will have an empty list of Pages. It can then be populated by the user.

The members of options are used as follows:

Member Description
DocumentId

The ID to be used with the loaded document. When this value null (the default), then the document factory will create a new unique ID using a GUID generator. If the value is not null, then it is assumed to be a user-defined ID and used as is. In either case, the value is set in the LEADDocument.DocumentId property of the newly created document.

User-defined IDs can be used when the system already have unique IDs associated with the documents to be viewed. The document factory will neither check nor guarantee the uniqueness of these IDs.
MimeType
Copied as is into the newly created document MimeType member. This value can be null but it is best to set it to the MIME type of the document since it will be used when saving the document. In the case of virtual documents, this value can be left as null.

Created documents can be saved into the cache using SaveToCache.

Example

This example will show how to compose a virtual document from a subset of PDF and a TIFF document pages.

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/v200/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/v200/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 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly