2

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?

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
Ethyl Casin
  • 791
  • 2
  • 16
  • 34

2 Answers2

1

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.

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);
Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176