views:

26

answers:

1

I'm trying to print a JPanel with some painted graphics on it (overriding paintComponent). The graphics is so big that they wont fit on a single page and therefor I'm letting it span across multiple pages. My problem lies within the fact that if I let the user choose the pageFormat/Paper type by calling:

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf = printJob.pageDialog(aset);
printJob.setPrintable(canvas, pf);

When I'm writing my print() method (implementing Printable) in my JPanel class I can't seem to get the hold of the margins? I use graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); to make it start drawing in the correct topleft corner (0;0) and it takes the margins into consideration (i.e.,starting more at (80; 100) or so). But then it prints over the bottom and right margin which I don't want it to do since that negates the user's wishes.

Here is the code of my print() method as a reference, which works fine when you don't let the user set the paper (using the default instead):

    Rectangle[] pageBreaks;

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        //Calculate how many pages our print will be
        if(pageBreaks == null){
            double pageWidth = pageFormat.getPaper().getWidth();
            double pageHeight = pageFormat.getPaper().getHeight();

            //Find out how many pages we need
            int numberOfPagesHigh = (int) Math.ceil(size.getHeight()/pageHeight);
            int numberOfPagesWide = (int) Math.ceil(size.getWidth()/pageWidth); 
            pageBreaks = new Rectangle[numberOfPagesHigh*numberOfPagesWide];

            double x = 0;
            double y = 0;
            int curXPage = 0;

            //Calculate what we will print on each page
            for (int i = 0; i < pageBreaks.length; i++){
                double xStart = x;
                double yStart = y;
                x += pageWidth;

                pageBreaks[i] = new Rectangle((int)xStart, (int)yStart, (int)pageWidth, (int)pageHeight);

                curXPage++;
                if (curXPage > numberOfPagesWide){
                    curXPage = 0;
                    x = 0;
                    y += pageHeight;
                }
            }


        }

        if (pageIndex < pageBreaks.length){
            //Cast graphics to Graphics2D for richer API
            Graphics2D g2d = (Graphics2D) graphics;

            //Translate into position of the paper
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            //Setup our current page
            Rectangle rect = pageBreaks[pageIndex];

            g2d.translate(-rect.x, -rect.y);
            g2d.setClip(rect.x, rect.y, rect.width, rect.height);

            //Paint the component on the graphics object
            Color oldBG = this.getBackground();
            this.setBackground(Color.white);
            util.PrintUtilities.disableDoubleBuffering(this);
            this.paintComponent(g2d);
            util.PrintUtilities.enableDoubleBuffering(this);
            this.setBackground(oldBG);

            //Return
            return PAGE_EXISTS;
        }
        else {
            return NO_SUCH_PAGE;
        }
   }
+1  A: 

After posting this question and tabbing back into my IDE I pretty easily found the answer. Instead of using

double pageWidth = pageFormat.getPaper().getWidth();
double pageHeight = pageFormat.getPaper().getHeight(); 

use

double pageWidth = pageFormat.getImageableWidth();
double pageHeight = pageFormat.getImageableHeight();

The getImageableWidth() returns the totalPaperWidth-totalMargins whereas getWidth() just returns totalPaperWidth. This makes the print() method not draw more that it can on each page!

Snail