←Select platform

LEADDocument Class


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

public abstract class LEADDocument : IDisposable 
public [DataContractAttribute] 
   ref class Document abstract 
public abstract class Document 
class LEADDocument(IDisposable): 


The LEADDocument class provides uniform support for any type of document. The actual data behind it 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. LEADDocument encapsulates the common functionality needed to uniformly access this data with the same properties, methods, and data structures.

Document Viewer

LEADDocument is used as an input to the DocumentViewer, which can be used to view the document and its pages and includes thumbnail, virtualization, text search, and annotation support.

Document Converter

LEADDocument can also be used as an input to the DocumentConverter, to convert the document to any other file format, with or without using OCR technology.

Creating a Document Class

A LEADDocument instance can be obtained using any of the following methods:

Method Description
DocumentFactory.LoadFromFile Creates a new instance from an existing document file on disk or network share.
DocumentFactory.LoadFromUri Creates a new instance from a document stored at a remote URL.
DocumentFactory.LoadFromUriAsync Creates a new instance asynchronously from a document stored at a remote URL or disk.
DocumentFactory.LoadFromUriAsync Creates a new instance asynchronously from a document stored at 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 the internal LEADTOOLS object used with the document.


In most cases, the LEADDocument is ready to use after it has been obtained. However, some documents such as PDF can be encrypted and require a password before it can be parsed and used. Most of the properties and methods of LEADDocument 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 the 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 using the DocumentConverter class, which has more options and control.

Document Identifier

Each document has a unique identifier that is set at creation time by the framework. It is stored in the DocumentId property. The ID is important when using the document with the cache system. The ID is the only value needed to completely re-construct the document from the cache.

The document ID can be set manually by the user by calling the LoadDocumentOptions.DocumentId, CreateDocumentOptions.DocumentId, or UploadDocumentOptions.DocumentId options used when loading, creating or uploading the document. If DocumentId is left to null, the factory will generate a new random ID and associate it with the document using a DocumentFactory.NewCacheId.


Documents can contain many pages and huge amounts of data. Storing all this data in the physical memory is not feasible in most situations. Therefore, the LEADDocument class was designed to use an external caching system to store the modified data. Refer to DocumentFactory.Cache for more information.

HasCache determines if this document is using the cache system. Call SaveToCache to save a document to the cache and re-load it using DocumentFactory.LoadFromCache. AutoDeleteFromCache and AutoSaveToCache can be used to determine what happens to the cache data associated with the document when the document is disposed.

Structure and Table of Contents

DocumentStructure manages the structure of the document. This structure includes the bookmarks that represents the table of contents. Access it through the Structure property of LEADDocument.


DocumentPages manages the pages of the document. Access it through the Pages property of LEADDocument.

DocumentPages derives from LeadCollection<T> and thus can implement System.Collections.ObjectModel.Collection. 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 objects, each containing 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. DocumentPage contains functions to retrieve or update the raster or SVG image of the page, text data, annotations, and hyperlinks. Refer to DocumentPage for more information.


DocumentDocuments manages the child documents of the document. Access it through the Documents property of LEADDocument.

DocumentDocuments derives from LeadCollection<T> and thus can implement System.Collections.ObjectModel.Collection. You can use any of the collection methods to iterate through the documents. This collection is read-only, however, so 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 automatically gets updated to reflect which child documents are currently held in the document.

History Tracking

Document modification history and tracking can be enabled and accessed through the DocumentHistory object. It can be accessed through the History property of LEADDocument.


The metadata includes the 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 LEADDocument and contain 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's 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 temporary file.
  • IsReadOnly: Determines if the document is read-only and cannot be changed.
  • UserId: User ID or name associated with this document.
  • 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 LEADDocument class contains the following objects to manage global settings used throughout the document.

Document Units

LEADDocument uses independent units of 1/720 of an inch for all items. This value is stored in the UnitsPerInch constant (720). Refer to Document Library Coordinate System for more information.


