How do I access the margins of the paper when using java print?
I am trying to print a JPanel with some drawings on it (overriding paintComponent). The graphics are so big that they won't fit on one page, and so I allow it to span multiple pages. My problem is that if I let the user select the Page Type / Paper Type by calling:
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf = printJob.pageDialog(aset);
printJob.setPrintable(canvas, pf);
When I write my print () method (implementing Printable) in my JPanel class, I can't seem to figure out that you have fields. I use graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
to start drawing at the correct angle (0; 0) and it takes margins into account (i.e. starting over (80; 100) or so). But then it prints the bottom and right edges, which I don't want to do as this negates the user's wishes.
Here is the code for my print () method as a reference, which works great 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;
}
}
source to share
After posting this question and iterating over the tabs in my IDE, I found the answer quite easily. Instead of using
double pageWidth = pageFormat.getPaper().getWidth();
double pageHeight = pageFormat.getPaper().getHeight();
use
double pageWidth = pageFormat.getImageableWidth();
double pageHeight = pageFormat.getImageableHeight();
getImageableWidth()
returns totalPaperWidth-totalMargins whereas it getWidth()
just returns totalPaperWidth. This makes the print () method not draw any more than it can on every page!
source to share