如何更改PageFormat纸质对象的属性?

时间:2017-07-06 07:30:43

标签: java printing

我试图获取默认打印选项,但我想手动设置宽度

PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pf = printJob.defaultPage();
pf.getPaper().setSize(200, pf.getHeight());

打印出pf.getPaper()的变量(width,height,imagableArea width和height)我设置大小的行似乎没有效果。

1 个答案:

答案 0 :(得分:0)

在java.awt.print.PageFormat:

的文档中找到了这个
public Paper getPaper() 
Returns a copy of the Paper object associated with this PageFormat. 
Changes made to the Paper object returned from this method do not affect 
the Paper object of this PageFormat. To update the Paper object of this 
PageFormat, create a new Paper object and set it into this PageFormat by 
using the #setPaper(Paper) method

所以该方法应该只提供纸质对象的副本。 改变大小的正确方法是

//Get the printer job
PrinterJob printJob = PrinterJob.getPrinterJob();
//Get the default PageFormat
PageFormat pf = printJob.defaultPage();
//Get a copy of the formats paper object
Paper copy = pf.getPaper();
//Change properties of the copy
copy.setSize(200, pf.getHeight());
//Since we only changed the copy, we need to set the PageFormats Paper Object to the copy
pf.setPaper(copy);