Java PageFormat不能正确选择

时间:2019-04-01 07:09:52

标签: java

我正在尝试捕获用户对java printdialog所做的更改。我从未使用过它,并且对该组件没有很好的实践。现在,我可以捕捉到打印机名称的更改,但是没有其他更改。我可以通过代码获取和设置属性,但不能赶上用户的更改。

final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(DialogTypeSelection.NATIVE);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.printDialog(attributes);
PageFormat pf = printJob.defaultPage(); 
System.out.println(" orientation "+pf.getOrientation());

在打印机属性中进行选择后,我希望看到一个不同的方向值。 有人可以给我一些想法,或者提供一些精巧的文档来了解如何管理打印用户界面?

1 个答案:

答案 0 :(得分:0)

我为我的问题找到了解决方案。希望对我在相同情况下的其他人有用。

PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(DialogTypeSelection.NATIVE);
PrinterJob printJob = PrinterJob.getPrinterJob();

if (printJob.printDialog(attributes)) 
{
    PrintService ps=printJob.getPrintService();                 
    printerName=ps.getName();
    PageFormat pFormat = printJob.getPageFormat(attributes);
    System.out.println(" orientation "+pFormat.getOrientation());
}