←Select platform

LEADDocument Class

Summary

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

Syntax
C#
C++/CLI
Java
Python
[DataContractAttribute()] 
public abstract class LEADDocument : IDisposable 
public [DataContractAttribute] 
   ref class Document abstract 
public abstract class Document 
class LEADDocument(IDisposable): 

Remarks

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.

Encryption

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.

Caching

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.

Pages

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.

Documents

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.

Metadata

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.

Properties

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.

Disposing

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

Example

This example loads a document and shows all its information.

C#
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.GetDocumentFileName(); 
      document.IsReadOnly = false; 
      document.AutoDeleteFromCache = false; 
      // DocumentImages reference 
      document.Images.DefaultBitsPerPixel = 24; 
 
      Console.WriteLine(document.Images.IsResolutionsSupported); 
      Console.WriteLine(document.Images.IsSvgSupported); 
 
      // Check if the document has a stream in memory 
      if (document.HasStream) 
      { 
         // Get the document stream 
         document.GetDocumentStream(); 
      } 
 
      // Indicate whether the document is using the cache or not 
      Console.WriteLine(document.HasCache); 
             
      //Indicate if the document was downloaded 
      Console.WriteLine(document.IsDownloaded); 
 
      // Gets a value that determines whether the document structure is supported 
      Console.WriteLine(document.IsStructureSupported); 
 
      // Output metadata values (DocumentMetadata reference) 
      Console.WriteLine(document.Metadata.Values.Count); 
 
      // Get the Mime type of the document 
      Console.WriteLine(document.MimeType); 
 
      // Parse document structure data (DocumentStructure reference) 
      foreach(DocumentBookmark bookmark in document.Structure.Bookmarks) 
      { 
         bookmark.Title = null; 
         bookmark.FontStyles = DocumentFontStyles.Normal; 
         document.Structure.Bookmarks.Add(bookmark); 
         Console.WriteLine(bookmark.Children); 
         Console.WriteLine(bookmark.Target); 
         Console.WriteLine(document.Structure.Bookmarks.Count); 
         Console.WriteLine(document.Structure.IsParsed); 
         Console.WriteLine(document.Structure.ParseBookmarks); 
      } 
 
      document.Structure.Parse(); 
 
      // Get the document URI 
      Console.WriteLine(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 
         page.GetImage(0); 
         // Get the page as an Svg with specified options 
         page.GetSvg(null); 
         // Flip this page horizontally 
         page.Reverse(); 
         // Use this method to add an array of links for this page 
         page.SetLinks(null); 
         page.IsLinksModified = false; 
         page.Resolution = 0; 
         page.ViewPerspective = RasterViewPerspective.TopLeft; 
         page.SetLinks(page.GetLinks()); 
         Console.WriteLine($"Page Number: {page.PageNumber}, Original PageNumber: {page.OriginalPageNumber}, Size of the page: {page.Size}"); 
      } 
 
      PrintOutDocumentInfo(document); 
 
      documentId = document.DocumentId; 
      document.SaveToCache(); 
   } 
 
   System.Threading.Thread.Sleep(2000); 
 
   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:\LEADTOOLS22\Resources\Images"; 
} 

Requirements

Target Platforms

See Also

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

Leadtools.Document Assembly

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