Set Printer PageLayout margin to zero

不打扰是莪最后的温柔 提交于 2019-12-11 12:51:59

问题


I created a small Javafx application that allows the user to print a node. I want the margin to be zero. I used the following code below, but it didn't work.

printer = javafx.print.Printer.getDefaultPrinter();
pageLayout = printer.createPageLayout(Paper.JAPANESE_POSTCARD,  PageOrientation.PORTRAIT, 0.0f, 0.0f, 0.0f, 0.0f);

According to the documentation, the last four parameters in createPageLayout is the margins(Left,Right,Top,Bottom). I made a print-test but there still a 0.5 inch margin in the printed document.

What's going on? Any Idea?


回答1:


You are facing a hardware limitation issue i.e. the margin that can be applied to a Paper depends on the hardware / printer and not just the API used to access it. In this case, though JavaFX allows you to pass margin values as 0, but they are later re-assigned to support the printers minimum values.

From the Javadocs :

A client that needs to know what margin values are legal should first obtain a PageLayout using the HARDWARE_MINIMUM margins.

If the printer cannot support the layout as specified, it will adjust the returned layout to a supported configuration

You can use Printer.MarginType.HARDWARE_MINIMUM while creating a layout, to check the minimum margin allowed.

pageLayout = printer.createPageLayout(Paper.JAPANESE_POSTCARD,  
             PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);



回答2:


Try the getPrintableWidth() and getPrintableHeight() methods to check what the actual margins are. Even though you set a 0 margin in the constructor, the hardware may limit the margins to a default size.



来源:https://stackoverflow.com/questions/30206149/set-printer-pagelayout-margin-to-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!