Represents a collection of LEADDocument objects.
DocumentDocuments manages the children of the document. It can be accessed through the Documents property of LEADDocument.
DocumentDocuments derives from LeadCollection. You can use any of the collection methods to iterate through the documents. This collection is read-only however and you cannot add, remove or change the items. Instead, use Pages to add or remove pages that belong to a separate document to this one. The LEADDocument.Documents collection will automatically gets updated to reflect what child documents are currently held in the document.
Here is an example that shows what happens to LEADDocument.Documents when pages are added and removed:
When document (virtualDocument
is created, LEADDocument.Documents is an empty collection
The user creates a new empty page using CreatePage of virtualDocument
and then adds it to the
LEADDocument.Pages collection. The LEADDocument.Documents collection will still be an empty
collection since the page original owner is the same document (in other words, DocumentPage.Document is virtualDocument
The user add a page from another document (document1
) into LEADDocument.Pages of virtualDocument
.
The LEADDocument.Documents collection will contain a single item: document1
The user add another page from document1
into LEADDocument.Pages of virtualDocument
.
The LEADDocument.Documents collection will still contain the single item document1
since both pages has the same owner
The user add a page from document2
into LEADDocument.Pages of virtualDocument
.
The LEADDocument.Documents collection will contain two items document1
and document2
The user removes this last page from virtualDocument
. The LEADDocument.Documents collection will
be containing the single item document1
The user removes all the pages from virtualDocument
. The LEADDocument.Documents collection will
be to being empty
When DocumentFactory.SaveToCache is called, the document will store the IDs of all the child documents.
When DocumentFactory.LoadFromCache is called, the document will try to re-load all the child documents and automatically set them in this collection. If any document fails to load or is no longer available in the cache, then all its pages are removed automatically and are nor loaded.
When the document is disposed, the value of AutoDisposeDocuments is examined, and if its true, then all the child documents are disposed as well.
import { DocumentHelper } from "../../utilities/DocumentHelper";
export class DocumentFactory_CreateExample {
public constructor() {
lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null);
DocumentHelper.initFactory();
}
private urls = [
"https://demo.leadtools.com/images/pdf/leadtools.pdf",
"https://demo.leadtools.com/images/tiff/barcodes.tif"
];
private childDocs = [];
private childDocCount = this.urls.length;
private processedCount = 0;
private loadedCount = 0;
public run = (buttonID: string) => {
const exampleButton = document.getElementById(buttonID);
exampleButton.onclick = this.loadDocument;
}
loadDocument = () => {
// Load this one
const index = this.processedCount;
const loadOptions = new lt.Document.LoadDocumentOptions();
DocumentHelper.log("Loading " + this.urls[index]);
lt.Document.DocumentFactory.loadFromUri(this.urls[index], loadOptions)
.done((childDoc) => {
// Finished, save the document and load next
this.loadedCount++;
this.childDocs.push(childDoc);
})
.fail(DocumentHelper.showServiceError)
.always(() => {
this.processedCount++;
if (this.processedCount >= this.childDocCount) {
// All children loaded, if we did not have an error continue with the example
if (this.loadedCount === this.processedCount) {
DocumentHelper.log("All documents loaded", this.childDocs);
this.createDocument();
}
}
else {
this.loadDocument();
}
});
}
logDocument = (doc) => DocumentHelper.log("Name: " + doc.name + " Pages: " + doc.pages.count + " Documents: " + doc.documents.count, doc);
createDocument = () => {
// Create a new document
const createOptions = new lt.Document.CreateDocumentOptions();
const doc = lt.Document.DocumentFactory.create(createOptions);
doc.name = "Virtual";
// Should have 0 pages and documents
this.logDocument(doc);
// Add page 2 and 3 from the first child document (PDF)
let childDoc = this.childDocs[0];
doc.pages.add(childDoc.pages.item(2));
doc.pages.add(childDoc.pages.item(3));
// Add an empty page (8.5 by 11 inches at 300 DPI)
const docPage = doc.pages.createPage(lt.LeadSizeD.create(lt.Document.LEADDocument.unitsPerInch * 8.5, lt.Document.LEADDocument.unitsPerInch * 11), 300);
doc.pages.add(docPage);
// Add page 1 from the second child document (TIFF)
childDoc = this.childDocs[1];
doc.pages.add(childDoc.pages.item(0));
// Should have 4 pages and 2 documents (the PDF and the TIFF)
DocumentHelper.log("Pages added");
this.logDocument(doc);
// Save the ID so we can load it
const documentId = doc.documentId;
// Now save this 'parent' document into the cache
lt.Document.DocumentFactory.saveToCache(doc)
.done(() => {
DocumentHelper.log("Document saved to cache at " + documentId)
// Tell this document to dispose all children
doc.autoDisposeDocuments = true;
// And dispose it
doc.dispose();
// Now, re-load this document from the cache
lt.Document.DocumentFactory.loadFromCache(documentId)
.done((doc) => {
// Should have 5 pages and 2 documents (the PDF and the TIFF)
DocumentHelper.log("Document loaded from cache");
this.logDocument(doc);
// Delete first page
doc.pages.removeAt(0);
// Delete the last page
doc.pages.removeAt(doc.pages.count - 1);
// Should have 3 pages and 1 document (the PDF only)
DocumentHelper.log("After removing first and last pages...");
this.logDocument(doc);
// Delete this document from the cache
// Note: This does not delete the 'children' from the cache
lt.Document.DocumentFactory.deleteFromCache(documentId);
})
.fail(DocumentHelper.showServiceError);
})
.fail(DocumentHelper.showServiceError);
}
}