Background

PDF is now widely used to represent document in independent specification. It encapsulates a complete description of a fixed-layout flat document, including the text, fonts and graphics and so on. Due to its powerful functions, it is difficult for developers to parse its format. Or more specifically, to parse content out from PDF document and convert it to different image format is a tough task for some developers. This article will help you solve this problem by using PDF document viewer component Spire.PDFViewer for WPF by 5 easy steps. Firstly, you can download Spire.PDFViewer for WPF.

Target

To convert a specified or random page including frames of images from PDF file to TIFF programmatically.

Step 1: To create WPF application in Visual Studio and reference Spire.PdfViewer.WPF dlls.

Set .NET 4 as target framework

Step 2: Instance an object of Spire.PdfViewer.Wpf.PdfDocumentViewer

[C#]
PdfDocumentViewer pdfViewer = new PdfDocumentViewer();

Step 3: Call the “LoadFromFile”of PdfDocumentViewer object and load a PDF file.

[C#]
pdfViewer.LoadFromFile ("sample.pdf");

Step 4: Create an array and save all pages of this PDF file.

[C#]
int[] pageNumbers=new int[pageCount];
            for (int i=0;i

Step 5: Save it to Tiff image format

[C#]
pdfViewer.SaveAsImage("sample.tiff",pageNumbers);

The following code snippet shows all the code when converting pdf page to tiff image:

[C#]
private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Instance an object of Spire.PdfViewer.Wpf.PdfDocumentViewer
            PdfDocumentViewer pdfViewer = new PdfDocumentViewer();
            //Load a pdf file 
            pdfViewer.LoadFromFile("sample.pdf");
            int pageCount = pdfViewer.PageCount;
            // create an array and save all pages of this PDF file.
            int[] pageNumbers=new int[pageCount];
            for (int i=0;i

Screenshot

wpf screenshot

Spire.PDFViewer for WPF is a powerful WPF PDF Viewer control which enables developers to display PDF documents with their WPF applications without Adobe Reader. It’s available to load and view PDF documents like PDF/A-1B, PDF/X1A, and even encrypted from stream, file and byte array with support for printing, zooming, etc.

XPS is a format similar to PDF but uses XML in layout, appearance and printing information of a file. XPS format was developed by Microsoft and it is natively supported by the Windows operating systems. If you want to work with your PDF files on a Windows computer without installing other software, you can convert it to XPS format. Likewise, if you need to share a XPS file with a Mac user or use it on various devices, it is more recommended to convert it to PDF. This article will demonstrate how to programmatically convert PDF to XPS or XPS to PDF using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Convert PDF to XPS in C# and VB.NET

Spire.PDF for .NET supports converting PDF to various file formats, and to achieve the PDF to XPS conversion, you just need three lines of core code. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Convert the PDF document to an XPS file using PdfDocument.SaveToFile (string filename, FileFormat.XPS) method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace ConvertPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load sample PDF document 
            pdf.LoadFromFile("sample.pdf");

            //Save it to XPS format 
            pdf.SaveToFile("ToXPS.xps", FileFormat.XPS);
            pdf.Close();
        }
    }
}

C#/VB.NET: Convert PDF to XPS or XPS to PDF

Convert XPS to PDF in C# and VB.NET

Conversion from XPS to PDF can also be achieved with Spire.PDF for .NET. While converting, you can set to keep high quality image on the generated PDF file by using the PdfDocument.ConvertOptions.SetXpsToPdfOptions() method. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load an XPS file using PdfDocument.LoadFromFile(string filename, FileFormat.XPS) method or PdfDocument.LoadFromXPS() method.
  • While conversion, set the XPS to PDF convert options to keep high quality images using PdfDocument.ConvertOptions.SetXpsToPdfOptions() method.
  • Save the XPS file to a PDF file using PdfDocument.SaveToFile(string filename, FileFormat.PDF) method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace ConvertXPStoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample XPS file
            pdf.LoadFromFile("Sample.xps", FileFormat.XPS);
            //pdf.LoadFromXPS("Sample.xps");

            //Keep high quality images when converting XPS to PDF
            pdf.ConvertOptions.SetXpsToPdfOptions(true);

            //Save the XPS file to PDF
            pdf.SaveToFile("XPStoPDF.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Convert PDF to XPS or XPS to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Convert PDF to Images in C# | .NET

2023-06-12 07:27:00 Written by daisy zhang

Converting PDF files to images is a common requirement for applications that involve document previews, thumbnails generation, or PDF content archiving. For .NET developers, the Spire.PDF library offers a robust and efficient way to achieve this PDF to image conversion with high fidelity. Its key advantages include:

  • High-Fidelity Rendering: Preserve original layouts and graphics.
  • Multi-Format Support: Export to PNG, JPEG, BMP, TIFF, and more.
  • Cross-Platform: Works with .NET Framework, .NET Core, and .NET 6+.

Visual guide for converting PDF to images Using C#

In this guide, you’ll learn how to use the Spire.PDF for .NET library to convert PDF to JPG or PNG images in C#, with installation guide, practical code snippets, and conversion optimization tips.

Install the PDF to Image Converter Library

Before you can start using C# to convert PDF to PNG or PDF to JPG, it’s necessary to install the Spire.PDF for .NET library first. You can do this via NuGet Package Manager:

  • Open your project in Visual Studio
  • Go to “Tools > NuGet Package Manager > Manage NuGet Packages for Solution”
  • Search for "Spire.PDF"
  • Select and install the package

Alternatively, use the Package Manager Console:

PM> Install-Package Spire.PDF

Once installed, you're ready to start writing code to handle PDF to image conversions.

Convert PDF to JPG Images in C#: Basic Example

The SaveAsImage(int pageIndex, PdfImageType type) method of the PdfDocument class can be used to convert a specified PDF page to an image. The two parameters are:

  1. pageIndex (Int32)
  • Description: The index of the PDF page to be converted (page index starts from 0).
  • Example:
    • 0 = First page
    • 1 = Second page
  1. type (PdfImageType)
  • Description: Specifies the type of the PdfImage.
  • Enum Values:
    • Bitmap
    • Metafile

The following code demonstrates how to convert the first page of a PDF to a JPEG image using Spire.PDF:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoJPG
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load a sample PDF document
            pdf.LoadFromFile("input.pdf");

            // Convert the first page to a bitmap image
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap);

            // Save the image as a JPG file
            image.Save("PdfToJPG.jpg", ImageFormat.Jpeg);

            // Disposes PDF resources
            pdf.Dispose();
        }
    }
}

Convert a PDF page to a JPG image

Advanced PDF to Image Conversion Options

1. Set Image Resolution

Spire.PDF offers another overload of the SaveAsImage() method that takes four parameters: the page index, image type, horizontal (dpiX) and vertical (dpiY) resolution. This allows you to control the quality and size of the output image by customizing the DPI settings.

The following code example converts the first page of a PDF to a high-resolution PNG image (300 DPI):

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load a sample PDF document
            pdf.LoadFromFile("input.pdf");

            // Convert the first page to an image and with specified image resolution
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);

            // Save the image as a PNG file
            image.Save("PdfToPNG.png", ImageFormat.Png);

            // Disposes PDF resources
            pdf.Dispose();

        }
    }
}

