Ethyl Casin
Ethyl Casin

Reputation: 793

Set Printer PageLayout margin to zero

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?

Upvotes: 2

Views: 3144

Answers (2)

ItachiUchiha
ItachiUchiha

Reputation: 36742

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

Upvotes: 1

Nate Schellink
Nate Schellink

Reputation: 43

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.

Upvotes: 1

Related Questions