我有一个奇怪的问题,似乎是 Mac 电脑特有的。我有一个程序可以将 AWT 绘图表面的内容打印到 A3 纸上。在 Linux 和 Windows 机器上,输出正常。从 Mac 打印 我得到相同的对话框和相同的预设参数,打印机按预期在 A3 纸上打印,但由于某种原因图像被缩放以适合 A4 区域。以下是代码的相关部分:
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat format = new PageFormat();
format.setOrientation(PageFormat.LANDSCAPE);
double margin = 18; // quarter inch
double m2 = margin * 2;
Paper paper = format.getPaper();
paper.setSize(16.54 * 72, 11.69 * 72); // A3 Landscape
paper.setImageableArea(margin, margin, paper.getWidth()-m2, paper.getHeight()-m2);
format.setPaper(paper);
printJob.setPrintable(this, format);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(MediaSizeName.ISO_A3);
attributes.add(OrientationRequested.LANDSCAPE);
attributes.add(new MediaPrintableArea((int)margin, (int)margin,
(int)(paper.getWidth()-m2), (int)(paper.getHeight()-m2),
MediaPrintableArea.INCH));
if(printJob.printDialog(attributes) ){
try {
printJob.print(attributes);
} catch(PrinterException pe) {
pe.printStackTrace();
}
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
if(this.componentToBePrinted instanceof PlannerView){
setScale(g2d);
}
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
public void setScale(Graphics2D g2d){
PlannerView planner = (PlannerView)this.componentToBePrinted;
planner.resetZoom();
double scale = 1.0 / planner.getRowLength();
scale *= 4.0;
g2d.scale(scale, scale);
}
有人知道是什么原因造成的吗?
干杯。
最佳答案
试着打电话
PageFormat newFormat =printJob.pageDialog(format);
打印前,打印机可能会修改边距。
至少看看调试器,看看它变成了什么。
另请注意,横向的 Macintosh native 打印坐标系是倒置的。
所以也许可以尝试使用 REVERSE_LANDSCAPE 而不是 LANDSCAPE(它可能会被翻转,但可能不会在翻译中得到列表)
关于Mac 上的 Java A3 打印以 A4 比例出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3768515/