Called by the factory when loading child documents from the cache.
public event EventHandler<ResolveDocumentEventArgs> LoadDocumentFromCache Public Event LoadDocumentFromCache As EventHandler(Of ResolveDocumentEventArgs) public:event EventHandler<ResolveDocumentEventArgs^>^ LoadDocumentFromCache
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.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 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:\users\Public\Documents\LEADTOOLS 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:\users\Public\Documents\LEADTOOLS 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);}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.Document.WriterImports Leadtools.SvgImports Leadtools.DocumentImports Leadtools.CachingImports Leadtools.Annotations.EngineImports Leadtools.BarcodeImports Leadtools.OcrImports LeadtoolsDocumentExamples.LeadtoolsExamples.CommonImports Leadtools.Document.ConverterPublic Sub LoadDocumentFromCacheTest()' The cache we are usingDim cache As New FileCache()cache.PolicySerializationMode = CacheSerializationMode.Jsoncache.DataSerializationMode = CacheSerializationMode.Jsoncache.CacheDirectory = "c:\cache-dir"' document IDs to useConst virtualDocumentId As String = "virtual"Const childDocumentId1 As String = "child1"Const childDocumentId2 As String = "child2"' Create a new documentDim createDocumentOptions As New CreateDocumentOptions()createDocumentOptions.Cache = cachecreateDocumentOptions.DocumentId = virtualDocumentIdUsing document As LEADDocument = 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 fileDim loadDocumentOptions As New LoadDocumentOptions()loadDocumentOptions.Cache = cacheloadDocumentOptions.DocumentId = childDocumentId1Dim childDocument As LEADDocument = DocumentFactory.LoadFromFile("C:\users\Public\Documents\LEADTOOLS 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 pageDim 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 fileloadDocumentOptions = New LoadDocumentOptions()loadDocumentOptions.Cache = cacheloadDocumentOptions.DocumentId = childDocumentId2childDocument = DocumentFactory.LoadFromFile("C:\users\Public\Documents\LEADTOOLS 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 = FalseEnd Using' Hook to the DocumentFactory.LoadDocumentFromCache event to log the documents being loadedDim 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 virtualDocumentIdDebug.Assert(virtualDocumentId = e.SourceDocument.DocumentId)' Child documents being loaded Is either childDocumentId1 Or childDocumentId2Debug.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 usIf childDocumentId2 = e.LoadFromCacheOptions.DocumentId Then' Modify e.LoadFromCacheOptions if needed, Or create a New instance And use ite.Document = DocumentFactory.LoadFromCache(e.LoadFromCacheOptions)e.Document.Name = "LoadedByEvent"End If' else, let the factory loaded it normallyEnd SubAddHandler DocumentFactory.LoadDocumentFromCache, loadDocumentFromCacheHandler' Now, load the document from the cacheDim loadFromCacheOptions As New LoadFromCacheOptions()loadFromCacheOptions.Cache = cacheloadFromCacheOptions.DocumentId = virtualDocumentIdUsing 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 LoadedByEventDim 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 UsingRemoveHandler DocumentFactory.LoadDocumentFromCache, loadDocumentFromCacheHandler' Done, delete all documents from the cacheDim 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
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
