saveAttachmentToCache Method

Summary

Saves an attachment to the cache.

Syntax
TypeScript
JavaScript
saveAttachmentToCache = function( 
   ownerDocument, 
   ownerDocumentId, 
   options 
) 
static saveAttachmentToCache( 
   ownerDocument: LEADDocument, 
   ownerDocumentId: string, 
   options: SaveAttachmentToCacheOptions 
): JQueryPromise<string>; 

Parameters

ownerDocument

Owner document. If this is null, then the owner document is specified by

ownerDocumentId.

ownerDocumentId

Owner document ID. If this is null, then the owner document is specified by ownerDocument.

options

Options to identify and save the attachment. This value cannot be null.

Return Value

A Promise object that can resolve successfully to a string object identifying the attachment document ID, or fail if the operation was not successful.

Remarks

Use saveAttachmentToCache to save an attachment to the service cache. The method can be used if the owner document is already loaded as a LEADDocument object (by passing it as the ownerDocument parameter). It can also be used to save an attachment if the owner document is not loaded, and only its ID is available (by passing it as the ownerDocumentId parameter).

One of ownerDocument or ownerDocumentId must be specified but not both.

This method works as follows:

  1. If and only if using ownerDocumentId: The owner document is loaded from the cache using loadFromCache. If loading is not successful, then null is returned.

  2. The corresponding DocumentAttachment object for SaveAttachmentToCacheOptions.attachmentNumber of options is obtained.

If loading is not successful, then an error is thrown.

  1. If the value of DocumentAttachment.documentId is not null, it is assumed that the attachment document is already in the cache and therefore this value is returned directly.

If this value is null or checking if the document in cache fails, then the workflow continues to the next section.

  1. If the attachment is not embedded (the value of DocumentAttachment.isEmbedded is false), then null is returned.

  2. If the attachment is embedded (the value of DocumentAttachment.isEmbedded is true), then the attachment data is obtained; and if successful, saved (uploaded) into the cache using the options set in SaveAttachmentToCacheOptions.uploadDocumentOptions of options. The ID of this new document is returned.

For more information, refer to Document Attachments.

Example
Main.ts
DocumentHelper.ts
Main.js
DocumentHelper.js
Main.html
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
import { DocumentFactory_CheckCacheInfoExample } from "../Factory/CheckCacheInfo"; 
 
export class DocumentAttachment_MainExample { 
   private el: HTMLElement; 
 
   public constructor() { 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
      DocumentHelper.initFactory(); 
   } 
 
   private urlToPdfFile = "http://localhost/images/pdf/portfolio/port1.pdf"; 
 
   public run(buttonID: string) { 
      document.getElementById(buttonID).onclick = this.onRun; 
   } 
 
   onRun = () => { 
      /* 
      The PDF document can be one of the following: 
      1. Normal PDF without attachments. Has one or more pages and no attachments. 
      2. Normal PDF with attachments. Has one or more pages and one or more attachments. 
      3. PDF Portfolio. Has a placeholder page and one or more attachments. 
 
      The value of LoadDocumentOptions.loadAttachmentsMode affects loading the documents as follows: 
 
      1. Normal PDF without attachments: Will not be affected. 
      2. Normal PDF with attachments: Number of pages is not affected. 
         Number of attachments is: 
            LoadAttachmentsMode = none then 0 
            LoadAttachmentsMode = asAttachments then number of attachments found in the document. 
      3. PDF Portfolio: Number of pages is: 
            LoadAttachmentsMode = none then 1 (the placeholder page) 
            LoadAttachmentsMode = asAttachments then 0 (the document has no pages). 
         Number of attachments is: 
            LoadAttachmentsMode = none then 0 
            LoadAttachmentsMode = asAttachments then number of attachments found in the document. 
      */ 
     const pdfUrl = this.urlToPdfFile; // URL to a PDF file as an input to this example 
      // First, load the document without attachments 
      console.log("Loading with DocumentLoadAttachmentsMode.none"); 
      const loadDocumentOptions = new lt.Document.LoadDocumentOptions(); 
      loadDocumentOptions.loadAttachmentsMode = lt.Document.DocumentLoadAttachmentsMode.none; 
      this.loadDocument(pdfUrl, loadDocumentOptions, (doc) => { 
         console.log("Document has " + doc.pages.count + " pages"); 
 
         // Find out if the document is portfolio 
         const isPortfolioValue = doc.metadata[lt.Document.LEADDocument.metadataKey_IsPortfolio]; 
         const isPortfolio = isPortfolioValue && isPortfolioValue.toLowerCase() === "true"; 
         console.log("isPortfolio:" + isPortfolio); 
 
         // Check that everything is as expected 
 
         // We should have 0 attachments since we did not instruct the toolkit to load these 
         if (doc.attachments.count != 0) throw new Error("Something went wrong"); 
 
         // We should have one or more pages regardless of whether the document is PDF portfolio 
         if (doc.pages.count === 0) throw new Error("Something went wrong"); 
 
         // Next, load the document with attachments 
         console.log("Loading with DocumentLoadAttachmentsMode.asAttachments"); 
         loadDocumentOptions.loadAttachmentsMode = lt.Document.DocumentLoadAttachmentsMode.asAttachments; 
         this.loadDocument(pdfUrl, loadDocumentOptions, (doc) => { 
            console.log("Document has " + doc.pages.count + " pages"); 
 
            // Find out if the document is portfolio 
            const isPortfolioValue = doc.metadata[lt.Document.LEADDocument.metadataKey_IsPortfolio]; 
            const isPortfolio = isPortfolioValue && isPortfolioValue.toLowerCase() === "true"; 
            console.log("isPortfolio:" + isPortfolio); 
 
            // Check that everything is as expected 
 
            // We may have 0 or more attachments depending on the file 
            console.log("Document has " + doc.attachments.count + " attachments"); 
 
            // We should have one or more pages unless this is PDF portfolio, then it will have 0 pages 
            if (isPortfolio) { 
               if (doc.pages.count !== 0) throw new Error("Something went wrong"); 
            } 
 
            // Extract all the embedded attachments into the cache 
            this.extractAttachments(doc, 0, () => { 
               console.log("Example done"); 
            }); 
         }); 
      }); 
   } 
 
