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 may 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 member 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 ID's associated with the documents to be viewer. The document factory will not 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 recommend you 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.

Start with the example from LEADDocument and replace all the code inside the example function (search for the "// TODO: add example code here" comment) with the following code:

TypeScript
JavaScript
// Load a couple of documents that will be used as the source 
// PDF and TIFF 
var urls = [ 
   "https://demo.leadtools.com/images/pdf/leadtools.pdf", 
   "https://demo.leadtools.com/images/tiff/barcodes.tif" 
]; 
var childDocs = []; 
var childDocCount = urls.length; 
var processedCount = 0; 
var loadedCount = 0; 
 
// Load the documents 
function loadDocument() { 
   // Load this one 
   var index = processedCount; 
   var loadOptions = new lt.Document.LoadDocumentOptions(); 
   log("Loading " + urls[index]); 
   lt.Document.DocumentFactory.loadFromUri(urls[index], loadOptions) 
      .done(function (childDoc) { 
         // Finished, save the document and load next 
         loadedCount++; 
         childDocs.push(childDoc); 
      }) 
      .fail(showServiceError) 
      .always(function () { 
         processedCount++; 
         if (processedCount >= childDocCount) { 
            // All children loaded, if we did not have an error continue with the example 
            if (loadedCount === processedCount) { 
               log("All documents loaded", childDocs); 
               createDocument(); 
            } 
         } 
         else { 
            loadDocument(); 
         } 
      }); 
}; 
loadDocument(); 
 
function logDocument(doc) { 
   log("Name: " + doc.name + " Pages: " + doc.pages.count + " Documents: " + doc.documents.count, doc); 
} 
 
function createDocument() { 
   // Create a new document 
   var createOptions = new lt.Document.CreateDocumentOptions(); 
   var doc = lt.Document.DocumentFactory.create(createOptions); 
   doc.name = "Virtual"; 
   // Should have 0 pages and documents 
   logDocument(doc); 
 
   // Add page 2 and 3 from the first child document (PDF) 
   var childDoc = 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) 
   var 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 = childDocs[1]; 
   doc.pages.add(childDoc.pages.item(0)); 
 
   // Should have 4 pages and 2 documents (the PDF and the TIFF) 
   log("Pages added"); 
   logDocument(doc); 
 
   // Save the ID so we can load it 
   var documentId = doc.documentId; 
 
   // Now save this 'parent' document into the cache 
   lt.Document.DocumentFactory.saveToCache(doc) 
      .done(function () { 
         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(function (doc) { 
               // Should have 5 pages and 2 documents (the PDF and the TIFF) 
               log("Document loaded from cache"); 
               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) 
               log("After removing first and last pages..."); 
               logDocument(doc); 
 
               // Delete this document from the cache 
               // Note: This does not delete the 'children' from the cache 
               lt.Document.DocumentFactory.deleteFromCache(documentId); 
            }) 
            .fail(showServiceError); 
      }) 
      .fail(showServiceError); 
} 
class DocumentFactory_CreateExample { 
    public constructor() { 
        lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt", "EVAL", null); 
    } 
 
    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(); 
      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(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) { 
                  log("All documents loaded", this.childDocs); 
                  this.createDocument(); 
               } 
            } 
            else { 
                this.loadDocument(); 
            } 
         }); 
    } 
 
    logDocument = (doc) => { 
        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) 
        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(() => { 
              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) 
                    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) 
                    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(showServiceError); 
           }) 
           .fail(showServiceError); 
     } 
} 

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