How to set printer margin in java

坚强是说给别人听的谎言 提交于 2020-01-20 08:07:24

问题


when trying to print using the Print API - the margins seem to be something not in our control!.. Please help me out to set printer margins or is there any way to set margins at Operating system level.

By default all the four left, right, top and bottom are set to 1.


回答1:


I'm using the Java Printing API, but have problems setting print margins.

...

Solution

I had to provide additional print attributes to override the default print margins, MediaPrintableArea.

Most printers cannot print on the entire surface of the media, due to printer hardware limitations.

MediaPrintableArea can be used to query the acceptable values for a supposed print job, and to request an area within the constraints of the printable area to be used in a print job.

HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(new MediaPrintableArea(0f, 0f, w/72f, h/72f, MediaPrintableArea.INCH));       
PrinterJob job = PrinterJob.getPrinterJob();    
job.setPrintService(ps);
job.setPrintable(this);
job.setJobName(jobName);
job.print(attr);
j.setVisible(false);
j.dispose();

The key was to provide the attributes along with the print() command.

Source Help with print margins



来源:https://stackoverflow.com/questions/25283110/how-to-set-printer-margin-in-java

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