Overview
The PdfStreamWriter class enables you to write file content directly to a Stream. This is the root element of the streaming mechanism used when exporting a PDF document.
Create a PdfStreamWriter Instance
To create an object of type PdfSteamWriter, you should pass it the Stream of the file, which you would like to work with, as a constructor parameter.
Example 1: Instantiate PdfStreamWriter
using (PdfStreamWriter writer = new PdfStreamWriter(File.OpenWrite(resultDocument)))
{
// ...
}
PdfStreamWriter inherits from IDisposable. Make sure the object is disposed when you are done with it. Otherwise, the content might not be written in the exported file. The best way to ensure this is handled properly is to wrap it in a using statement.
PdfStreamWriter Members
The members of the class allow you to set several properties of the document you are working with as well as generate and write new pages.
-
BeginPage(): The BeginPage() method returns an instance of the PdfPageStreamWriter class, which is responsible to draw the content of the page. More information about this class is available in the PdfPageStreamWriter article. The overloads of BeginPage() allow you to pass the size and the Rotation of the page.
Example 2: Insert a new page into a document
using (PdfStreamWriter writer = new PdfStreamWriter(File.OpenWrite(resultDocument))) { Size size = new Size(700,1200); Rotation rotation = Rotation.Rotate270; using (PdfPageStreamWriter pageWriter = writer.BeginPage(size, rotation)) { // Use the pageWriter object to fill the content of the page. } }
-
WritePage(): The WritePage() methods enable you to pass an already constructed page object. With the different overloads, you can pass an instance of RadFixedPage and PdfPageStreamWriter.
Example 3: Insert an already generated page into a document
using (PdfStreamWriter writer = new PdfStreamWriter(File.OpenWrite(resultDocument))) { RadFixedPage page = this.GeneratePage(); writer.WritePage(page); }
Settings of PdfStreamWriter
Through the Settings property of PdfStreamWriter you can control the way the document is exported. The following list describes the available properties:
DocumentInfo: A property of type RadFixedDocumentInfo, intended to hold additional information about the document. The RadFixedDocumentInfo class allows you to set the title, author and description of the document.
ImageQuality: This property is of type ImageQuality and gets or sets the default image quality when exporting images to PDF. The default value is High. The value of this property is overridden when specifying the ImageQuality in ImageSource constructor or when creating ImageSource from EncodedImageData. The quality of the images reflects the size of the PDF file. The higher the quality, the bigger the document size is.
WriteAnnotations: A boolean property indicating whether the annotations should be included in the exported document.
The XAML SDK repository on GitHub contains examples showing the capabilities of PdfStreamWriter:
- The PdfStreamWriterPerformance sample shows the performance you can achieve using PdfStreamWriter
- The Manipulate Pages example shows different use cases of PdfStreamWriter