←Select platform

Descriptor Property

Summary

The JSON serializable object that describes a document.

Syntax
C#
C++/CLI
Java
Python
public DocumentDescriptor Descriptor { get; set; } 
public:  
   property DocumentDescriptor^ Descriptor 
   { 
      DocumentDescriptor^ get() 
      void set(DocumentDescriptor^ value) 
   } 
public DocumentDescriptor getDescriptor() 
public void setDescriptor(DocumentDescriptor value) 
Descriptor # get and set (CreateDocumentOptions) 

Property Value

JSON serializable object that describes a document.

Remarks

DocumentDescriptor is a class that holds JSON data that describes a document. This data can be obtained from any document using LEADDocument.CreateDocumentDescriptor and the document can then be disposed. At a later time, DocumentFactory.Create can be called with this descriptor object set in CreateDocumentOptions.Descriptor to regenerate the original document. LEADDocument.UpdateFromDocumentDescriptor can be used to update an existing document from a descriptor.

This technique is used by LEADTOOLS Document Web Service as follows:

  1. The user creates a new document using JavaScript.

  2. The document is manipulated at the JavaScript side, for example, pages are added or removed and the document could be set in the document viewer for display

  3. During this whole process, the document exists only on the client side - it is not save to the server cache.

  4. When ready, the user calls DocumentFactory.saveToCache from the JavaScript code. Here, the JavaScript toolkit library will create a DocumentDescriptor object and send it to the Documents service method SaveToCache

  5. The service will try first to load the document from the cache, if it does not exist (when this is the first call to save this document), it will then use DocumentFactory.Create with CreateDocumentOptions.Descriptor set to the object passed into the request by JavaScript. If the document exists, then LEADDocument.UpdateFromDocumentDescriptor is called to update the document object from the JavaScript object in case it has changed (subsequent calls to LEADDocument.SaveToCache for this document).

  6. Finally, LEADDocument.SaveToCache is called to save the updated document into the cache

DocumentId is not used if Descriptor is not null. Instead, the document creation data along with its ID is stored in the DocumentDescriptor object set in the method.

