ImageGear Java PDF
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);
  1. Use the openDocument method of Document class to load PDF document.
  2. Use the getPage method of Document class to retrieve the specific page of the PDF document.
  3. Use the render method of Page class to render the content of the page to an uncompressed image. The render method has one parameter: 
    • The options parameter specifies control over some aspects of how the page is rendered to an image. See the RenderOptions class for details.

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();
                     }
              }
       }
}

See Also

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback