C# Solution for PDF to Image Conversion

2024-01-31 02:16:51 Koohji

C# Solution for PDF to Image Conversion

PDF files are widely used in daily work due to their stable layout, easy transferability and high readability. In some cases, you may need to convert PDF to images. Saving content as images not only speeds up loading, but also reduces the risk of accidental editing. In addition, if you want to embed PDF files or pages into other documents or web pages, converting them to specified image formats is an excellent choice. This passage covers the details of using C# to convert PDF to image. And you can effortlessly convert PDF to the desired image formats with C# code while preserving the visual fidelity of the original content.

C# Library for PDF Conversion

This article demonstrates how to convert PDF to image using Spire.PDF for .NET, which is a reliable PDF processing library that enables you to manipulate and convert PDF files within .NET applications. With it, you can even adjust the properties of the image, such as its size and pixels, during the conversion from PDF to image.
In addition to image, this library also allows you to convert PDF to Word, PDF to HTML, PDF to XPS, and so on.

You can either download it and reference the DLL file manually or install it via NuGet using the following command:

PM> Install-Package Spire.PDF

Classification of Image Formats

In computer graphics, images can be classified into two main categories:

  • Bitmap images are composed of a grid of individual pixels, where each pixel is assigned a specific color. Common bitmap image formats include PNG, JPG, BMP, EMF and TIFF.
  • Vector graphics, on the other hand, represent images using mathematical equations and geometric shapes such as lines, curves, and polygons. Common vector graphic formats include SVG and EPS.

Currently, Spire.PDF for .NET supports the PNG/JPG/BMP/EMF/TIFF/SVG formats. Next, I will give you the details of each conversion.

Convert PDF to PNG/JPG/BMP/EMF/TIFF in C#

Steps

  1. Create a PdfDocument instance and load a PDF file from disk by using LoadFromFile() method.
  2. Call the SaveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method to convert the specified page to an image.
  3. Finally, save images as JPG files using Image.Save(string filename, ImageFormat format) method. In this case, I take PDF to JPG as an example. You can also change the format if you like.

Sample Code

  • C#
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 file
            pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            //Convert the first page to an image and set the image Dpi
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 500, 500);

            //Save images as JPG format to a specified folder 
            image.Save("C:\\Users\\Administrator\\Desktop\\Image\\ToImage.jpg", ImageFormat.Jpeg);

            //Close the document
            pdf.Close();
        }
    }
}

Convert PDF to SVG in C#

Steps

  1. Create a PdfDocument instance and load a PDF file from disk by using LoadFromFile() method.
  2. Convert the first page to SVG using the SaveToFile(string filename, int startIndex, int endIndex, FileFormat fileFormat) method. You can specify the page index you want to convert in the parameter.

Sample Code

  • C#
using Spire.Pdf;

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

            //Load a sample PDF file
            pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            //Convert the first page to SVG
            pdf.SaveToFile("C:\\Users\\Administrator\\Desktop\\Image\\ToImage.svg", 0, 0, FileFormat.SVG);

            //Close the document
            pdf.Close();
        }
    }
}

Convert PDF to a Multiple-Page TIFF in C#

Compared with other image formats, TIFF allows multiple images or pages to be stored in a single file. This feature makes it a popular choice among users in some cases.

In this section, I will show you how to convert a PDF file into a multi-page TIFF file. To achieve this conversion, we can customize the following methods.
  • SaveAsImage() method supports converting each PDF page to an image and then returns an array of images.
  • GetEncoderInfo() method supports finding and returning matching image encoder information based on the given MIME type.
  • JoinTiffImages() method is used to merge multiple images into a single TIFF file. It works by looping through each image in the image array and saving it according to the specified encoder parameters.

Steps

  1. Create a PdfDocument instance and load a PDF file from disk by using LoadFromFile() method.
  2. Call SaveAsImage() method to convert each page of the PDF to an image and save it in the image array.
  3. Finally, call JoinTiffImages() method to merge the converted TIFF images into a multi-page TIFF file.

Sample Code

  • C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
namespace SavePdfAsTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load the PDF file
            pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Convert PDF pages to images and save them as an image array
            Image[] images = SaveAsImage(pdf);

            // Merge the converted TIFF images into a multi-page TIFF file
            JoinTiffImages(images, "C:\\Users\\Administrator\\Desktop\\Image\\ToImage.tiff", EncoderValue.CompressionLZW);
        }

        private static Image[] SaveAsImage(PdfDocument pdf)
        {
            // Create an image array with the size equal to the number of PDF pages
            Image[] images = new Image[pdf.Pages.Count];

            //Iterate through each page of PDF
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                //Convert these pages to images 
                images[i] = pdf.SaveAsImage(i);
            }
            return images;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            // Get all available image encoders
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                // Find the encoder that matches the specified mime type
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            // An exception is thrown if no matching encoder is found
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }

        public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            //Set the parameters of the image encoder
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);

            // Initialize the first image as the base for merging
            Image pages = images[0];
            int frame = 0;

            // Get the encoder information for TIFF format
            ImageCodecInfo info = GetEncoderInfo("image/tiff");

            // Iterate through each image
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    // If it's the first frame, set it as the current base image
                    pages = img;

                    // Save the first frame using the specified encoder parameters
                    pages.Save(outFile, info, ep);
                }
                else
                {
                    // For intermediate frames, update the encoder parameter to indicate a new page
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    // Save the intermediate frame
                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {
                    // If it's the last frame, flush the encoder parameters to close the file
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}

Free Trial of C# Library

Spire.PDF for .NET provides free usage to help users better evaluate product features without restrictions. You can obtain a Free 30-Day Temporary License from the following link to convert PDF to image in C#.

Conclusion

In this post, you have learned how to convert PDF to popular image formats using C#. Spire.PDF for .NET can also help you with other PDF processing tasks, such as creating PDF, merging PDF, comparing PDF, and so on. In a word, this library simplifies the process and allows developers to focus on building powerful applications that involve PDF manipulation tasks.

See Also