←Select platform

DownloadDocument(ObjectCache,string,long,int,Stream) Method

Summary

Downloads the data of the specified document ID into a stream.

Syntax
C#
VB
C++
public static int DownloadDocument( 
   ObjectCache cache, 
   string documentId, 
   long offset, 
   int bytes, 
   Stream stream 
) 
Public Shared Function DownloadDocument( 
   ByVal cache As ObjectCache, 
   ByVal documentId As String, 
   ByVal offset As Long, 
   ByVal bytes As Integer, 
   ByVal stream As Stream 
) As Integer 
public:  
   static Int32 DownloadDocument( 
      ObjectCache^ cache, 
      String^ documentId, 
      Int64 offset, 
      Int32 bytes, 
      Stream^ stream 
   ) 

Parameters

cache

The cache containing the document. This value cannot be null.

documentId

ID of the document to download.

offset

0-based byte offset into the source data at which to begin reading the data.

bytes

The maximum number of bytes to read. If this value is -1, then the remaining data will be read till the end is reached.

stream

Stream to read the data into. This should be a write-able stream and cannot be null.

Return Value

The total number of bytes read into stream. This can be less than the number of bytes requested by bytes, if that many bytes are not currently available, or zero (0) if the end of the data has been reached.

Remarks

Similar to UploadDocument, data can be downloaded in chunks or all at once. Refer to Uploading Using the Document Library for detailed information on how to use these methods and the various options used.

GetDocumentCacheInfo can be used to determine if a document is in the cache and to get its information.

DownloadAnnotations can be used to download the annotations of a document (if any).

Example

