페인트 된 그래픽이 포함 된 JPanel을 paint하려고합니다 (paintComponent를 재정의). 그래픽은 너무 크기 때문에 한 페이지에 맞지 않아 여러 페이지에 걸쳐 표시됩니다.자바 인쇄를 사용할 때 용지 여백을 어떻게 잡습니까?
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf = printJob.pageDialog(aset);
printJob.setPrintable(canvas, pf);
을 내가 할 수없는 내 JPanel의 클래스 (인쇄 구현) 내 인쇄() 메소드를 쓰고 있어요 경우 : 내 문제는 호출하여 내가 할 경우 사용자가 PageFormat에/용지 유형을 선택한다는 사실 내에있는 마진을 유지하는 것 같습니까? 나는 graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
을 사용하여 올바른 토플렛 코너 (0; 0)에서 그리기를 시작하고 여백을 고려합니다 (즉, (80,100) 정도에서 시작). 그러나 그것은 사용자의 희망을 무효화하기 때문에 내가하지 않기를 바라는 아래쪽과 오른쪽 여백 위로 인쇄합니다.
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;
}
}