←Select platform

LoadDocumentFromCache Event

Summary

Called by the factory when loading child documents from the cache.

Syntax
C#
VB
C++
public event EventHandler<ResolveDocumentEventArgs> LoadDocumentFromCache 
Public Event LoadDocumentFromCache As EventHandler(Of ResolveDocumentEventArgs) 
public:  
   event EventHandler<ResolveDocumentEventArgs^>^ LoadDocumentFromCache 
Remarks

The LoadDocumentFromCache event is called during LoadFromCache(LoadFromCacheOptions) if the document being loaded contains child documents. The event is fired once for each child document and allow the application to customize this behavior.

Event Data
Parameter Type Description
sender object The source of the event
e ResolveDocumentEventArgs The event data

ResolveDocumentEventArgs contains the following members:

Input: The source (owner) document being loaded.

Input: The options to use when loading this child document. The handler can modify the properties of this member (for example, replace LoadFromCacheOptions.Cache with a different cache).

Output: The loaded child document. If the handler leaves this value as null, then the factory will attempt to load the document using the options set in LoadFromCacheOptions.

Example

This example will create a document from child documents, save it to the cache, and then re-load it. It will use the LoadDocumentFromCache event to manually load the child documents.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void LoadDocumentFromCacheTest() 
{ 
   // The cache we are using 
   FileCache cache = new FileCache(); 
   cache.PolicySerializationMode = CacheSerializationMode.Json; 
   cache.DataSerializationMode = CacheSerializationMode.Json; 
   cache.CacheDirectory = @"c:\cache-dir"; 
 
   // document IDs to use 
   const string virtualDocumentId = "virtual"; 
   const string childDocumentId1 = "child1"; 
   const string childDocumentId2 = "child2"; 
 
   // Create a new document 
   var createDocumentOptions = new CreateDocumentOptions(); 
   createDocumentOptions.Cache = cache; 
   createDocumentOptions.DocumentId = virtualDocumentId; 
 
   using (LEADDocument document = DocumentFactory.Create(createDocumentOptions)) 
   { 
      document.Name = "Virtual"; 
      Debug.Assert(virtualDocumentId == document.DocumentId); 
 
      // Should have 0 pages and documents 
      Debug.Assert(document.Pages.Count == 0); 
      Debug.Assert(document.Documents.Count == 0); 
 
      // Add page 1 and 2 from a PDF file 
      var loadDocumentOptions = new LoadDocumentOptions(); 
      loadDocumentOptions.Cache = cache; 
      loadDocumentOptions.DocumentId = childDocumentId1; 
      LEADDocument childDocument = DocumentFactory.LoadFromFile(@"C:\users\Public\Documents\LEADTOOLS Images\leadtools.pdf", loadDocumentOptions); 
      // Set the name 
      childDocument.Name = "Child1"; 
      childDocument.SaveToCache(); 
      // Now add the pages 
      document.Pages.Add(childDocument.Pages[0]); 
      document.Pages.Add(childDocument.Pages[1]); 
 
      // Add an empty page 
      DocumentPage 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 
      loadDocumentOptions = new LoadDocumentOptions(); 
      loadDocumentOptions.Cache = cache; 
      loadDocumentOptions.DocumentId = childDocumentId2; 
      childDocument = DocumentFactory.LoadFromFile(@"C:\users\Public\Documents\LEADTOOLS Images\ocr1-4.tif", loadDocumentOptions); 
      // Set the name 
      childDocument.Name = "Child2"; 
      // Also save it into the cache 
      childDocument.SaveToCache(); 
      // Now add the pages 
      document.Pages.Add(childDocument.Pages[2]); 
      document.Pages.Add(childDocument.Pages[3]); 
 
      // Should have 5 pages and 2 documents (the PDF and the TIF) 
      Debug.Assert(document.Pages.Count == 5); 
      Debug.Assert(document.Documents.Count == 2); 
 
      // Tell the parent document to dispose any child documents when the parent is disposed 
      document.AutoDisposeDocuments = true; 
 
      // Now save, the parent document into the cache 
      document.SaveToCache(); 
 
      // And tell all documents to not delete themselves from the cache 
      document.AutoDeleteFromCache = false; 
   } 
 
   // Hook to the DocumentFactory.LoadDocumentFromCache event to log the documents being loaded 
   EventHandler<ResolveDocumentEventArgs> loadDocumentFromCacheHandler = (sender, e) => 
   { 
      Console.WriteLine("Loading child document from cache:"); 
      Console.WriteLine($" Source DocumentId:{e.SourceDocument.DocumentId}"); 
      Console.WriteLine($" DocumentId to load{e.LoadFromCacheOptions.DocumentId}"); 
      // Source document must be the virtual virtualDocumentId 
      Debug.Assert(virtualDocumentId == e.SourceDocument.DocumentId); 
      // Child documents being loaded is either childDocumentId1 or childDocumentId2 
      Debug.Assert(childDocumentId1 == e.LoadFromCacheOptions.DocumentId || childDocumentId2 == e.LoadFromCacheOptions.DocumentId); 
      // If this is childDocumentId2, we will load it ourselves, and change its name to indicate it has been loaded by us 
      if (childDocumentId2 == e.LoadFromCacheOptions.DocumentId) 
      { 
         // Modify e.LoadFromCacheOptions if needed, or create a new instance and use it 
         e.Document = DocumentFactory.LoadFromCache(e.LoadFromCacheOptions); 
         e.Document.Name = "LoadedByEvent"; 
      } 
      // else, let the factory loaded it normally 
   }; 
   DocumentFactory.LoadDocumentFromCache += loadDocumentFromCacheHandler; 
 
   // Now, load the document from the cache 
   var loadFromCacheOptions = new LoadFromCacheOptions(); 
   loadFromCacheOptions.Cache = cache; 
   loadFromCacheOptions.DocumentId = virtualDocumentId; 
   using (LEADDocument document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
   { 
      Console.WriteLine($"Loaded virtual document id{document.DocumentId} name:{document.Name}"); 
      Debug.Assert(document.Name == "Virtual"); 
      // Should have 5 pages and 2 documents (the PDF and the TIF) 
      Debug.Assert(document.Pages.Count == 5); 
      Debug.Assert(document.Documents.Count == 2); 
 
      // Show the child document names, it should be Child1 and then LoadedByEvent 
      LEADDocument childDocument = document.Documents[0]; 
      Console.WriteLine($"First child document id{childDocument.DocumentId} name:{childDocument.Name}"); 
      Debug.Assert(childDocument.Name == "Child1"); 
      childDocument = document.Documents[1]; 
      Console.WriteLine($"Second child document id{childDocument.DocumentId} name:{childDocument.Name}"); 
      Debug.Assert(childDocument.Name == "LoadedByEvent"); 
   } 
 
   DocumentFactory.LoadDocumentFromCache -= loadDocumentFromCacheHandler; 
 
   // Done, delete all documents from the cache 
   bool isDeleted = DocumentFactory.DeleteFromCache(new LoadFromCacheOptions { Cache = cache, DocumentId = childDocumentId1 }); 
   Debug.Assert(isDeleted); 
   isDeleted = DocumentFactory.DeleteFromCache(new LoadFromCacheOptions { Cache = cache, DocumentId = childDocumentId2 }); 
   Debug.Assert(isDeleted); 
   isDeleted = DocumentFactory.DeleteFromCache(new LoadFromCacheOptions { Cache = cache, DocumentId = virtualDocumentId }); 
   Debug.Assert(isDeleted); 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Document.Writer 
Imports Leadtools.Svg 
Imports Leadtools.Document 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Engine 
Imports Leadtools.Barcode 
Imports Leadtools.Ocr 
Imports LeadtoolsDocumentExamples.LeadtoolsExamples.Common 
Imports Leadtools.Document.Converter 
 
Public Sub LoadDocumentFromCacheTest() 
   ' The cache we are using 
   Dim cache As New FileCache() 
   cache.PolicySerializationMode = CacheSerializationMode.Json 
   cache.DataSerializationMode = CacheSerializationMode.Json 
   cache.CacheDirectory = "c:\cache-dir" 
 
   ' document IDs to use 
   Const virtualDocumentId As String = "virtual" 
   Const childDocumentId1 As String = "child1" 
   Const childDocumentId2 As String = "child2" 
 
   ' Create a new document 
   Dim createDocumentOptions As New CreateDocumentOptions() 
   createDocumentOptions.Cache = cache 
   createDocumentOptions.DocumentId = virtualDocumentId 
 
   Using document As LEADDocument = DocumentFactory.Create(createDocumentOptions) 
      document.Name = "Virtual" 
      Debug.Assert(virtualDocumentId = document.DocumentId) 
 
      ' Should have 0 pages and documents 
      Debug.Assert(document.Pages.Count = 0) 
      Debug.Assert(document.Documents.Count = 0) 
 
      ' Add page 1 and 2 from a PDF file 
      Dim loadDocumentOptions As New LoadDocumentOptions() 
      loadDocumentOptions.Cache = cache 
      loadDocumentOptions.DocumentId = childDocumentId1 
      Dim childDocument As LEADDocument = DocumentFactory.LoadFromFile("C:\users\Public\Documents\LEADTOOLS Images\leadtools.pdf", loadDocumentOptions) 
      ' Set the name 
      childDocument.Name = "Child1" 
      childDocument.SaveToCache() 
      ' Now add the pages 
      document.Pages.Add(childDocument.Pages(0)) 
      document.Pages.Add(childDocument.Pages(1)) 
 
      ' Add an empty page 
      Dim documentPage As 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 
      loadDocumentOptions = New LoadDocumentOptions() 
      loadDocumentOptions.Cache = cache 
      loadDocumentOptions.DocumentId = childDocumentId2 
      childDocument = DocumentFactory.LoadFromFile("C:\users\Public\Documents\LEADTOOLS Images\ocr1-4.tif", loadDocumentOptions) 
      ' Set the name 
      childDocument.Name = "Child2" 
      ' Also save it into the cache 
      childDocument.SaveToCache() 
      ' Now add the pages 
      document.Pages.Add(childDocument.Pages(2)) 
      document.Pages.Add(childDocument.Pages(3)) 
 
      ' Should have 5 pages and 2 documents (the PDF and the TIF) 
      Debug.Assert(document.Pages.Count = 5) 
      Debug.Assert(document.Documents.Count = 2) 
 
      ' Tell the parent document to dispose any child documents when the parent is disposed 
      document.AutoDisposeDocuments = True 
 
      ' Now save, the parent document into the cache 
      document.SaveToCache() 
 
      ' And tell all documents to not delete themselves from the cache 
      document.AutoDeleteFromCache = False 
   End Using 
 
   ' Hook to the DocumentFactory.LoadDocumentFromCache event to log the documents being loaded 
   Dim loadDocumentFromCacheHandler As EventHandler(Of ResolveDocumentEventArgs) = 
      Sub(sender As Object, e As ResolveDocumentEventArgs) 
         Console.WriteLine("Loading child document from cache:") 
         Console.WriteLine($" Source DocumentId:{e.SourceDocument.DocumentId}") 
         Console.WriteLine($" DocumentId to load{e.LoadFromCacheOptions.DocumentId}") 
         ' Source document must be the virtual virtualDocumentId 
         Debug.Assert(virtualDocumentId = e.SourceDocument.DocumentId) 
         ' Child documents being loaded Is either childDocumentId1 Or childDocumentId2 
         Debug.Assert(childDocumentId1 = e.LoadFromCacheOptions.DocumentId OrElse childDocumentId2 = e.LoadFromCacheOptions.DocumentId) 
         ' If this Is childDocumentId2, we will load it ourselves, And change its name to indicate it has been loaded by us 
         If childDocumentId2 = e.LoadFromCacheOptions.DocumentId Then 
            ' Modify e.LoadFromCacheOptions if needed, Or create a New instance And use it 
            e.Document = DocumentFactory.LoadFromCache(e.LoadFromCacheOptions) 
            e.Document.Name = "LoadedByEvent" 
         End If 
         ' else, let the factory loaded it normally 
      End Sub 
 
   AddHandler DocumentFactory.LoadDocumentFromCache, loadDocumentFromCacheHandler 
 
   ' Now, load the document from the cache 
   Dim loadFromCacheOptions As New LoadFromCacheOptions() 
   loadFromCacheOptions.Cache = cache 
   loadFromCacheOptions.DocumentId = virtualDocumentId 
   Using document As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions) 
      Console.WriteLine($"Loaded virtual document id{document.DocumentId} name:{document.Name}") 
      Debug.Assert(document.Name = "Virtual") 
      ' Should have 5 pages and 2 documents (the PDF and the TIF) 
      Debug.Assert(document.Pages.Count = 5) 
      Debug.Assert(document.Documents.Count = 2) 
 
      ' Show the child document names, it should be Child1 and then LoadedByEvent 
      Dim childDocument As LEADDocument = document.Documents(0) 
      Console.WriteLine($"First child document id{childDocument.DocumentId} name:{childDocument.Name}") 
      Debug.Assert(childDocument.Name = "Child1") 
      childDocument = document.Documents(1) 
      Console.WriteLine($"Second child document id{childDocument.DocumentId} name:{childDocument.Name}") 
      Debug.Assert(childDocument.Name = "LoadedByEvent") 
   End Using 
 
   RemoveHandler DocumentFactory.LoadDocumentFromCache, loadDocumentFromCacheHandler 
 
   ' Done, delete all documents from the cache 
   Dim isDeleted As Boolean = DocumentFactory.DeleteFromCache(New LoadFromCacheOptions With {.Cache = cache, .DocumentId = childDocumentId1}) 
   Debug.Assert(isDeleted) 
   isDeleted = DocumentFactory.DeleteFromCache(New LoadFromCacheOptions With {.Cache = cache, .DocumentId = childDocumentId2}) 
   Debug.Assert(isDeleted) 
   isDeleted = DocumentFactory.DeleteFromCache(New LoadFromCacheOptions With {.Cache = cache, .DocumentId = virtualDocumentId}) 
   Debug.Assert(isDeleted) 
End Sub 
Requirements

Target Platforms

Help Version 20.0.2019.12.10
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly