saveToCache Method (original) (raw)

Summary

Saves the document to the cache.

Syntax

saveToCache = function( _doc_ )

Parameters

doc

The document to save. Must not be null.

Return Value

A Promise object that may resolve successfully to a LEADDocumentobject, or fail if the LEADDocument cannot be saved to the cache.

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>