LEADDocument implements System.IDisposable and must be disposed of 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 of if it was saved into the cache, (AutoSaveToCache was set to true, or SaveToCache was used).


This example loads a document and shows all its information.

using Leadtools; 
using Leadtools.Caching; 
using Leadtools.Document; 
public 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; 
   if(options.Cache == null) 
      options.Cache = DocumentFactory.Cache; 
   string documentId = null; 
   using (var document = DocumentFactory.LoadFromFile(Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"), options)) 
      document.IsReadOnly = false; 
      document.AutoDeleteFromCache = false; 
      // DocumentImages reference 
      document.Images.DefaultBitsPerPixel = 24; 
      // Check if the document has a stream in memory 
      if (document.HasStream) 
         // Get the document stream 
      // Indicate whether the document is using the cache or not 
      //Indicate if the document was downloaded 
      // Gets a value that determines whether the document structure is supported 
      // Output metadata values (DocumentMetadata reference) 
      // Get the Mime type of the document 
      // Parse document structure data (DocumentStructure reference) 
      foreach(DocumentBookmark bookmark in document.Structure.Bookmarks) 
         bookmark.Title = null; 
         bookmark.FontStyles = DocumentFontStyles.Normal; 
      // Get the document URI 
      // Get each DocumentPage object (DocumentPage & DocumentPages reference) 
      foreach (DocumentPage page in document.Pages) 
         // Get the page as a raster image at the specified resolution 
         // Get the page as an Svg with specified options 
         // Flip this page horizontally 
         // Use this method to add an array of links for this page 
         page.IsLinksModified = false; 
         page.Resolution = 0; 
         page.ViewPerspective = RasterViewPerspective.TopLeft; 
         Console.WriteLine($"Page Number: {page.PageNumber}, Original PageNumber: {page.OriginalPageNumber}, Size of the page: {page.Size}"); 
      documentId = document.DocumentId; 
   var loadFromCacheOptions = new LoadFromCacheOptions(); 
   loadFromCacheOptions.Cache = cache; 
   loadFromCacheOptions.DocumentId = documentId; 
   using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
      if (null == document) 
         Console.WriteLine("Cached document was expired and deleted!"); 
static class LEAD_VARS 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.Calendar; 
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.caching.*; 
import leadtools.document.*; 
public void documentExample() throws InterruptedException, IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   FileCache cache = getCache(); 
   CacheItemPolicy policy = new CacheItemPolicy(); 
   Calendar nowPlus = Calendar.getInstance(); 
   nowPlus.add(Calendar.SECOND, 1); 
   LoadDocumentOptions options = new LoadDocumentOptions(); 
   if (options.getCache() == null) { 
   LEADDocument document = DocumentFactory.loadFromFile(combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"), options); 
   // DocumentImages reference 
   // Check if the document has a stream in memory 
   if (document.hasStream()) { 
      // Get the document stream 
   // Indicate whether the document is using the cache or not 
   // Indicate if the document was downloaded 
   // Gets a value that determines whether the document structure is supported 
   // Output metadata values (DocumentMetadata reference) 
   // Get the Mime type of the document 
   // Parse document structure data (DocumentStructure reference) 
   for (DocumentBookmark bookmark : document.getStructure().getBookmarks()) { 
   // Get the document URI 
   // Get each DocumentPage object (DocumentPage & DocumentPages reference) 
   for (DocumentPage page : document.getPages()) { 
      assertTrue(document != null); 
      System.out.println("Document created successfully"); 
      // Get the page as a raster image at the specified resolution 
      // Get the page as an Svg with specified options 
      // Flip this page horizontally 
      // Use this method to add an array of links for this page 
      System.out.println("Page Number: " + page.getPageNumber() + ", Original PageNumber: " 
            + page.getOriginalPageNumber() + ", Size of the page: " + page.getSize() + ""); 


Target Platforms

See Also

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.