views:

81

answers:

2

Hello,

My application allows the user to print the screen content which is made of several Swing controls (images, texts, etc.) - the printed panel it self is long (several pages) and the width about 600pixels.

Since I know exactly each page layout (I put the exact number of images and components) on each page (the total number of images is dynamic), I can calculate exactly how many pages I will need for the print.

I've implemented the Printable interface as follows:

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.BLACK);    //set default foreground color to black
    RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
    Dimension d = this.getSize();    //get size of document
    double panelWidth = d.width;    //width in pixels
    double panelHeight = d.height;   //height in pixels
    double pageHeight = pf.getImageableHeight();   //height of printer page
    double pageWidth = pf.getImageableWidth();    //width of printer page
    double scale = pageHeight / panelHeight;
    int totalNumPages = (int) Math.ceil(scale * panelWidth / pageWidth);

    if (pageIndex >= totalNumPages) {
            return Printable.NO_SUCH_PAGE;
    }


    g2.translate(pf.getImageableX(), pf.getImageableY());
    g2.translate(0f, -pageIndex * pageHeight);
    g2.scale(scale, scale);
    this.paint(g2);
    return Printable.PAGE_EXISTS;
}

It seems that everything works fine. Since I don't have a printer, I've tested it using a PDF printer and on several clients printer - and the results were good.

However, on one of my user's printer - it seems that something in not working with the calculation, since it breaks the page in the middle of the images.

Is there a more elegant way to do these calculations ? Can I add instead a "page break" notification somewhere ?

Thanks

+1  A: 

In the past I have found that trying to implement the printable interface myself has caused formatting issues such as you are experiencing. One thing I have done in the past that greatly reduced the effort on my part, and worked well, was using JasperReports to manage this. You could create a jrxml definition that has one image/page, and then just pass the images into the jasperreport. From here you can use the JasperPrint object and send this directly to the printer. This has worked well for me. In addition it has several benefits with the exports already build in to jasper reports such as pdf if needed.

broschb
A: 

The code itself looks ok, especially considering it works for the PDF printer. I just have some ideas for debugging:

1) Have you checked the printer settings for the user's printer? Maybe the page size is not set correctly. 2) Any way you could hook up your development machine to that printer and debug it?
3) If not, you could add logging to tell you the pg.getImageableWidth and pg.getImageableHeight and other information throughout the calculation to see where it's wrong.

Amber Shah