WriteAsync(ILeadStream) Method

Summary

Asynchronously writes the created PDF content to a LEAD stream.

Syntax
C#
public Task WriteAsync( 
   ILeadStream stream 
) 

Parameters

stream
The output LEAD stream.

Remarks

This topic is part of PdfCompressor for .NET standard async/await support.

For .NET Framework, you have two choices:

1) Use the async/await mechanism with PdfCompressorExtensions.WriteAsync(PdfCompressorEngine,ILeadStream) 2) Use the System.ComponentModel.AsyncOperation mechanism with PdfCompressorEngine.WriteAsync.

For synchronous writing, use PdfCompressorEngine.Write.

The PDF Compressor object normally creates the compressed PDF document file in memory. It can add as many pages as required to the file. Once the PDF file in memory is complete, call WriteAsync method to write the file to a LEAD stream. Once WriteAsync finishes, call the PdfCompressorEngine.Dispose method.

WriteAsync(ILeadStream) operates asynchronously, which is useful in cases when you are saving a large file and the Write operation takes a long time. You can use this operation from the main thread and wait for it to finish in a worker thread.

It is important to wait for the operation to finish before disposing any resources added to the PdfCompressorEngine object.

Note that the ILeadStream object can also indicate a disk file rather than a .NET stream. See the LeadFileStream class for more details.

For more information on creating PDF files using PdfCompressorEngine, refer to Creating a Compressed PDF File.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
 
using Leadtools.PdfCompressor; 
 
/* This example shows how to write a PDF file to a disk file asynchronously. Writing the PDF content to a .NET stream is very similar: simply create the LEAD stream using LeadStream.Factory.FromStream */ 
public void TestPdfCompressorWriteAsyncILeadStream() 
{ 
   string srcFile = Path.Combine(LEAD_VARS.ImagesDir, "loadsave.jpg"); 
   using (RasterCodecs codecs = new RasterCodecs()) 
   using (RasterImage image = codecs.Load(srcFile)) 
   { 
      string dstFile = Path.Combine(LEAD_VARS.ImagesDir, "out_AsyncILeadStream.pdf"); 
 
 
      /* This is how you can create the leadOutStream from a .NET stream object: 
         using (ILeadStream leadOutStream = LeadStream.Factory.FromStream(new System.IO.FileStream(dstFile, System.IO.FileMode.Create), true)) 
      */ 
 
      using (ILeadStream leadOutStream = new LeadFileStream(dstFile, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite)) 
      using (PdfCompressorEngine pdfCompressor = new PdfCompressorEngine()) 
      { 
         pdfCompressor.Insert(image); 
         Task task = pdfCompressor.WriteAsync(leadOutStream); 
         Debug.WriteLine("pdfCompressor.WriteAsync returned. Waiting for it to finish asynchronously..."); 
         task.Wait(); 
         Debug.WriteLine("pdfCompressor.WriteAsync finished asynchronously. Returning"); 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
Requirements

For .NET Standard: This functionality is included in the Leadtools.PdfCompressor.dll assembly.

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

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