←Select platform

Document Class

Summary

Encapsulates a multi-page document with support for raster and SVG images, bookmarks, annotations and text data.

Syntax

C#
VB
Java
C++
[DataContractAttribute()] 
public abstract class Document 
<DataContractAttribute()>  
Public MustInherit Class Document 
public [DataContractAttribute] 
   ref class Document abstract 
public abstract class Document

Remarks

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.

Document Viewer

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.

Document Converter

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.

Creating a Document Class

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.

Encryption

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.

Saving a Document Class

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.

Document Identifier

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. The document ID can be set manually by the user through the LoadDocumentOptions.DocumentId, CreateDocumentOptions.DocumentId or UploadDocumentOptions.DocumentId options used when loading, creating or uploading the document. If the value was left to null, then the factory will generate a new random ID and associated it with the document using a GUID generator.

Caching

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.

Structure and Table of Content

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.

Pages

DocumentPages manages the pages of the document. It can be accessed through the Pages property of Document.

DocumentPages derives from LeadCollection<T> and thus can implement [System.Collections.ObjectModel.Collection1](https://msdn.microsoft.com/en-us/library/System.Collections.ObjectModel.Collection1.aspx). 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.

Documents

DocumentDocuments manages the child documents of the document. It can be accessed theough the Documents property of Document.

DocumentDocuments derives from LeadCollection<T> and this can implement [System.Collections.ObjectModel.Collection1](https://msdn.microsoft.com/en-us/library/System.Collections.ObjectModel.Collection1.aspx). 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 Document.Documents collection will automatically gets updated to reflect what child documents are currently held in the document.

Metadata

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.

Properties

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. If this is a newly created document then Uri will be null.

  • CacheUri: The URL to the original document's image data if it was stored in the cache.

  • 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.

  • GetDocumentStream: Gets a stream to the original data of the document.

  • GetAnnotationsFileName: Gets the path to the file holding the original annotations.

  • GetAnnotationsStream: Gets a stream to the original annotations.

  • FileLength: The length of the original document file or URL in bytes.

  • CacheStatus: The status of this document in the cache.

Access to the original document data depends on how the document was created and its cache status, as follows:

Global Document Settings

The Document class contains the following to manage global settings used throughout the document.

Document Units

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.

Disposing

Document implements System.IDisposable and must be disposed after it has been used. Refer to System.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.

Example

This example will load a document and shows all its information.

C#
VB
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 ObjectCache GetCache() 
{ 
   // Create a LEADTOOLS FileCache object 
   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); 
   if (document.Uri != null) 
      Console.WriteLine("  Uri:" + document.Uri); 
   Console.WriteLine("  Name:" + document.Name); 
   Console.WriteLine("  CacheStatus:" + document.CacheStatus); 
   Console.WriteLine("  LastCacheSyncTime:" + document.LastCacheSyncTime); 
   Console.WriteLine("  IsReadOnly:" + document.IsReadOnly); 
   Console.WriteLine("  IsLocal:" + document.IsLocal); 
   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); 
   Console.WriteLine("Metadata"); 
   foreach (var item in document.Metadata) 
      Console.WriteLine("  {0} {1}", item.Key, item.Value); 
 
   Console.WriteLine("Documents"); 
   Console.WriteLine("  Count:" + document.Documents.Count); 
   foreach (var childDocument in document.Documents) 
   { 
      Console.WriteLine("    Name:" + childDocument.Name); 
   } 
 
   Console.WriteLine("Pages"); 
   Console.WriteLine("  Count:" + document.Pages.Count); 
 
   for (var pageNumber = 1; pageNumber <= document.Pages.Count; pageNumber++) 
   { 
      var page = document.Pages[pageNumber - 1]; 
 
      Console.WriteLine("    PageNumber:" + pageNumber); 
      Console.WriteLine("      OriginalPageNumber:" + page.OriginalPageNumber); 
      Console.WriteLine("      OriginalDocumentName:" + page.Document.Name); 
      Console.WriteLine("      Size:{0}", page.Size); 
   } 
 
   Console.WriteLine("--------"); 
} 
 
       
public static void DocumentExample() 
{ 
   var cache = GetCache(); 
 
   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; 
   options.Cache = cache; 
 
   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(cache, documentId)) 
   { 
      if (null == document) 
      { 
         Console.WriteLine("Cached document was expired and deleted!"); 
      } 
   } 
} 
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 GetCache() As ObjectCache 
   ' Create a LEADTOOLS FileCache object 
   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 Not IsNothing(document.Uri) Then 
      Console.WriteLine("  Uri:" + document.Uri.ToString()) 
   Else 
      Console.WriteLine("  Name:" + document.Name) 
   End If 
 
   Console.WriteLine("  CacheStatus:" + document.CacheStatus.ToString()) 
   Console.WriteLine("  LastCacheSyncTime:" + document.LastCacheSyncTime.ToString()) 
   Console.WriteLine("  IsReadOnly:" + document.IsReadOnly.ToString()) 
   Console.WriteLine("  IsLocal:" + document.IsLocal.ToString()) 
   Console.WriteLine("  MimeType:" + document.MimeType) 
   Console.WriteLine("  IsEncrypted:" + document.IsEncrypted.ToString()) 
   Console.WriteLine("  IsDecrypted:" + document.IsDecrypted.ToString()) 
   If Not IsNothing(document.UserData) Then 
      Console.WriteLine("  UserData:" + document.UserData.ToString()) 
   End If 
   Console.WriteLine("Cache") 
   Console.WriteLine("  HasCache:" + document.HasCache.ToString()) 
   Console.WriteLine("  AutoDeleteFromCache:" + document.AutoDeleteFromCache.ToString()) 
   Console.WriteLine("Metadata") 
   For Each item As KeyValuePair(Of String, String) In document.Metadata 
      Console.WriteLine("  {0} {1}", item.Key, item.Value) 
   Next 
 
   Console.WriteLine("Documents") 
   Console.WriteLine("  Count:" + document.Documents.Count.ToString()) 
   For Each childDocument As Document In document.Documents 
      Console.WriteLine("    Name:" + childDocument.Name) 
   Next 
 
   Console.WriteLine("Pages") 
   Console.WriteLine("  Count:" + document.Pages.Count.ToString()) 
 
   For pageNumber As Integer = 1 To document.Pages.Count 
      Dim page As Leadtools.Documents.DocumentPage = document.Pages(pageNumber - 1) 
 
      Console.WriteLine("    PageNumber:" + pageNumber.ToString()) 
      Console.WriteLine("      OriginalPageNumber:" + page.OriginalPageNumber.ToString()) 
      Console.WriteLine("      OriginalDocumentName:" + page.Document.Name) 
      Console.WriteLine("      Size:{0}", page.Size.ToString()) 
   Next 
 
   Console.WriteLine("--------") 
End Sub 
 
<TestMethod()> 
Public Shared Sub DocumentExample() 
   Dim cache As ObjectCache = GetCache() 
 
   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 
   options.Cache = cache 
 
   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(cache, documentId) 
      If document Is Nothing Then 
         Console.WriteLine("Cached document was expired and deleted!") 
      End If 
   End Using 
End Sub 

Requirements

Target Platforms

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
Leadtools.Documents Assembly
Click or drag to resize