gpt4 book ai didi

java - 将 Paper 设置为 PageFormatter 不会更新 PageFormatter

转载 作者:行者123 更新时间:2023-12-02 06:03:21 25 4
gpt4 key购买 nike

我正在尝试打印我的 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(页面格式化程序)中的纸张对象也不会更新。这是我的调试器的屏幕,显示该值尚未更改。

enter image description here

什么给出了?

最佳答案

好的,我已经尝试过这个了。所以使用你的例子我得到......

// 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)

这几乎就是您开始时设置的内容(注意,宽度和高度已交换,因为页面是横向的)...

所以...错误...欢迎来到为什么的奇妙世界??!?!

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

关于java - 将 Paper 设置为 PageFormatter 不会更新 PageFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com