←Select platform

Stream Property

Summary

Stream to read the data into during this download operation.

Syntax
C#
C++/CLI
Python
public Stream Stream {get; set;} 
public:  
   property Stream^ Stream 
   { 
      Stream^ get() 
      void set(Stream^ value) 
   } 
Stream # get and set (DownloadDocumentOptions) 

Property Value

Stream into which to read the document data. The default value is null.

Remarks

To download a document from the cache, use either Data or Stream, but not both. In other words, one of these properties must be set to a non-null value to indicate the target for the download operation.

When Stream is used, the data is stored beginning at the offset stored in the stream itself, and DataOffset is not used.

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

Length is the maximum number of bytes to read.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void DownloadDocumentExample() 
{ 
   var documentUri = new Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf"); 
   string documentFile1 = Path.Combine(LEAD_VARS.ImagesDir, "downloaded1.pdf"); 
   string documentFile2 = Path.Combine(LEAD_VARS.ImagesDir, "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); 
 
      long annotations = DocumentFactory.DownloadAnnotations(downloadDocumentOptions); 
      Console.WriteLine(annotations + " annotations downloaded"); 
 
      // The following method is obsolete. Use DownloadAnnotations(DownloadDocumentOptions) instead 
      int annotations2 = DocumentFactory.DownloadAnnotations(downloadDocumentOptions.Cache, downloadDocumentOptions.DocumentId, downloadDocumentOptions.Offset, (int)bytes, stream); 
      Console.WriteLine(annotations2 + " annotations downloaded"); 
 
      // 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); 
 
            // The following method is obsolete. Use DownloadAnnotations(DownloadDocumentOptions) instead 
            int annotations = DocumentFactory.DownloadAnnotations(downloadDocumentOptions.Cache, downloadDocumentOptions.DocumentId, downloadDocumentOptions.Offset, buffer, downloadDocumentOptions.DataOffset, buffer.Length); 
            Console.WriteLine(annotations + " annotations downloaded"); 
         } 
      } 
      while (bytes > 0); 
   } 
 
   Process.Start(documentFile2); 
 
   // We are done, clean up 
   var deleteFromCacheOptions = new LoadFromCacheOptions(); 
   deleteFromCacheOptions.Cache = cache; 
   deleteFromCacheOptions.DocumentId = documentId; 
   DocumentFactory.DeleteFromCache(deleteFromCacheOptions); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
 
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.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void downloadDocumentExample() throws IOException, URISyntaxException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   URI documentUri = new URI("https://demo.leadtools.com/images/pdf/leadtools.pdf"); 
   String documentFile1 = combine(LEAD_VARS_IMAGES_DIR, "downloaded1.pdf"); 
   String documentFile2 = combine(LEAD_VARS_IMAGES_DIR, "downloaded2.pdf"); 
 
   // Setup a cache 
   FileCache cache = new FileCache(); 
   cache.setCacheDirectory("c:\\cache-dir"); 
 
   String documentId; 
 
   // Load a document into the cache 
   LoadDocumentOptions loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.setCache(cache); 
   LEADDocument document = DocumentFactory.loadFromUri(documentUri, loadDocumentOptions); 
 
   // Get its document ID and save it 
   documentId = document.getDocumentId(); 
   document.setAutoDeleteFromCache(false); 
   document.setAutoSaveToCache(false); 
   document.saveToCache(); 
 
   // Now download it all at once into a memory stream 
   DownloadDocumentOptions downloadDocumentOptions = new DownloadDocumentOptions(); 
   downloadDocumentOptions.setCache(cache); 
   downloadDocumentOptions.setDocumentId(documentId); 
   downloadDocumentOptions.setOffset(0); 
   downloadDocumentOptions.setLength(-1); 
   ILeadStream stream = LeadStreamFactory.create(documentFile1); 
 
   downloadDocumentOptions.setStream(stream); 
   long bytes = DocumentFactory.downloadDocument(downloadDocumentOptions); 
   System.out.printf("Downloaded %s bytes into the stream%n", bytes); 
 
   long annotations = DocumentFactory.downloadAnnotations(downloadDocumentOptions); 
   System.out.println(annotations + " annotations downloaded"); 
 
   // The following method is obsolete. Use 
   // DownloadAnnotations(DownloadDocumentOptions) instead 
   int annotations2 = DocumentFactory.downloadAnnotations(downloadDocumentOptions.getCache(), 
         downloadDocumentOptions.getDocumentId(), downloadDocumentOptions.getOffset(), (int) bytes, stream); 
   System.out.println(annotations2 + " annotations downloaded"); 
 
   // Save the stream to a file and show it 
   stream.dispose(); 
 
   // Download it again, this time we will buffer it 32K at a time 
   downloadDocumentOptions = new DownloadDocumentOptions(); 
   downloadDocumentOptions.setCache(cache); 
   downloadDocumentOptions.setDocumentId(documentId); 
   byte[] buffer = new byte[1024 * 32]; 
   FileOutputStream output = new FileOutputStream(documentFile2); 
 
   // Offset to where we are: 
   long offset = 0; 
 
   bytes = 0; 
   do { 
      downloadDocumentOptions.setOffset(offset); 
      downloadDocumentOptions.setLength(buffer.length); 
      downloadDocumentOptions.setData(buffer); 
      downloadDocumentOptions.setDataOffset(0); 
      bytes = (int) DocumentFactory.downloadDocument(downloadDocumentOptions); 
      if (bytes > 0) { 
         System.out.printf("Downloaded %s bytes into the buffer%n", bytes); 
         // Next chunk 
         offset += bytes; 
         output.write(buffer, 0, (int) bytes); 
 
         // The following method is obsolete. Use 
         // DownloadAnnotations(DownloadDocumentOptions) instead 
         annotations = DocumentFactory.downloadAnnotations(downloadDocumentOptions.getCache(), 
               downloadDocumentOptions.getDocumentId(), downloadDocumentOptions.getOffset(), buffer, 
               downloadDocumentOptions.getDataOffset(), buffer.length); 
         System.out.println(annotations + " annotations downloaded"); 
      } 
   } while (bytes > 0); 
 
   // We are done, clean up 
   LoadFromCacheOptions deleteFromCacheOptions = new LoadFromCacheOptions(); 
   deleteFromCacheOptions.setCache(cache); 
   deleteFromCacheOptions.setDocumentId(documentId); 
   DocumentFactory.deleteFromCache(deleteFromCacheOptions); 
   assertTrue(new File(documentFile2).exists()); 
   output.close(); 
} 
Requirements

Target Platforms

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.