我正在尝试打印我的Jpanel。我大部分时间都在工作。现在,我正在尝试自定义一些内容,例如边距。

这是我的代码

PrinterJob pjob = PrinterJob.getPrinterJob();

//*
PageFormat pf0 = pjob.defaultPage();
Paper p = pf0.getPaper();

//hardcode the page imageable area for testing. The sizes are valid ones i pulled from calling printerJob.getDialog(attr)...)
p.setImageableArea(28, 28, 556, 734);
pf0.setPaper(p);

//set the attributes for the formatter
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(Fidelity.FIDELITY_TRUE);
attr_set.add(PrintQuality.HIGH);
attr_set.add(PrintQuality.HIGH);
attr_set.add(OrientationRequested.LANDSCAPE);

//set the printable area
int width = Math.round(MediaSize.NA.LETTER.getX(MediaSize.MM));
int height = Math.round(MediaSize.NA.LETTER.getY(MediaSize.MM));
attr_set.add(new MediaPrintableArea(10, 10, width - 20, height - 20, MediaPrintableArea.MM));

//update the page formatter
pf0 = pjob.getPageFormat(attr_set);


即使我正在调用pf0.setPaper(p),在pf0(页面格式化程序)中的纸张对象也不会更新。这是调试器的屏幕,显示该值未更改。



是什么赋予了?

最佳答案

好吧,所以我玩了这个。因此,使用您的示例,我得到...

// Paper.setImageableArea
28 pixels = 0.99 cm (x)
28 pixels = 0.99 cm (y)
556 pixels = 19.653 cm (width)
734 pixels = 25.945 cm (height)

// PrintRequestAttributeSet
29.528 pixels = 1 cm (x)
29.528 pixels = 1 cm (y)
578.74 pixels = 19.6 cm (width)
764.764 pixels = 25.9 cm (height)

// Page format AFTER getting it from the PrintJob
72 pixels = 2.545 cm (x)
72 pixels = 2.545 cm (y)
697.918 pixels = 24.669 cm (width)
451.332 pixels = 15.953 cm (height)


显然,这不是您的期望。

接下来,我在attr_set.add(MediaSizeName.NA_LETTER);中添加了PrintRequestAttributeSet

// Page format AFTER getting it from the PrintJob
22.082 pixels = 0.781 cm (x)
72 pixels = 2.545 cm (y)
697.918 pixels = 24.669 cm (width)
451.332 pixels = 15.953 cm (height)


仍然不是您所期望的...

现在,如果我使用pf0 = pjob.pageDialog(attr_set);而不是pf0 = pjob.getPageFormat(attr_set);

// Page format AFTER getting it from the PrintJob
30 pixels = 1.06 cm (x)
28 pixels = 0.99 cm (y)
734 pixels = 25.945 cm (width)
556 pixels = 19.653 cm (height)


这几乎就是您要设置的内容(请注意,由于页面处于横向状态,宽度和高度已交换)...

所以... bug ...欢迎来到WHY的美好世界??!?!

经过一番挖掘之后,我发现MediaPrintableArea被忽略了,并且在某些情况下是由于调用service.isAttributeValueSupported(orientReq, null, attributes)的原因,其中servicePrintService。这可能与默认打印机有关……?

07-26 04:38