Called by the factory when loading child documents from the cache.
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.
| 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.
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.
using Leadtools;using Leadtools.Codecs;using Leadtools.Document.Writer;using Leadtools.Document;using Leadtools.Caching;using Leadtools.Annotations.Engine;using Leadtools.Ocr;using Leadtools.Barcode;using Leadtools.Document.Converter;public void LoadDocumentFromCache(){// The cache we are usingFileCache cache = new FileCache();cache.PolicySerializationMode = CacheSerializationMode.Json;cache.DataSerializationMode = CacheSerializationMode.Json;cache.CacheDirectory = @"c:\cache-dir";// document IDs to useconst string virtualDocumentId = "virtual";const string childDocumentId1 = "child1";const string childDocumentId2 = "child2";// Create a new documentvar 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 documentsDebug.Assert(document.Pages.Count == 0);Debug.Assert(document.Documents.Count == 0);// Add page 1 and 2 from a PDF filevar loadDocumentOptions = new LoadDocumentOptions();loadDocumentOptions.Cache = cache;loadDocumentOptions.DocumentId = childDocumentId1;LEADDocument childDocument = DocumentFactory.LoadFromFile(@"C:\LEADTOOLS22\Resources\Images\leadtools.pdf", loadDocumentOptions);// Set the namechildDocument.Name = "Child1";childDocument.SaveToCache();// Now add the pagesdocument.Pages.Add(childDocument.Pages[0]);document.Pages.Add(childDocument.Pages[1]);// Add an empty pageDocumentPage 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 fileloadDocumentOptions = new LoadDocumentOptions();loadDocumentOptions.Cache = cache;loadDocumentOptions.DocumentId = childDocumentId2;childDocument = DocumentFactory.LoadFromFile(@"C:\LEADTOOLS22\Resources\Images\ocr1-4.tif", loadDocumentOptions);// Set the namechildDocument.Name = "Child2";// Also save it into the cachechildDocument.SaveToCache();// Now add the pagesdocument.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 disposeddocument.AutoDisposeDocuments = true;// Now save, the parent document into the cachedocument.SaveToCache();// And tell all documents to not delete themselves from the cachedocument.AutoDeleteFromCache = false;}// Hook to the DocumentFactory.LoadDocumentFromCache event to log the documents being loadedEventHandler<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 virtualDocumentIdDebug.Assert(virtualDocumentId == e.SourceDocument.DocumentId);// Child documents being loaded is either childDocumentId1 or childDocumentId2Debug.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 usif (childDocumentId2 == e.LoadFromCacheOptions.DocumentId){// Modify e.LoadFromCacheOptions if needed, or create a new instance and use ite.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 cachevar 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 LoadedByEventLEADDocument 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 cachebool 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);}