Convert a PDF to Images
Rasterizing a PDF to Images
Rasterizing a PDF to an image involves converting each page of the PDF document into a static image format, such as JPEG or PNG. This process is commonly used when you need to extract individual pages or images from a PDF for various purposes, like displaying them on a website or incorporating them into a different document.
PDF documents can be rasterized into images using IronPDF's PDF to Image Converter. You also have the option to specify the image type for the output images. Each output image will be named with "_pageNumber" at the end to indicate its corresponding page number in the original PDF.
Below is an example of how you can use IronPDF to convert a PDF to images using C#:
// This example demonstrates how to rasterize a PDF into individual images using IronPDF.
using IronPdf;
class PdfToImageExample
{
static void Main()
{
// Define the path to the PDF file you want to rasterize
string pdfPath = "example.pdf";
// Create a new instance of IronPdf.PdfDocument from a PDF file
PdfDocument pdfDoc = PdfDocument.FromFile(pdfPath);
// Optional: Specify the image format (e.g., JPEG, PNG) and DPI (dots per inch) for output images
var options = new ImageSaveOptions
{
ImageFormat = ImageFormat.Png, // Specify the desired image format
Dpi = 300 // Specify the resolution of the output images
};
// Define the directory where the images will be saved
string outputDirectory = "OutputImages";
// Convert each page of the PDF to an image and save them to the specified directory
pdfDoc.RasterizeToImageFiles(outputDirectory, "Page_Image_Page_{0}.png", options);
// Inform the user that the process is complete
Console.WriteLine("PDF pages have been successfully rasterized to images.");
}
}
// This example demonstrates how to rasterize a PDF into individual images using IronPDF.
using IronPdf;
class PdfToImageExample
{
static void Main()
{
// Define the path to the PDF file you want to rasterize
string pdfPath = "example.pdf";
// Create a new instance of IronPdf.PdfDocument from a PDF file
PdfDocument pdfDoc = PdfDocument.FromFile(pdfPath);
// Optional: Specify the image format (e.g., JPEG, PNG) and DPI (dots per inch) for output images
var options = new ImageSaveOptions
{
ImageFormat = ImageFormat.Png, // Specify the desired image format
Dpi = 300 // Specify the resolution of the output images
};
// Define the directory where the images will be saved
string outputDirectory = "OutputImages";
// Convert each page of the PDF to an image and save them to the specified directory
pdfDoc.RasterizeToImageFiles(outputDirectory, "Page_Image_Page_{0}.png", options);
// Inform the user that the process is complete
Console.WriteLine("PDF pages have been successfully rasterized to images.");
}
}
' This example demonstrates how to rasterize a PDF into individual images using IronPDF.
Imports IronPdf
Friend Class PdfToImageExample
Shared Sub Main()
' Define the path to the PDF file you want to rasterize
Dim pdfPath As String = "example.pdf"
' Create a new instance of IronPdf.PdfDocument from a PDF file
Dim pdfDoc As PdfDocument = PdfDocument.FromFile(pdfPath)
' Optional: Specify the image format (e.g., JPEG, PNG) and DPI (dots per inch) for output images
Dim options = New ImageSaveOptions With {
.ImageFormat = ImageFormat.Png,
.Dpi = 300
}
' Define the directory where the images will be saved
Dim outputDirectory As String = "OutputImages"
' Convert each page of the PDF to an image and save them to the specified directory
pdfDoc.RasterizeToImageFiles(outputDirectory, "Page_Image_Page_{0}.png", options)
' Inform the user that the process is complete
Console.WriteLine("PDF pages have been successfully rasterized to images.")
End Sub
End Class
Key Points:
- IronPDF is used to handle the conversion of PDF documents to images.
- The
FromFile
method loads the PDF document into anIronPdf.PdfDocument
object. ImageSaveOptions
allows customization of the image format and resolution.RasterizeToImageFiles
method exports each page of the PDF to separate image files, naming them according to their page number.- The images are saved to a specified output directory.
Ensure you have the IronPDF library installed in your project to use its functionalities. You can install it via NuGet Package Manager:
This snippet provides a basic framework for using IronPDF to convert a PDF into a series of images, which can be further customized to suit specific needs.