1

I'm printing a Jasper report to a Zebra label printer. The label has a QR code and a number. If I export the report to a PDF file and print it, it prints fine, but if I print it directly from the Java app, the numbers aren't shown. Upon further testing, if I print it directly but if I show the printing dialog and if I erase the margins, then it prints properly as well. I noticed that the dialog adds very significant margins to the report: right (mm): 191,21 and bottom (mm) 229,66.

The report itself doesn't have any margins, and indeed it prints as expected as a pdf file:

leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0"

But it seems like they get added when printing with JRPrintServiceExporter with the following code:

JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, rpt);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, true); // won't work if false
exporter.exportReport();

How do I remove margins programatically so that I can bypass the printing dialog?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Smig
  • 683
  • 6
  • 17
  • Did you try to play with OffsetX/OffsetY? – Alex K Dec 30 '19 at 18:56
  • 1
    You can look at: [Changing the report margins using the API](https://community.jaspersoft.com/questions/522037/changing-report-margins-using-api) & [Margins in 'Page Setup'](https://community.jaspersoft.com/questions/520305/margins-page-setup) & [How to set printer margin in java](https://stackoverflow.com/questions/25283110/how-to-set-printer-margin-in-java) & [Making margins smaller - Java Printing](https://stackoverflow.com/q/7535443/876298) & [Java printing: creating a PageFormat with minimum acceptable margin](https://stackoverflow.com/q/10455268/876298) – Alex K Dec 30 '19 at 18:59
  • The offset question in the first link is actually from me, from 11 years ago. Wow... Anyway, I found a solution on the 3rd link you offered. Thanks! – Smig Dec 30 '19 at 20:32

1 Answers1

0

Thanks to one of the links kindly offered by Alex K, in the comments, I was able to get it to work by adding a new MediaPrintableArea with the exact dimensions I needed:

PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
printReqAttr.add(new MediaPrintableArea(0f, 0f, 50f, 25f, MediaPrintableArea.MM));
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printReqAttr);
Smig
  • 683
  • 6
  • 17