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 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:\LEADTOOLS22\Resources\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:\LEADTOOLS22\Resources\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);
}