How to convert PDF to tiff in WPF PDFViewer?
WPF PDF Viewer control supports viewing, reviewing, and printing PDF files in WPF applications. The PDF Viewer also provides a way to convert PDF files into images in a format such as .jpg, .png, and .tiff in C# and VB.NET using the ExportAsImage API. You can use the images in your application to create thumbnails or cover page for a document, and more.
Convert a page of the PDF file into TIFF image
You can refer to the following steps for performing the same:
Step 1: Include the following namespace in the MainWindow.xaml.cs file.
C#
using Syncfusion.Windows.PdfViewer; using System.IO; using System.Windows; using System.Windows.Media.Imaging;
Step 2: Using the following code snippet, a PDF page can be exported as TIFF image.
C#
/// <summary> /// Exports the PDF pages as Tiff Images. /// </summary> /// <param name="fileName">The PDF file name</param> private void ExportPDFtoTiff(string fileName) { PdfDocumentView pdfViewer = new PdfDocumentView(); //Load the input PDF file pdfViewer.Load(fileName); //Export the first page as image . BitmapSource image = pdfViewer.ExportAsImage(0); if (image != null) { //Initialize the new Tiff bitmap encoder TiffBitmapEncoder encoder = new TiffBitmapEncoder(); //Set the compression to zip to reduce the file size. encoder.Compression = TiffCompressOption.Zip; //Create the bitmap frame using the bitmap source and add it to the encoder. encoder.Frames.Add(BitmapFrame.Create(image)); //Create the file stream for the output in the desired image format. using (FileStream stream = new FileStream("Image" + ".Tiff", FileMode.Create)) { //Save the stream, so that the image will be generated in the output location. encoder.Save(stream); } } }
Convert a specific range of pages of the PDF file into TIFF images
You can refer to the following steps for performing the same:
Step 1: Include the following namespace in the MainWindow.xaml.cs file.
C#
using Syncfusion.Windows.PdfViewer; using System.Windows.Media.Imaging; using System.IO;
Step 2: Using the following code snippet, the specific range of pages of a PDF file can be exported as TIFF images.
C#
/// <summary> /// Exports the PDF pages as Tiff Images. /// </summary> /// <param name="fileName">The PDF file name</param> private void ExportPDFtoTiff(string fileName) { PdfDocumentView pdfViewer = new PdfDocumentView(); //Load the input PDF file pdfViewer.Load(fileName); //Export the images From the input PDF file at the page range of 0 to 1 . BitmapSource[] image = pdfViewer.ExportAsImage(0, pdfViewer.PageCount - 1); if (image != null) { for (int i = 0; i < image.Length; i++) { //Initialize the new Tiff bitmap encoder TiffBitmapEncoder encoder = new TiffBitmapEncoder(); //Set the compression to zip to reduce the file size. encoder.Compression = TiffCompressOption.Zip; //Create the bitmap frame using the bitmap source and add it to the encoder. encoder.Frames.Add(BitmapFrame.Create(image[i])); //Create the file stream for the output in the desired image format. using (FileStream stream = new FileStream("Image_" + i.ToString() + ".Tiff", FileMode.Create)) { //Save the stream, so that the image will be generated in the output location. encoder.Save(stream); } } } }