PDF files have the advantage of being highly interactive and easy to transfer, but in certain cases, it is also necessary to convert PDF to images for embedding in web pages or displaying on some platforms that do not support PDF format. In this article, you will learn how to convert PDF to JPG, PNG or BMP image formats in C# and VB.NET 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 a Specific PDF Page to an Image in C# and VB.NET
Spire.PDF for .NET offers the PdfDocument.SaveAsImage() method to convert a particular page in PDF to an image. Then, you can save the image as a JPEG, PNG, BMP, EMF, GIF or WMF file. The following are the detailed steps.
- Create a Document instance.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Convert a specific page to an image and set the image Dpi using PdfDocument.SaveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method.
- Save the image as a PNG, JPG or BMP file using Image.Save(string filename, ImageFormat format) method.
- C#
- VB.NET
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("E:\\Files\\input.pdf"); //Convert the first page to an image and set the image Dpi Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 500, 500); //Save the image as a JPG file image.Save("ToJPG.jpg", ImageFormat.Jpeg); //Save the image as a PNG file //image.Save("ToPNG.png", ImageFormat.Png); //Save the image as a BMP file //image.Save("ToBMP.bmp", ImageFormat.Bmp); } } }
Convert an Entire PDF Document to Multiple Images in C# and VB.NET
If you want to convert the whole PDF document into multiple individual images, you can loop through all the pages in the PDF and then save them as JPG, PNG or BMP images. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Loop through all pages of the document and set the image Dpi when converting them to images using PdfDocument.SaveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method.
- Save images as PNG files using Image.Save() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using System; 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"); //Loop through each page in the PDF for (int i = 0; i < pdf.Pages.Count; i++) { //Convert all pages to images and set the image Dpi Image image = pdf.SaveAsImage(i, PdfImageType.Bitmap, 500, 500); //Save images as PNG format to a specified folder String file = String.Format("Image\\ToImage-{0}.png", i); image.Save(file, ImageFormat.Png); } } } }
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.