This technical tip shows how to convert PDF pages to PNG Image using Aspose.Pdf for Java. Users can choose to convert a particular PDF page to PNG image or convert all PDF pages to PNG Images. In order to convert all page of PDF file to PNG format, you need to iterate through individual page and convert it to PNG format. The given code snippet shows you how to traverse through all the pages of PDF file and convert it to PNG image. The PngDevice class allows you to convert PDF pages to PNG images. This class provides a method named process(..) which allows you to convert a particular page of the PDF file to PNG image. You first need to create an object of Document class, so you could get the particular page which you want to convert to PNG. After that, you need to call the process(..) method to convert the page to PNG image.
Convert particular PDF page to PNG Image
[Java]
//open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
// create stream object to save the output image
java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.png");
//create Resolution object
com.aspose.pdf.Resolution resolution = new com.aspose.pdf.Resolution(300);
//create PngDevice object with particular resolution
com.aspose.pdf.PngDevice pngDevice = new com.aspose.pdf.PngDevice(resolution);
//convert a particular page and save the image to stream
pngDevice.process(pdfDocument.getPages().get_Item(1), imageStream);
//close the stream
imageStream.close();
Convert all PDF pages to PNG Images
[Java]
//open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
// loop through all the pages of PDF file
for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++)
{
// create stream object to save the output image
java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".png");
//create Resolution object
com.aspose.pdf.Resolution resolution = new com.aspose.pdf.Resolution(300);
//create PngDevice object with particular resolution
com.aspose.pdf.PngDevice pngDevice = new com.aspose.pdf.PngDevice(resolution);
//convert a particular page and save the image to stream
pngDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);
//close the stream
imageStream.close();
}
Overview: Aspose.Pdf for Java
Aspose.Pdf is a Java PDF component to create PDF documents without using Adobe Acrobat. It supports Floating box, PDF form field, PDF attachments, security, Foot note & end note, Multiple columns document, Table of Contents, List of Tables, Nested tables, Rich text format, images, hyperlinks, JavaScript, annotation, bookmarks, headers, footers and many more. Now you can create PDF by API, XML and XSL-FO files. It also enables you to converting HTML, XSL-FO and Excel files into PDF.
More about Aspose.Pdf for Java
- Homepage of Aspose.Pdf is a Java: http://www.aspose.com/java/pdf-component.aspx
- Read More Technical Tips by Aspose.Pdf is a Java at: http://www.aspose.com/docs/display/pdfjava/Working+with+com.aspose.pdf
- Download Aspose.Pdf is a Java: http://www.aspose.com/community/files/72/java-components/aspose.pdf-for-java/default.aspx
|