This example will download the data of a document in the cache.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public static void DownloadDocumentExample() 
{ 
   var documentUri = new Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf"); 
   string documentFile1 = Path.Combine(ImagesPath.Path, "downloaded1.pdf"); 
   string documentFile2 = Path.Combine(ImagesPath.Path, "downloaded2.pdf"); 
 
   // Setup a cache 
   FileCache cache = new FileCache(); 
   cache.CacheDirectory = @"c:\cache-dir"; 
 
   string documentId; 
 
   // Load a document into the cache 
   var loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.Cache = cache; 
   using (var document = DocumentFactory.LoadFromUri(documentUri, loadDocumentOptions)) 
   { 
      // Get its document ID and save it 
      documentId = document.DocumentId; 
      document.AutoDeleteFromCache = false; 
      document.AutoSaveToCache = false; 
      document.SaveToCache(); 
   } 
 
   // Now download it all at once into a memory stream 
   var downloadDocumentOptions = new DownloadDocumentOptions(); 
   downloadDocumentOptions.Cache = cache; 
   downloadDocumentOptions.DocumentId = documentId; 
   downloadDocumentOptions.Offset = 0; 
   downloadDocumentOptions.Length = -1; 
   using (var stream = new MemoryStream()) 
   { 
      downloadDocumentOptions.Stream = stream; 
      long bytes = DocumentFactory.DownloadDocument(downloadDocumentOptions); 
      Console.WriteLine("Downloaded {0} bytes into the stream", bytes); 
 
      // Save the stream to a file and show it 
      using (var output = File.Create(documentFile1)) 
         stream.WriteTo(output); 
 
      Process.Start(documentFile1); 
   } 
 
   // Download it again, this time we will buffer it 32K at a time 
   downloadDocumentOptions = new DownloadDocumentOptions(); 
   downloadDocumentOptions.Cache = cache; 
   downloadDocumentOptions.DocumentId = documentId; 
   byte[] buffer = new byte[1024 * 32]; 
   using (var output = File.Create(documentFile2)) 
   { 
      // Offset to where we are: 
      long offset = 0; 
 
      int bytes; 
      do 
      { 
         downloadDocumentOptions.Offset = offset; 
         downloadDocumentOptions.Length = buffer.Length; 
         downloadDocumentOptions.Data = buffer; 
         downloadDocumentOptions.DataOffset = 0; 
         bytes = (int)DocumentFactory.DownloadDocument(downloadDocumentOptions); 
         if (bytes > 0) 
         { 
            Console.WriteLine("Downloaded {0} bytes into the buffer", bytes); 
            // Next chunk 
            offset += bytes; 
            output.Write(buffer, 0, bytes); 
         } 
      } 
      while (bytes > 0); 
   } 
 
   Process.Start(documentFile2); 
 
   // We are done, clean up 
   var deleteFromCacheOptions = new LoadFromCacheOptions(); 
   deleteFromCacheOptions.Cache = cache; 
   deleteFromCacheOptions.DocumentId = documentId; 
   DocumentFactory.DeleteFromCache(deleteFromCacheOptions); 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Document.Writer 
Imports Leadtools.Svg 
Imports Leadtools.Document 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Engine 
Imports Leadtools.Barcode 
Imports Leadtools.Ocr 
Imports LeadtoolsDocumentExamples.LeadtoolsExamples.Common 
Imports Leadtools.Document.Converter 
 
Public Shared Sub DownloadDocumentExample() 
   Dim documentUri As New Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf") 
   Dim documentFile1 As String = Path.Combine(ImagesPath.Path, "downloaded1.pdf") 
   Dim documentFile2 As String = Path.Combine(ImagesPath.Path, "downloaded2.pdf") 
 
   ' Setup a cache 
   Dim cache As New FileCache() 
   cache.CacheDirectory = "c:\cache-dir" 
 
   Dim documentId As String 
 
   ' Load a document into the cache 
   Dim loadDocumentOptions As New LoadDocumentOptions() 
   loadDocumentOptions.Cache = cache 
   Using document As LEADDocument = DocumentFactory.LoadFromUri(documentUri, loadDocumentOptions) 
      ' Get its document ID And save it 
      documentId = document.DocumentId 
      document.AutoDeleteFromCache = False 
      document.AutoSaveToCache = False 
      document.SaveToCache() 
   End Using 
 
   ' Now download it all at once into a memory stream 
   Dim downloadDocumentOptions As New DownloadDocumentOptions() 
   downloadDocumentOptions.Cache = cache 
   downloadDocumentOptions.DocumentId = documentId 
   downloadDocumentOptions.Offset = 0 
   downloadDocumentOptions.Length = -1 
   Using stream As New MemoryStream() 
      downloadDocumentOptions.Stream = stream 
      Dim bytes As Long = DocumentFactory.DownloadDocument(downloadDocumentOptions) 
      Console.WriteLine("Downloaded {0} bytes into the stream", bytes) 
 
      ' Save the stream to a file And show it 
      Using output As Stream = File.Create(documentFile1) 
         stream.WriteTo(output) 
      End Using 
 
      Process.Start(documentFile1) 
   End Using 
 
   ' Download it again, this time we will buffer it 32K at a time 
   downloadDocumentOptions = New DownloadDocumentOptions() 
   downloadDocumentOptions.Cache = cache 
   downloadDocumentOptions.DocumentId = documentId 
   Dim buffer((1024 * 32) - 1) As Byte 
   Using output As Stream = File.Create(documentFile2) 
      ' Offset to where we are 
      Dim offset As Long = 0 
 
      Dim bytes As Integer 
      Do 
         downloadDocumentOptions.Offset = offset 
         downloadDocumentOptions.Length = buffer.Length 
         downloadDocumentOptions.Data = buffer 
         downloadDocumentOptions.DataOffset = 0 
         bytes = CType(DocumentFactory.DownloadDocument(downloadDocumentOptions), Integer) 
         If bytes > 0 Then 
            Console.WriteLine("Downloaded {0} bytes into the buffer", bytes) 
            ' Next chunk 
            offset += bytes 
            output.Write(buffer, 0, bytes) 
         End If 
      Loop While bytes > 0 
   End Using 
 
   Process.Start(documentFile2) 
 
   ' We are done, clean up 
   Dim deleteFromCacheOptions As New LoadFromCacheOptions 
   deleteFromCacheOptions.Cache = cache 
   deleteFromCacheOptions.DocumentId = documentId 
   DocumentFactory.DeleteFromCache(deleteFromCacheOptions) 
End Sub 

Requirements

Target Platforms

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

Leadtools.Document Assembly