Convert PDF to Different Image Formats in C#
Overview
This article explains how to convert PDF to different image formats using C#. It covers the following topics.
Image Format: TIFF
Image Format: BMP
Image Format: EMF
Image Format: JPG
Image Format: PNG
Image Format: GIF
Image Format: SVG
C# Convert PDF to Image
The following code snippet also work with Aspose.PDF.Drawing library.
Aspose.PDF for .NET uses several approaches to convert PDF to image. Generally speaking, we use two approaches: conversion using the Device approach and conversion using SaveOption. This section will show you how to convert PDF documents to image formats such as BMP, JPEG, GIF, PNG, EMF, TIFF, and SVG formats using one of those approaches.
There are several classes in the library that allow you to use a virtual device to transform images. DocumentDevice is oriented for conversion whole document, but ImageDevice - for a particular page.
Convert PDF using DocumentDevice class
Aspose.PDF for .NET makes a possible to convert PDF Pages to TIFF images.
The TiffDevice (based on DocumentDevice) class allows you to convert PDF pages to TIFF images. This class provides a method named Process
which allows you to convert all the pages in a PDF file to a single TIFF image.
Try to convert PDF to TIFF online
Aspose.PDF for .NET presents you online free application “PDF to TIFF”, where you may try to investigate the functionality and quality it works.
Convert PDF Pages to One TIFF Image
Aspose.PDF for .NET explain how to convert all pages in a PDF file to a single TIFF image:
Steps: Convert PDF to TIFF in C#
- Create an object of the Document class.
- Create TiffSettings and TiffDevice objects.
- Call the TiffDevice.Process() method to convert the PDF document to TIFF.
- To set the output file’s properties, use the TiffSettings class.
The following code snippet shows how to convert all the PDF pages to a single TIFF image.
private static void ConvertPDFtoTIFF()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document(dataDir + "PDFtoTIFF.pdf"))
{
// Create Resolution object
var resolution = new Aspose.Pdf.Devices.Resolution(300);
// Create TiffSettings object
var tiffSettings = new Aspose.Pdf.Devices.TiffSettings
{
Compression = Aspose.Pdf.Devices.CompressionType.None,
Depth = Aspose.Pdf.Devices.ColorDepth.Default,
Shape = Aspose.Pdf.Devices.ShapeType.Landscape,
SkipBlankPages = false
};
// Create TIFF device
var tiffDevice = new Aspose.Pdf.Devices.TiffDevice(resolution, tiffSettings);
// Convert a particular page and save the image to stream
tiffDevice.Process(document, dataDir + "PDFtoTIFF_out.tif");
}
}
Convert One Page to TIFF Image
Aspose.PDF for .NET allows to convert a particular page in a PDF file to a TIFF image, use an overloaded version of the Process(..) method which takes a page number as an argument for conversion. The following code snippet shows how to convert the first page of a PDF to TIFF format.
Steps: Convert Single or Particular Pages of PDF to TIFF in C#
- Create an object of the Document class.
- Create TiffSettings and TiffDevice objects.
- Call the overloaded TiffDevice.Process() method with fromPage and toPage parameters to convert PDF document pages to TIFF.
private static void ConvertPDFtoTiffSinglePage()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document(dataDir + "PDFtoTiffSinglePage.pdf"))
{
// Create Resolution object
var resolution = new Aspose.Pdf.Devices.Resolution(300);
// Create TiffSettings object
var tiffSettings = new Aspose.Pdf.Devices.TiffSettings
{
Compression = Aspose.Pdf.Devices.CompressionType.None,
Depth = Aspose.Pdf.Devices.ColorDepth.Default,
Shape = Aspose.Pdf.Devices.ShapeType.Landscape,
};
// Create TIFF device
var tiffDevice = new Aspose.Pdf.Devices.TiffDevice(resolution, tiffSettings);
// Convert a particular page and save the image to stream
tiffDevice.Process(document, 1, 1, dataDir + "PDFtoTiffSinglePage_out.tif");
}
}
Use Bradley algorithm during conversion
Aspose.PDF for .NET has been supporting the feature to convert PDF to TIF using LZW compression and then with the use of AForge, Binarization can be applied. However one of the customers requested that for some images, they need to get the Threshold using Otsu, so they also would like to use Bradley.
private static void ConvertPDFtoTiffBradleyBinarization()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document(dataDir + "PDFtoTiffBradleyBinarization.pdf"))
{
string outputImageFile = dataDir + "PDFtoTiffBradleyBinarization_out.tif";
string outputBinImageFile = dataDir + "PDFtoTiffBradleyBinarization-bin_out.tif";
// Create Resolution object
var resolution = new Aspose.Pdf.Devices.Resolution(300);
// Create TiffSettings object
var tiffSettings = new Aspose.Pdf.Devices.TiffSettings
{
Compression = Aspose.Pdf.Devices.CompressionType.LZW,
Depth = Aspose.Pdf.Devices.ColorDepth.Format1bpp
};
// Create TIFF device
var tiffDevice = new Aspose.Pdf.Devices.TiffDevice(resolution, tiffSettings);
// Convert a particular page and save the image to stream
tiffDevice.Process(document, outputImageFile);
// Binarize the image using Bradley method
using (var inStream = new FileStream(outputImageFile, FileMode.Open))
{
using (var outStream = new FileStream(outputBinImageFile, FileMode.Create))
{
tiffDevice.BinarizeBradley(inStream, outStream, 0.1);
}
}
}
}
Convert PDF using ImageDevice class
ImageDevice
is the ancestor for BmpDevice
, JpegDevice
, GifDevice
, PngDevice
and EmfDevice
.
- The BmpDevice class allows you to convert PDF pages to BMP images.
- The EmfDevice class allows you to convert PDF pages to EMF images.
- The JpegDevice class allows you to convert PDF pages to JPEG images.
- The PngDevice class allows you to convert PDF pages to PNG images.
- The GifDevice class allows you to convert PDF pages to GIF images.
Let’s take a look at how to convert a PDF page to an image.
BmpDevice
class provides a method named Process which allows you to convert a particular page of the PDF file to BMP image format. The other classes have the same method. So, if we need to convert a PDF page to an image, we just instantiate the required class.
The following steps and code snippet in C# shows this possibility
- Convert PDF to BMP in C#
- Convert PDF to EMF in C#
- Convert PDF to JPG in C#
- Convert PDF to PNG in C#
- Convert PDF to GIF in C#
Steps: PDF to Image (BMP, EMF, JPG, PNG, GIF) in C#
- Load the PDF file using Document class.
- Create an instance of subclass of ImageDevice i.e.
- BmpDevice (to convert PDF to BMP).
- EmfDevice (to convert PDF to Emf).
- JpegDevice (to convert PDF to JPG).
- PngDevice (to convert PDF to PNG).
- GifDevice (to convert PDF to GIF).
- Call the ImageDevice.Process() method to perform PDF to Image conversion.
// BMP, JPEG, GIF, PNG, EMF
private static void ConvertPDFusingImageDevice()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create Resolution object
var resolution = new Aspose.Pdf.Devices.Resolution(300);
var bmpDevice = new Aspose.Pdf.Devices.BmpDevice(resolution);
var jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution);
var gifDevice = new Aspose.Pdf.Devices.GifDevice(resolution);
var pngDevice = new Aspose.Pdf.Devices.PngDevice(resolution);
var emfDevice = new Aspose.Pdf.Devices.EmfDevice(resolution);
// Open document using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertAllPagesToBmp.pdf"))
{
ConvertPDFtoImage(bmpDevice, "bmp", document, dataDir);
ConvertPDFtoImage(jpegDevice, "jpeg", document, dataDir);
ConvertPDFtoImage(gifDevice, "gif", document, dataDir);
ConvertPDFtoImage(pngDevice, "png", document, dataDir);
ConvertPDFtoImage(emfDevice, "emf", document, dataDir);
}
}
private static void ConvertPDFtoImage(ImageDevice imageDevice,
string ext, Document document, string dataDir)
{
for (int pageCount = 1; pageCount <= document.Pages.Count; pageCount++)
{
using (FileStream imageStream =
new FileStream($"{dataDir}image{pageCount}_out.{ext}",
FileMode.Create))
{
// Convert a particular page and save the image to stream
imageDevice.Process(document.Pages[pageCount], imageStream);
}
}
}
Try to convert PDF to PNG online
As an example of how our free applications work please check the next feature.
Aspose.PDF for .NET presents you online free application “PDF to PNG”, where you may try to investigate the functionality and quality it works.
Convert PDF using SaveOptions class
This part of article shows you how to convert PDF to SVG using C# and SaveOptions class.
Try to convert PDF to SVG online
Aspose.PDF for .NET presents you online free application “PDF to SVG”, where you may try to investigate the functionality and quality it works.
Scalable Vector Graphics (SVG) is a family of specifications of an XML-based file format for two-dimensional vector graphics, both static and dynamic (interactive or animated). The SVG specification is an open standard that has been under development by the World Wide Web Consortium (W3C) since 1999.
SVG images and their behaviors are defined in XML text files. This means that they can be searched, indexed, scripted and if required, compressed. As XML files, SVG images can be created and edited with any text editor, but it is often more convenient to create them with drawing programs such as Inkscape.
Aspose.PDF for .NET supports the feature to convert SVG image to PDF format and also offers the capability to convert PDF files to SVG format. To accomplish this requirement, the SvgSaveOptions
class has been introduced into the Aspose.PDF namespace. Instantiate an object of SvgSaveOptions and pass it as a second argument to the Document.Save(..)
method.
The following code snippet shows the steps for converting a PDF file to SVG format with .NET.
Steps: Convert PDF to SVG in C#
- Create an object of the Document class.
- Create SvgSaveOptions object with needed settings.
- Call the Document.Save() method and pass it SvgSaveOptions object convert the PDF document to SVG.
private static void ConvertPDFtoSVG()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Load PDF document using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document(dataDir + "PDFtoSVG.pdf"))
{
// Instantiate an object of SvgSaveOptions
var saveOptions = new Aspose.Pdf.SvgSaveOptions
{
// Do not compress SVG image to Zip archive
CompressOutputToZipArchive = false,
TreatTargetFileNameAsDirectory = true
};
// Save the output in SVG files
document.Save(dataDir + "PDFToSVG_out.svg", saveOptions);
}
}