Error processing SSI file
LEADTOOLS Leadtools.Documents (Leadtools.Documents assembly)

Show in webframe

Document Class






Members 
Encapsulates a multi-page document with support for raster and SVG images, bookmarks, annotations and text data.
Object Model
Syntax
[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 
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

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.

Document Converter

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.

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.

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

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:

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

Example

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

Copy Code  
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!");
      }
   }
}
Requirements

Target Platforms

See Also

Reference

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

Error processing SSI file
   Leadtools.Documents requires a Document or Medical toolkit license and unlock key. For more information, refer to: LEADTOOLS Toolkit Features