File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes Printing multiple pages in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Printing multiple pages in Java" Watch "Printing multiple pages in Java" New topic

Printing multiple pages in Java

Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
I wrote a help viewer with JavaHelp API's. The html help files are displayed in JHelpContentViewer, which is a sub-class of JComponent. I want to print the contents in JHelpContentViewer. I don't know how many pages they will be, because different html pages will have different lenghts. When I try to print them, I am able to print only the first page. Other pages comes as blank. Here is my source code:
public int
print(Graphics g, PageFormat pf, int pi) throws PrinterException
Graphics g2d = (Graphics2D) g.create();
int x = (int)pf.getImageableX();
int y = (int)pf.getImageableY();
int hPage = (int)pf.getImageableHeight();
int wPage = (int)pf.getImageableWidth();
if (pi == 0)
// In this example, I'm putting 3, how can
// I find this value
if (pi >= 3)
return NO_SUCH_PAGE;
return Printable.PAGE_EXISTS;
public static void
printScreen(Printable printable)
PrinterJob printJob = PrinterJob.getPrinterJob();
if (pf == null)
pf = printJob.defaultPage();

if (printJob.printDialog())
catch (Exception PrintException)
Here only the first page gets printed? The remaining pages are printed blank. Also, I need to know how many pages are there, so that I can return NO_SUCH_PAGE appropriately.
Any help would be greatly appreciated.
- Raja.

garrett sudicky

Joined: Jul 10, 2013
Posts: 7
Im not sure if you will ever see this seeing how this is 10 years later but im doing it now and i found the height of the textarea im using and then compared that to the imageable area in a for statement.
I agree. Here's the link:
subject: Printing multiple pages in Java
It's not a secret anymore!