   loadDocument = (url: string, loadDocumentOptions: lt.Document.LoadDocumentOptions, done: (doc: lt.Document.LEADDocument) => void) => { 
      DocumentHelper.log("Loading document " + url); 
      lt.Document.DocumentFactory.loadFromUri(url, loadDocumentOptions) 
      .done((doc) => { 
         DocumentHelper.log("Document loaded and has cache id: " + doc.documentId); 
         done(doc); 
      }) 
      .fail(DocumentHelper.showServiceError); 
   } 
 
   extractAttachments = (doc: lt.Document.LEADDocument, attachmentIndex: number, done: () => void) => { 
      if (attachmentIndex >= doc.attachments.count) { 
         done(); 
         return; 
      } 
 
      // Show info on this attachment 
      const attachment = doc.attachments.item(attachmentIndex); 
      console.log("Attachment number " + attachment.AttachmentNumber + "file name is '" + attachment.fileName +"' and length is " + attachment.fileLength + " bytes"); 
 
      // Save it into the cache 
      // Another option is to load it directly using lt.Document.DocumentFactory.loadDocumentAttachment to load it as a LEAD document 
      const saveAttachmentToCacheOptions = new lt.Document.SaveAttachmentToCacheOptions(); 
      saveAttachmentToCacheOptions.attachmentNumber = attachmentIndex + 1; 
      lt.Document.DocumentFactory.saveAttachmentToCache(doc, null, saveAttachmentToCacheOptions) 
         .done((attachmentDocumentId) => { 
            console.log("attachment " + saveAttachmentToCacheOptions.attachmentNumber + " saved into the cache with document ID = " + attachmentDocumentId); 
 
            // The attachment data can be downloaded using lt.Document.DocumentFactory.downloadDocumentData or loaded using DocumentFactory.loadFromCache 
 
            this.extractAttachments(doc, attachmentIndex + 1, done); 
         }) 
         .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"; 
import { DocumentFactory_CheckCacheInfoExample } from "../Factory/CheckCacheInfo"; 
 
export class DocumentAttachment_MainExample { 
   constructor() { 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
      DocumentHelper.initFactory(); 
   } 
 
   urlToPdfFile = "http://localhost/images/pdf/portfolio/port1.pdf"; 
 
   run = (buttonID) => { 
      document.getElementById(buttonID).onclick = this.onRun; 
   } 
 
   onRun = () => { 
      /* 
      The PDF document can be one of the following: 
      1. Normal PDF without attachments. Has one or more pages and no attachments. 
      2. Normal PDF with attachments. Has one or more pages and one or more attachments. 
      3. PDF Portfolio. Has a placeholder page and one or more attachments. 
 
      The value of LoadDocumentOptions.loadAttachmentsMode affects loading the documents as follows: 
 
      1. Normal PDF without attachments: Will not be affected. 
      2. Normal PDF with attachments: Number of pages is not affected. 
         Number of attachments is: 
            LoadAttachmentsMode = none then 0 
            LoadAttachmentsMode = asAttachments then number of attachments found in the document. 
      3. PDF Portfolio: Number of pages is: 
            LoadAttachmentsMode = none then 1 (the placeholder page) 
            LoadAttachmentsMode = asAttachments then 0 (the document has no pages). 
         Number of attachments is: 
            LoadAttachmentsMode = none then 0 
            LoadAttachmentsMode = asAttachments then number of attachments found in the document. 
      */ 
     const pdfUrl = this.urlToPdfFile; // URL to a PDF file as an input to this example 
      // First, load the document without attachments 
      console.log("Loading with DocumentLoadAttachmentsMode.none"); 
      const loadDocumentOptions = new lt.Document.LoadDocumentOptions(); 
      loadDocumentOptions.loadAttachmentsMode = lt.Document.DocumentLoadAttachmentsMode.none; 
      this.loadDocument(pdfUrl, loadDocumentOptions, (doc) => { 
         console.log("Document has " + doc.pages.count + " pages"); 
 
         // Find out if the document is portfolio 
         const isPortfolioValue = doc.metadata[lt.Document.LEADDocument.metadataKey_IsPortfolio]; 
         const isPortfolio = isPortfolioValue && isPortfolioValue.toLowerCase() === "true"; 
         console.log("isPortfolio:" + isPortfolio); 
 
         // Check that everything is as expected 
 
         // We should have 0 attachments since we did not instruct the toolkit to load these 
         if (doc.attachments.count != 0) throw new Error("Something went wrong"); 
 
         // We should have one or more pages regardless of whether the document is PDF portfolio 
         if (doc.pages.count === 0) throw new Error("Something went wrong"); 
 
         // Next, load the document with attachments 
         console.log("Loading with DocumentLoadAttachmentsMode.asAttachments"); 
         loadDocumentOptions.loadAttachmentsMode = lt.Document.DocumentLoadAttachmentsMode.asAttachments; 
         this.loadDocument(pdfUrl, loadDocumentOptions, (doc) => { 
            console.log("Document has " + doc.pages.count + " pages"); 
 
            // Find out if the document is portfolio 
            const isPortfolioValue = doc.metadata[lt.Document.LEADDocument.metadataKey_IsPortfolio]; 
            const isPortfolio = isPortfolioValue && isPortfolioValue.toLowerCase() === "true"; 
            console.log("isPortfolio:" + isPortfolio); 
 
            // Check that everything is as expected 
 
            // We may have 0 or more attachments depending on the file 
            console.log("Document has " + doc.attachments.count + " attachments"); 
 
            // We should have one or more pages unless this is PDF portfolio, then it will have 0 pages 
            if (isPortfolio) { 
               if (doc.pages.count !== 0) throw new Error("Something went wrong"); 
            } 
 
            // Extract all the embedded attachments into the cache 
            this.extractAttachments(doc, 0, () => { 
               console.log("Example done"); 
            }); 
         }); 
      }); 
   } 
 
   loadDocument = (url, loadDocumentOptions, done) => { 
      DocumentHelper.log("Loading document " + url); 
      lt.Document.DocumentFactory.loadFromUri(url, loadDocumentOptions) 
      .done((doc) => { 
         DocumentHelper.log("Document loaded and has cache id: " + doc.documentId); 
         done(doc); 
      }) 
      .fail(DocumentHelper.showServiceError); 
   } 
 
   extractAttachments = (doc, attachmentIndex, done) => { 
      if (attachmentIndex >= doc.attachments.count) { 
         done(); 
         return; 
      } 
 
      // Show info on this attachment 
      const attachment = doc.attachments.item(attachmentIndex); 
      console.log("Attachment number " + attachment.AttachmentNumber + "file name is '" + attachment.fileName +"' and length is " + attachment.fileLength + " bytes"); 
 
      // Save it into the cache 
      // Another option is to load it directly using lt.Document.DocumentFactory.loadDocumentAttachment to load it as a LEAD document 
      const saveAttachmentToCacheOptions = new lt.Document.SaveAttachmentToCacheOptions(); 
      saveAttachmentToCacheOptions.attachmentNumber = attachmentIndex + 1; 
      lt.Document.DocumentFactory.saveAttachmentToCache(doc, null, saveAttachmentToCacheOptions) 
         .done((attachmentDocumentId) => { 
            console.log("attachment " + saveAttachmentToCacheOptions.attachmentNumber + " saved into the cache with document ID = " + attachmentDocumentId); 
 
            // The attachment data can be downloaded using lt.Document.DocumentFactory.downloadDocumentData or loaded using DocumentFactory.loadFromCache 
 
            this.extractAttachments(doc, attachmentIndex + 1, done); 
         }) 
         .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 | DocumentAttachment</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.DocumentAttachment.Main(); 
      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.