Example
C#
Java
using Leadtools; 
using Leadtools.Caching; 
using Leadtools.Document; 
 
 
public void DocumentFactoryCreateExample() 
{ 
   var cache = GetCache(); 
 
   // Create a new document 
   var createOptions = new CreateDocumentOptions(); 
   createOptions.Cache = cache; 
   createOptions.Descriptor = null; 
   createOptions.MimeType = null; 
   createOptions.UseCache = true; 
   createOptions.UserId = null; 
 
   Console.WriteLine("Cache Policy: {0}", createOptions.CachePolicy.AbsoluteExpiration); 
 
   string documentId = null; 
 
   using (LEADDocument document = DocumentFactory.Create(createOptions)) 
   { 
      document.Name = "Virtual"; 
 
      // Should have 0 pages and documents 
      System.Diagnostics.Debug.Assert(document.Pages.Count == 0); 
      System.Diagnostics.Debug.Assert(document.Documents.Count == 0); // DocumentDocuments reference 
 
      // Add page 1 and 2 from a PDF file 
      LoadDocumentOptions loadOptions = new LoadDocumentOptions(); 
      loadOptions.Cache = cache; 
      LEADDocument childDocument = DocumentFactory.LoadFromFile(Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"), loadOptions); 
      // Do not dispose the child documents, but save it into the cache 
      // This is optional and is done in this example since we will try to re-load the parent document 
      // from the cache - and the child documents should be in the cache as well 
      childDocument.SaveToCache(); 
      // Now add the pages 
      document.Pages.Add(childDocument.Pages[0]); 
      document.Pages.Add(childDocument.Pages[1]); 
 
      // Add an empty page 
      var documentPage = document.Pages.CreatePage(LeadSizeD.Create(LEADDocument.UnitsPerInch * 8.5, LEADDocument.UnitsPerInch * 11), 300); 
      document.Pages.Add(documentPage); 
 
      // Add page 3 and 4 from a TIF file 
      childDocument = DocumentFactory.LoadFromFile(Path.Combine(LEAD_VARS.ImagesDir, "ocr1.tif"), loadOptions); 
      // Also save it into the cache 
      childDocument.SaveToCache(); 
      // Now add the pages 
      document.Pages.Add(childDocument.Pages[0]); 
      document.Pages.Add(childDocument.Pages[0]); 
 
      // Should have 5 pages and 2 documents (the PDF and the TIF) 
      System.Diagnostics.Debug.Assert(document.Pages.Count == 5); 
      System.Diagnostics.Debug.Assert(document.Documents.Count == 2); 
 
      // Tell the parent document to dispose any child documents when the parent is disposed 
      document.AutoDisposeDocuments = true; 
 
      // Show the info of this document, should say 5 pages 
      Console.WriteLine("Original document information"); 
      PrintOutDocumentInfo(document); 
 
      // Now save, the parent document into the cache 
      document.SaveToCache(); 
 
      // And tell all documents to not delete themselves from the cache 
      document.AutoDeleteFromCache = false; 
 
      // Save the ID so we can load it 
      documentId = document.DocumentId; 
   } 
 
   // Now, load the document from the cache 
   var loadFromCacheOptions = new LoadFromCacheOptions(); 
   loadFromCacheOptions.Cache = cache; 
   loadFromCacheOptions.DocumentId = documentId; 
   using (LEADDocument document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
   { 
      // Should have 5 pages and 2 documents (the PDF and the TIF) 
      System.Diagnostics.Debug.Assert(document.Pages.Count == 5); 
      System.Diagnostics.Debug.Assert(document.Documents.Count == 2); 
 
      // Show the info of this document, should still say 5 pages 
      Console.WriteLine("Loaded from cache information"); 
      PrintOutDocumentInfo(document); 
 
      // Delete first page 
      document.Pages.RemoveAt(0); 
      // Delete the last page 
      document.Pages.RemoveAt(document.Pages.Count - 1); 
 
      // Should have 3 pages and 2 documents (the PDF and the TIF) 
      System.Diagnostics.Debug.Assert(document.Pages.Count == 3); 
      System.Diagnostics.Debug.Assert(document.Documents.Count == 2); 
 
      Console.WriteLine("After removing the first 2 pages"); 
      PrintOutDocumentInfo(document); 
 
      // Delete this document and all its children from the cache when we are disposed 
      document.AutoDeleteFromCache = true; 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.Calendar; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.caching.*; 
import leadtools.document.*; 
 
 
public void documentFactoryCreateExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   FileCache cache = getCache(); 
 
   // Create a new document 
   CreateDocumentOptions createOptions = new CreateDocumentOptions(); 
   createOptions.setCache(cache); 
   createOptions.setDescriptor(null); 
   createOptions.setMimeType(null); 
   createOptions.setUseCache(true); 
 
   System.out.println("Cache Policy: " + createOptions.getCachePolicy().getAbsoluteExpiration()); 
 
   String documentId = null; 
 
   LEADDocument document = DocumentFactory.create(createOptions); 
 
   document.setName("Virtual"); 
 
   // Should have 0 pages and documents 
   assertTrue(document.getPages().size() == 0); 
 
   // Add page 1 and 2 from a PDF file 
   LoadDocumentOptions loadOptions = new LoadDocumentOptions(); 
   loadOptions.setCache(cache); 
   LEADDocument childDocument = DocumentFactory.loadFromFile(combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"), 
         loadOptions); 
   // Do not dispose the child documents, but save it into the cache 
   // This is optional and is done in this example since we will try to re-load the 
   // parent document 
   // from the cache - and the child documents should be in the cache as well 
   childDocument.saveToCache(); 
 
   // Now add the pages 
   document.getPages().add(childDocument.getPages().get(0)); 
   document.getPages().add(childDocument.getPages().get(1)); 
 
   // Add an empty page 
   DocumentPage documentPage = document.getPages() 
         .createPage(LeadSizeD.create(LEADDocument.UNITS_PER_INCH * 8.5, LEADDocument.UNITS_PER_INCH * 11), 300); 
   document.getPages().add(documentPage); 
 
   // Add page 3 and 4 from a TIF file 
   childDocument = DocumentFactory.loadFromFile(combine(LEAD_VARS_IMAGES_DIR, "ocr1.tif"), loadOptions); 
   // Also save it into the cache 
   childDocument.saveToCache(); 
   // Now add the pages 
   document.getPages().add(childDocument.getPages().get(0)); 
   document.getPages().add(childDocument.getPages().get(0)); 
 
   // Should have 5 pages and 2 documents (the PDF and the TIF) 
   assertTrue(document.getPages().size() == 5); 
   assertTrue(document.getDocuments().size() == 2); 
 
   // Tell the parent document to dispose any child documents when the parent is 
   // disposed 
   document.setAutoDisposeDocuments(false); 
 
   // Show the info of this document, should say 5 pages 
   System.out.println("Original document information"); 
   printOutDocumentInfo(document); 
 
   // Now save, the parent document into the cache 
   document.saveToCache(); 
 
   // And tell all documents to not delete themselves from the cache 
   document.setAutoDeleteFromCache(false); 
   // Save the ID so we can load it 
   document.dispose(); 
   documentId = document.getDocumentId(); 
   System.out.println("Docu: " + documentId); 
 
   // Now, load the document from the cache 
   LoadFromCacheOptions loadFromCacheOptions = new LoadFromCacheOptions(); 
   loadFromCacheOptions.setCache(cache); 
   loadFromCacheOptions.setDocumentId(documentId); 
   System.out.println("Load: " + loadFromCacheOptions.getDocumentId()); 
   document = DocumentFactory.loadFromCache(loadFromCacheOptions); 
 
   // Should have 5 pages and 2 documents (the PDF and the TIF) 
   assertTrue(document.getPages().size() == 5); 
   assertTrue(document.getDocuments().size() == 2); 
 
   // Show the info of this document, should still say 5 pages 
   System.out.println("Loaded from cache information"); 
   printOutDocumentInfo(document); 
 
   // Delete first page 
   document.getPages().remove(0); 
   document.getPages().remove(document.getPages().size() - 1); 
   // Delete the last page 
   // Should have 3 pages and 2 documents (the PDF and the TIF) 
   assertTrue(document.getPages().size() == 3); 
   assertTrue(document.getDocuments().size() == 2); 
 
   System.out.println("After removing the first 2 pages"); 
   printOutDocumentInfo(document); 
 
   // Delete this document and all its children from the cache when we are disposed 
   document.setAutoDeleteFromCache(true); 
   document.dispose(); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
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.