Document Property
Summary
Loaded child document.
Syntax
Property Value
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
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);
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
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.annotations.engine.*;
import leadtools.barcode.*;
import leadtools.caching.*;
import leadtools.codecs.*;
import leadtools.document.*;
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler;
import leadtools.document.converter.*;
import leadtools.document.writer.*;
import leadtools.ocr.*;
public void loadDocumentFromCacheExample() {
// The cache we are using
FileCache cache = new FileCache();
cache.setPolicySerializationMode(CacheSerializationMode.JSON);
cache.setDataSerializationMode(CacheSerializationMode.JSON);
cache.setCacheDirectory("c:\\cache-dir");
// document IDs to use
final String virtualDocumentId = "virtual";
final String childDocumentId1 = "child1";
final String childDocumentId2 = "child2";
// Create a new document
CreateDocumentOptions createDocumentOptions = new CreateDocumentOptions();
createDocumentOptions.setCache(cache);
createDocumentOptions.setDocumentId(virtualDocumentId);
LEADDocument document = DocumentFactory.create(createDocumentOptions);
document.setName("Virtual");
assertTrue(virtualDocumentId == document.getDocumentId());
// Should have 0 pages and documents
assertTrue(document.getPages().size() == 0);
assertTrue(document.getDocuments().size() == 0);
// Add page 1 and 2 from a PDF file
LoadDocumentOptions loadDocumentOptions = new LoadDocumentOptions();
loadDocumentOptions.setCache(cache);
loadDocumentOptions.setDocumentId(childDocumentId1);
LEADDocument childDocument = DocumentFactory.loadFromFile("C:\\LEADTOOLS23\\Resources\\Images\\leadtools.pdf",
loadDocumentOptions);
// Set the name
childDocument.setName("Child1");
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
loadDocumentOptions = new LoadDocumentOptions();
loadDocumentOptions.setCache(cache);
loadDocumentOptions.setDocumentId(childDocumentId2);
childDocument = DocumentFactory.loadFromFile("C:\\LEADTOOLS23\\Resources\\Images\\ocr1-4.tif",
loadDocumentOptions);
// Set the name
childDocument.setName("Child2");
// Also save it into the cache
childDocument.saveToCache();
// Now add the pages
document.getPages().add(childDocument.getPages().get(2));
document.getPages().add(childDocument.getPages().get(3));
// 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(true);
// Now save, the parent document into the cache
document.saveToCache();
// And tell all documents to not delete themselves from the cache
document.setAutoDeleteFromCache(false);
ResolveDocumentEventListener loadDocumentFromCacheHandler = new ResolveDocumentEventListener() {
@Override
public void onEvent(ResolveDocumentEvent e) {
System.out.println("Loading child document from cache: ");
System.out.println(" Source DocumentId: " + e.getSourceDocument().getDocumentId());
System.out.println(" DocumentId to load " + e.getLoadFromCacheOptions().getDocumentId());
// Source document must be the virtual virtualDocumentId
assertTrue(virtualDocumentId.equals(e.getSourceDocument().getDocumentId()));
// Child documents being loaded is either childDocumentId1 or childDocumentId2
assertTrue(childDocumentId1.equals(e.getLoadFromCacheOptions().getDocumentId())
|| childDocumentId2.equals(e.getLoadFromCacheOptions().getDocumentId()));
// If this is childDocumentId2, we will load it ourselves, and change its name
// to indicate it has been loaded by us
if (childDocumentId2.equals(e.getLoadFromCacheOptions().getDocumentId())) {
// Modify e.LoadFromCacheOptions if needed, or create a new instance and use
// it
e.setDocument(DocumentFactory.loadFromCache(e.getLoadFromCacheOptions()));
e.getDocument().setName("LoadedByEvent");
}
// // else, let the factory loaded it normally
}
};
// Hook to the DocumentFactory.LoadDocumentFromCache event to log the documents
// being loaded
DocumentFactory.addLoadDocumentFromCacheListener(loadDocumentFromCacheHandler);
// Now, load the document from the cache
LoadFromCacheOptions loadFromCacheOptions = new LoadFromCacheOptions();
// Load it and show its history
loadFromCacheOptions.setCache(cache);
loadFromCacheOptions.setDocumentId(virtualDocumentId);
document = DocumentFactory.loadFromCache(loadFromCacheOptions);
System.out.println("Loaded virtual document id " + document.getDocumentId() + " name:" + document.getName());
assertTrue(document.getName().equals("Virtual"));
// Should have 5 pages and 2 documents (the PDF and the TIF)
assertTrue(document.getPages().size() == 5);
assertTrue(document.getDocuments().size() == 2);
// Show the child document names, it should be Child1 and then LoadedByEvent
childDocument = document.getDocuments().get(0);
System.out
.println("First child document id " + childDocument.getDocumentId() + " name:" + childDocument.getName());
assertTrue(childDocument.getName().equals("Child1"));
childDocument = document.getDocuments().get(1);
System.out
.println("Second child document id " + childDocument.getDocumentId() + " name: " + childDocument.getName());
assertTrue(childDocument.getName().equals("LoadedByEvent"));
DocumentFactory.removeLoadDocumentFromCacheListener(loadDocumentFromCacheHandler);
// Done, delete all documents from the cache
LoadFromCacheOptions loadOptions = new LoadFromCacheOptions();
loadOptions.setCache(cache);
loadOptions.setDocumentId(childDocumentId1);
boolean isDeleted = DocumentFactory.deleteFromCache(loadOptions);
assertTrue(isDeleted);
loadOptions.setDocumentId(childDocumentId2);
isDeleted = DocumentFactory.deleteFromCache(loadOptions);
assertTrue(isDeleted);
loadOptions.setDocumentId(virtualDocumentId);
isDeleted = DocumentFactory.deleteFromCache(loadOptions);
assertTrue(isDeleted);
}