←Select platform

WriteAsync(Stream,object) Method

Summary

Asynchronously writes the created PDF content to a .NET stream.

Syntax
C#
C++/CLI
Python
public void WriteAsync( 
   Stream outputStream, object userState 
) 
public: 
void WriteAsync(  
   Stream^ outputStream, object^ userState 
)  
def WriteAsync(self,outputStream,userState): 

Parameters

outputStream
The output stream.

userState
User data object that will be passed to the WriteAsyncCompleted event.

Remarks

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 this method to write the file to a stream, before calling the Dispose method.

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

WriteAsync fires the WriteAsyncCompleted event when it finishes.

For more information, refer to Creating a Compressed PDF File.

Use PdfCompressorEngine.Write(string) to write the result to a disk file.

Use PdfCompressorExtensions.WriteAsync(PdfCompressorEngine,ILeadStream) for asynchronous writing using the async/await mechanism.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
 
using Leadtools.PdfCompressor; 
 
/* This example shows a how to use the PdfCompressorEngine::WriteAsync method to create a PDF asynchronously.  
 * The output file is very simple and contains a single image.  
 * We will store the RasterImage and RasterImage and PdfCompressorEngine in a user class so we can dispose them  
 * when WriteAsync finishes and fires the WriteAsyncCompleted event. 
*/ 
 
 
/* Class used to store the RasterImage and PdfCompressorEngine objects */ 
private class WriteAsyncData 
{ 
    public RasterImage image;                 /* image to write as Pdf */ 
    public PdfCompressorEngine pdfCompressor; /* compressor object that will do the save */ 
}; 
 
/* Handler for the WriteAsyncCompleted event */ 
private static void OnWriteAsyncCompletedStreamObject(object sender, PdfCompressorWriteAsyncCompletedEventArgs args) 
{ 
    if (args.Error != null) 
        Debug.WriteLine($"WriteAsyncThread: The save operation to {((FileStream)args.Stream).Name} failed with exception = {args.Error.Message}."); 
    else if (args.Cancelled) 
        Debug.WriteLine($"WriteAsyncThread: The save operation to {((FileStream)args.Stream).Name} was cancelled!"); 
    else 
        Debug.WriteLine($"WriteAsyncThread: The save operation to {((FileStream)args.Stream).Name} succeeded!"); 
 
    /* clean up the data used by WriteAsync operation */ 
    WriteAsyncData data = (WriteAsyncData)args.UserState; 
    data.pdfCompressor.Dispose(); 
    data.image.Dispose(); 
    if (args.Stream != null) 
        args.Stream.Close(); 
} 
 
/* Main function calling PdfCompressorEngine.WriteAsync. Note that WriteAsync will finish after this function returns */ 
public void TestPdfCompressorSaveAsyncStreamObject() 
{ 
    string srcFile = Path.Combine(LEAD_VARS.ImagesDir, "glare.jpg"); 
    string dstFile = Path.Combine(LEAD_VARS.ImagesDir, "out.pdf"); 
 
    WriteAsyncData data = new WriteAsyncData(); 
    using (RasterCodecs codecs = new RasterCodecs()) 
        data.image = codecs.Load(srcFile); 
    data.pdfCompressor = new PdfCompressorEngine(); 
    data.pdfCompressor.Insert(data.image); 
 
    /* Register OnWriteAsyncCompleted as a handler for the WriteAsyncCompleted event */ 
    data.pdfCompressor.WriteAsyncCompleted += OnWriteAsyncCompletedStreamObject; 
    FileStream outputStream = File.Create(dstFile); 
    data.pdfCompressor.WriteAsync(outputStream, data); 
    /* We cannot close outputStream now. We'll close it when WriteAsync finishes */ 
 
    Debug.WriteLine("MainThread: WriteAsync returned..."); 
} 
 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

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

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