Convert a PDF page to a PNG image

Note: Higher DPI improves image quality but slow down conversion.

2. Convert an Entire PDF File to Images

Loop through all pages in the PDF and then convert each one to a separate image.

// Loop through all pages in the PDF
for (int i = 0; i < pdf.Pages.Count; i++)
{
    // Convert each page to an image and set the image Dpi
    Image image = pdf.SaveAsImage(i, PdfImageType.Bitmap, 300, 300);

    // Save the image as PNG format to a specified folder
    string file = string.Format("Image\\ToImage-{0}.png", i);
    image.Save(file, ImageFormat.Png);
}

3. Convert to Different Image Formats

By changing the ImageFormat parameter of the Image.Save() method, you can convert PDF to JPG, PNG, BMP, or Gif formats:

// Save to JPEG format
image.Save("PDFtoJPG.jpg", ImageFormat.Jpeg);

// Save to PNG format
image.Save("PDFtoPNG.png", ImageFormat.Png);

// Save to BMP format
image.Save("PDFtoBMP.bmp", ImageFormat.Bmp);

// Save to EMF format
image.Save("PDFtoEMF.emf", ImageFormat.Emf);

// Save to GIF format
image.Save("PDFtoGIF.gif", ImageFormat.Gif);

4. Using MemoryStream

For scenarios requiring stream handling, use the following code:

// Create a MemoryStream object
MemoryStream ms = new MemoryStream();

// Convert the first page to an image with specified dpi
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);

// Save the image to stream in PNG format
image.Save(ms, ImageFormat.Png);

// Write the Stream object to a byte array for further processing
byte[] imageBytes = ms.ToArray();

Performance Optimization Tips

  • Memory Management: Convert pages one by one to avoid memory overload for large PDFs.
  • DPI Optimization: High DPI settings improve image quality, but also increase file size and processing time.
  • Dispose Resources: Call the Dispose() method of PdfDocument class to free resources.

Conclusion

Spire.PDF provides a straightforward and efficient way to convert PDF files to images in C#. With its rich feature set, high-quality rendering, and support for multiple image formats, it’s a reliable choice for both simple and complex PDF-to-image conversion tasks. By following the examples in this article, you can quickly implement PDF converter functionality in your .NET applications.

FAQs

Q1: How to remove the watermarks on the output images?

A: You can request a free 30-day trial license here to remove the red watermark and fully evaluate the Spire.PDF library. Or you can use its Free version (with certain page limitations).

Q2: Can I convert PDFs to multi-page TIFF files with Spire.PDF?

A: Yes! A detailed guide for this can be found at: Convert PDF to TIFF in C#

Q3: Can I convert PDF to SVG using Spire.PDF?

A: Yes! Spire.PDF for .NET offers the SaveToFile(String, FileFormat.SVG) method to convert a PDF file to a SVG file. You can check out the article for a comprehensive guide.

Q4. Where can I find additional documentation or support?

page 289