ImageGear Java PDF > How to... > Render a PDF Page |
To render the contents of the PDF document page to an uncompressed image use render method of the Page class:
Copy Code | |
---|---|
byte[] render(RenderOptions options); |
The following is an illustration of how to render a page of a PDF document to a BufferedImage:
Copy Code | |
---|---|
import com.accusoft.imagegearpdf.*; import java.awt.image; import javax.imageio; import java.io.*; class PdfDemo { private PDF pdf; private Document document; // Save BufferedImage to a PNG file. public void saveImageToPngFile(BufferedImage bufferedImage, String imageFilename) { File outputFile = new File(imageFilename); ImageIO.write(bufferedImage, "png", outputFile); } // Render PDF document page to a BufferedImage. public BufferedImage renderPage(long pageNumber) { Page page = null; try { // Retrieve specific page to render. page = document.getPage(pageNumber); // Prepare render options. RenderOptions options = new RenderOptions(); options.setResolution(200); options.setSmoothingFlags(SmoothingFlags.SmoothText | SmoothingFlags.SmoothLineArt); // Render page to a byte array. byte[] content = page.render(options); // Convert byte array to a BufferedImage. ByteArrayInputStream imageStream = new ByteArrayInputStream(content); BufferedImage bufferedImage = ImageIO.read(imageStream); return bufferedImage; } catch (Throwable ex) { // Failed to render a page. System.err.println("Exception: " + ex.toString()); return null } finally { if (page != null) { // Close PDF page as it is not needed anymore. page.close(); } } } } |