Open source Java projects: SwingLabs PDF Renderer

View and render PDF content from your Java programs

1 2 3 4 5 Page 3
Page 3 of 5

Accessing document pages

PDF documents are organized into sequences of pages. You can obtain the number of pages in the current document by invoking PDFFile's public int getNumPages() method. Furthermore, you can access these pages by invoking PDFFile's public PDFPage getPage(int pagenum) and public PDFPage getPage(int pagenum, boolean wait) methods.

The former getPage() method invokes the latter getPage() method, which creates and returns a com.sun.pdfview.PDFPage instance. This instance contains the document commands for rendering the page identified by pagenum. Pass to pagenum any value from 1 through the value returned by getNumPages().

Behind the scenes, getPage() uses a com.sun.pdfview.PDFParser to parse the commands. Because parsing takes time, pass false to wait to have the parser offload parsing to a background thread -- the former getPage() method passes false to wait. Pass true to wait to have parsing take place on the current thread.

Terminate page parsing
If you need to stop a page's background thread that is in the midst of parsing the page's document commands, invoke PDFFile's public void stop(int pageNum) method with the page's number specified by pageNum.

PDFParser invokes PDFPage methods such as public void addCommands(PDFPage page), public void addPush(), and public void addImage(PDFImage image) to add commands to the PDFPage instance. Instead of working with these methods, your programs will work with other PDFPage methods, which you'll see at work in the next few sections.

Render to Images

Once you've obtained a PDFPage instance, you can render page content to a java.awt.Image by invoking PDFPage's public Image getImage(int width, int height, Rectangle2D clip, ImageObserver observer) and public Image getImage(int width, int height, Rectangle2D clip, ImageObserver observer, boolean drawbg, boolean wait) methods:

  • width and height identify the pixel dimensions of the created Image.
  • clip identifies that portion of the page rendered to the Image. Each of its field values is specified in terms of user-space units, as the pdf specification explains.
  • observer identifies an image observer that's notified as the image changes. Pass null to observer if you don't need to be notified.
  • drawbg specifies whether a white background (true) or no background (false) should be rendered behind the image. The former getImage() method, which invokes the latter getImage() method, passes true to drawbg.
  • wait specifies whether this method should not return until the image is fully rendered (true) or return immediately (false). The former getImage() method passes false to wait.

Before invoking getImage(), you'll need to obtain a suitable java.awt.geom.Rectangle2D value that you can pass to clip. For example, you can obtain a bounding box for the entire page (allowing you to render the entire page to an Image) by invoking PDFPage's public Rectangle2D getBBox() method.

1 2 3 4 5 Page 3
Page 3 of 5