Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.4.3
|
Leadtools.Documents Namespace : Document Class |
[DataContractAttribute()] public abstract class Document
'Declaration
<DataContractAttribute()> Public MustInherit Class Document
'Usage
Dim instance As Document
public abstract class Document
[DataContractAttribute()] public ref class Document abstract
The Document class provides uniform support for any type of document. The actual data behind can be a PDF document, Microsoft Word document, TIFF image, an AutoCAD DWG drawing or any other of the hundreds of different raster, document or vector file formats supported by LEADTOOLS. Document encapsulates the common functionality needed to access this data in a uniform manner with the same properties, methods and data structures.
Leadtools.Documents is used as an input to DocumentViewer which can be used to view the document and its pages with thumbnails, virtualization, text search and annotation support.
Leadtools.Documents can also be used as an input to DocumentConverter to convert the document to any other file format with or without using OCR technology.
A Document instance be obtained using the following:
Method | Description |
---|---|
DocumentFactory.LoadFromFile |
Create a new instance from an existing document file on disk or network share. |
DocumentFactory.LoadFromUri |
Create a new instance from a document stored in a remote URL. |
DocumentFactory.LoadFromUriAsync |
Create a new instance asynchronously from a document stored in a remote URL or disk. |
DocumentFactory.LoadFromUriAsync |
Create a new instance asynchronously from a document stored in a remote URL. |
DocumentFactory.LoadFromStream |
Creates a new instance from an existing document stored in a stream. |
DocumentFactory.LoadFromCache |
Loads a previously saved document from the cache. |
DocumentFactory.Create |
Creates a new empty document. |
After the document is obtained, InternalObject will be to the internal LEADTOOLS object used with the document.
In most cases, the Document is ready to use after it has been obtained. However, some documents such as PDF can be encrypted and required a password before it can be parsed and used. Most of the properties and methods of Document will throw an error if the document has not been decrypted. IsEncrypted can be used to check if the document is encrypted and if so, Decrypt must be called with a password obtained from the user to unlock the document. When that happens, the value of IsDecrypted becomes true and the document is ready to be used. Note that IsEncrypted will stay true to indicate the original state of the document.
The SaveToFile and SaveToUri methods can be used to save the document to a disk file or remote URL. These methods support saving the document to a raster image format, not a document. In most cases, converting a document should be performed with more options and control using the DocumentConverter class.
Each document has a unique identifier that is set at creation time by the framework. This is stored in the DocumentId property.
The ID is important when using the document with the cache system and is the only value needed to re-construct completely the document from the cache.
Documents can contain large number of pages and huge amount of data. Storing all this data in the physical memory is not feasible in most situations. Therefore, the Document class was designed to use an external caching system to store the modified. Refer to DocumentFactory.Cache for more information.
HasCache determines if this document is using the cache system. SaveToCache can be used to save a document to the cache and re-loading it using DocumentFactory.LoadFromCache. AutoDeleteFromCache and AutoSaveToCache can be used to determine what happens to the cache data associated with the document when it is disposed.
DocumentStructure manages the structure of the document. This includes the bookmarks that represents the table of content. It can be accessed through the Structure property of Document.
DocumentPages manages the pages of the document. It can be accessed through the Pages property of Document.
DocumentPages derives from LeadCollection`1 and thus can implement Collection`1. You can use any of the collection methods to add, remove, insert, get, set and iterate through the pages.
DocumentPages contains a collection of DocumentPage that contains the data for a single page in the document. The page item is the main entry point for using the documents in a viewer or converter application. It contains functions to retrieve or update the raster or XVG image of the page, text data, annotations and hyperlinks. Refer to DocumentPage for more information.
The metadata includes default values added by the DocumentFactory when the document is loaded or created as well as any other data extracted from the document file itself, such as author, subject and any keywords stored by other applications.
The following properties are part of Document and contains useful information:
DocumentId: The unique identifier of this document.
Name: The name of this document.
DocumentType: The document type.
MimeType: The MIME type of the document.
Uri: The URL to the original document physical location.
IsDownloaded: Determines if the document was downloaded into the cache or a temp file.
IsReadOnly: Determines if the document is read-only and cannot be changed.
UserData: User-defined data associated with this document.
GetDocumentFileName: Gets the path to the file holding the original document.
GetAnnotationsFileName: Gets the path to the file holding the original annotations.
FileLength: The length of the original document file or URL in bytes.
The Document class contains the following to manage global settings used throughout the document.
Images: Manages the raster and SVG image settings of the document.
RasterCodecs: The RasterCodecs object to use when loading and saving RasterImage and SvgDocument objects.
Annotations: Manages the annotations settings of the document.
Text: Manages the text and OCR recognition settings of the document.
Barcodes: Manages the barcode settings of the document.
Document uses independent units of 1/720 of an inch for all items. This value is stored in UnitsPerInch constant (720). Refer to Documents Library Coordinate System for more information.
Document implements IDisposable and must be disposed after it has been used. Refer to IDisposable in .NET for more information.
The document can be re-constructed as is after it has been disposed it is saved into the cache if AutoSaveToCache was set to true or if SaveToCache was used.
This example will load a document and shows all its information.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms.DocumentWriters Imports Leadtools.Svg Imports Leadtools.Documents Imports Leadtools.Caching Imports Leadtools.Annotations.Core Imports Leadtools.Barcode Imports Leadtools.Forms.Ocr Public Shared Function CreateCache() As FileCache Dim cacheDir As String = Path.Combine(ImagesPath.Path, "cache") If Directory.Exists(cacheDir) Then Directory.Delete(cacheDir, True) End If Directory.CreateDirectory(cacheDir) Dim cache As New FileCache() cache.CacheDirectory = cacheDir Return cache End Function Public Shared Sub PrintOutDocumentInfo(ByVal document As Document) Console.WriteLine("General") Console.WriteLine(" DocumentId:" & document.DocumentId) If document.Uri <> Nothing Then Console.WriteLine(" Uri:" & document.Uri.ToString()) End If Console.WriteLine(" IsDownloaded:" & document.IsDownloaded) Console.WriteLine(" DocumentFileName:" & document.GetDocumentFileName()) Console.WriteLine(" IsReadOnly:" & document.IsReadOnly) Console.WriteLine(" MimeType:" & document.MimeType) Console.WriteLine(" IsEncrypted:" & document.IsEncrypted) Console.WriteLine(" IsDecrypted:" & document.IsDecrypted) Console.WriteLine("Cache") Console.WriteLine(" HasCache:" & document.HasCache) Console.WriteLine(" AutoDeleteFromCache:" & document.AutoDeleteFromCache) If document.HasCache Then Dim policy As CacheItemPolicy = document.CreateCacheItemPolicy() Console.WriteLine(" CacheItemPolicy:{0} {1}", policy.AbsoluteExpiration, policy.SlidingExpiration) Else Console.WriteLine(" CacheItemPolicy: none") End If Console.WriteLine("Metadata") For Each item As KeyValuePair(Of String, String) In document.Metadata Console.WriteLine(" {0} {1}", item.Key, item.Value) Next item Console.WriteLine("Annotations") If document.Annotations.AnnotationsUri <> Nothing Then Console.WriteLine(" AnnotationsUri:" & document.Annotations.AnnotationsUri.ToString()) End If Console.WriteLine("Images") Console.WriteLine(" DefaultBitsPerPixel:{0}", document.Images.DefaultBitsPerPixel) Console.WriteLine(" ThumbnailPixelSize:{0}", document.Images.ThumbnailPixelSize) Console.WriteLine("Pages") Console.WriteLine(" DefaultPageSize:{0}", document.Pages.DefaultPageSize) Console.WriteLine(" DefaultResolution:{0}", document.Pages.DefaultResolution) Console.WriteLine(" Count:" & document.Pages.Count) For Each page As Leadtools.Documents.DocumentPage In document.Pages Console.WriteLine(" PageNumber:" & page.PageNumber) Console.WriteLine(" OriginalPageNumber:" & page.OriginalPageNumber) Console.WriteLine(" Size:{0}", page.Size) Console.WriteLine(" Resolution:{0}", page.Resolution) Console.WriteLine(" IsDeleted:" & page.IsDeleted) Console.WriteLine(" IsImageModified:" & page.IsImageModified) Using rasterImage As RasterImage = page.GetImage() If Not rasterImage Is Nothing Then Console.WriteLine("Image {0} by {1} {2}bpp", rasterImage.Width, rasterImage.Height, rasterImage.BitsPerPixel) Else Console.WriteLine("null") End If End Using Using thumbnail As RasterImage = page.GetThumbnailImage() If Not thumbnail Is Nothing Then Console.WriteLine("Thumbnail {0} by {1} {2}bpp", thumbnail.Width, thumbnail.Height, thumbnail.BitsPerPixel) Else Console.WriteLine("null") End If End Using Console.WriteLine(" Annotations") Console.WriteLine(" IsAnnotationsModified:" & page.IsAnnotationsModified) Console.WriteLine(" HasEmbeddedAnnotations:" & page.HasEmbeddedAnnotations) Dim container As AnnContainer = page.GetAnnotations(False) If Not container Is Nothing Then Console.WriteLine(" Size:" & container.Size.ToString()) Console.WriteLine(" Objects:" & container.Children.Count) For Each child As AnnObject In container.Children Console.WriteLine(" FriendlyName:" & child.FriendlyName) Console.WriteLine(" Bounds:" & child.Bounds.ToString()) Next child Else Console.WriteLine(" NULL") End If Next page Console.WriteLine("--------") End Sub <TestMethod()> _ Public Sub DocumentExample() DocumentFactory.Cache = CreateCache() Dim policy As CacheItemPolicy = New CacheItemPolicy() policy.AbsoluteExpiration = DateTime.Now + New TimeSpan(0, 0, 1) policy.SlidingExpiration = New TimeSpan(0, 0, 1) Dim options As LoadDocumentOptions = New LoadDocumentOptions() options.CachePolicy = policy Dim documentId As String = Nothing Using document As Document = DocumentFactory.LoadFromFile(Path.Combine(ImagesPath.Path, "Leadtools.pdf"), options) document.AutoDeleteFromCache = False PrintOutDocumentInfo(document) documentId = document.DocumentId document.SaveToCache() End Using System.Threading.Thread.Sleep(2000) Using document As Document = DocumentFactory.LoadFromCache(documentId) If document Is Nothing Then Console.WriteLine("Cached document was expired and deleted!") End If End Using End Sub
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms.DocumentWriters; using Leadtools.Svg; using Leadtools.Documents; using Leadtools.Caching; using Leadtools.Annotations.Core; using Leadtools.Forms.Ocr; using Leadtools.Barcode; public static FileCache CreateCache() { var cacheDir = Path.Combine(ImagesPath.Path, "cache"); if (Directory.Exists(cacheDir)) Directory.Delete(cacheDir, true); Directory.CreateDirectory(cacheDir); var cache = new FileCache(); cache.CacheDirectory = cacheDir; return cache; } public static void PrintOutDocumentInfo(Document document) { Console.WriteLine("General"); Console.WriteLine(" DocumentId:" + document.DocumentId); Console.WriteLine(" Uri:" + document.Uri); Console.WriteLine(" IsDownloaded:" + document.IsDownloaded); Console.WriteLine(" DocumentFileName:" + document.GetDocumentFileName()); Console.WriteLine(" IsReadOnly:" + document.IsReadOnly); Console.WriteLine(" MimeType:" + document.MimeType); Console.WriteLine(" IsEncrypted:" + document.IsEncrypted); Console.WriteLine(" IsDecrypted:" + document.IsDecrypted); Console.WriteLine(" UserData:" + document.UserData); Console.WriteLine("Cache"); Console.WriteLine(" HasCache:" + document.HasCache); Console.WriteLine(" AutoDeleteFromCache:" + document.AutoDeleteFromCache); if (document.HasCache) { var policy = document.CreateCacheItemPolicy(); Console.WriteLine(" CacheItemPolicy:{0} {1}", policy.AbsoluteExpiration, policy.SlidingExpiration); } else { Console.WriteLine(" CacheItemPolicy: none"); } Console.WriteLine("Metadata"); foreach (var item in document.Metadata) Console.WriteLine(" {0} {1}", item.Key, item.Value); Console.WriteLine("Annotations"); Console.WriteLine(" AnnotationsUri:" + document.Annotations.AnnotationsUri); Console.WriteLine("Images"); Console.WriteLine(" DefaultBitsPerPixel:{0}", document.Images.DefaultBitsPerPixel); Console.WriteLine(" ThumbnailPixelSize:{0}", document.Images.ThumbnailPixelSize); Console.WriteLine("Pages"); Console.WriteLine(" DefaultPageSize:{0}", document.Pages.DefaultPageSize); Console.WriteLine(" DefaultResolution:{0}", document.Pages.DefaultResolution); Console.WriteLine(" Count:" + document.Pages.Count); foreach (var page in document.Pages) { Console.WriteLine(" PageNumber:" + page.PageNumber); Console.WriteLine(" OriginalPageNumber:" + page.OriginalPageNumber); Console.WriteLine(" Size:{0}", page.Size); Console.WriteLine(" Resolution:{0}", page.Resolution); Console.WriteLine(" UserData:" + page.UserData); Console.WriteLine(" IsDeleted:" + page.IsDeleted); Console.WriteLine(" IsImageModified:" + page.IsImageModified); using (var rasterImage = page.GetImage()) { if (rasterImage != null) Console.WriteLine("Image {0} by {1} {2}bpp", rasterImage.Width, rasterImage.Height, rasterImage.BitsPerPixel); else Console.WriteLine("null"); } using (var thumbnail = page.GetThumbnailImage()) { if (thumbnail != null) Console.WriteLine("Thumbnail {0} by {1} {2}bpp", thumbnail.Width, thumbnail.Height, thumbnail.BitsPerPixel); else Console.WriteLine("null"); } Console.WriteLine(" Annotations"); Console.WriteLine(" IsAnnotationsModified:" + page.IsAnnotationsModified); Console.WriteLine(" HasEmbeddedAnnotations:" + page.HasEmbeddedAnnotations); var container = page.GetAnnotations(false); if (container != null) { Console.WriteLine(" Size:" + container.Size); Console.WriteLine(" Objects:" + container.Children.Count); foreach (var child in container.Children) { Console.WriteLine(" FriendlyName:" + child.FriendlyName); Console.WriteLine(" Bounds:" + child.Bounds); } } else { Console.WriteLine(" NULL"); } } Console.WriteLine("--------"); } [TestMethod] public void DocumentExample() { DocumentFactory.Cache = CreateCache(); var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.Now + new TimeSpan(0, 0, 1); policy.SlidingExpiration = new TimeSpan(0, 0, 1); var options = new LoadDocumentOptions(); options.CachePolicy = policy; string documentId = null; using (var document = DocumentFactory.LoadFromFile(Path.Combine(ImagesPath.Path, "Leadtools.pdf"), options)) { document.AutoDeleteFromCache = false; PrintOutDocumentInfo(document); documentId = document.DocumentId; document.SaveToCache(); } System.Threading.Thread.Sleep(2000); using (var document = DocumentFactory.LoadFromCache(documentId)) { if (null == document) { Console.WriteLine("Cached document was expired and deleted!"); } } }
Document Members
Leadtools.Documents Namespace
Documents Library Features
Loading Using LEADTOOLS Documents Library
Documents Library Coordinate System
Loading Encrypted Files Using the Documents Library
Parsing Text with the Documents Library
Barcode processing with the Documents Library
Using LEADTOOLS Document Viewer
Using LEADTOOLS Document Converters