I have a JasperReports report for printing in landscape mode on a two-sided printer. On this I have to support PCL5 and PCL6 print drivers.
Searching the Internet, I found the following piece of code to complete this task:
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import javax.print.PrintService; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.OrientationRequested; import javax.print.attribute.standard.Sides; public class PrintingTest { public static void main(String[] args) { try { //This are for configuration purpose String orientation = "LANDSCAPE"; String duplexMode = "LONG_EDGE"; int pageOrientation = 0; PrintRequestAttributeSet atr = new HashPrintRequestAttributeSet(); if ("Landscape".equals(orientation)) { atr.add(OrientationRequested.LANDSCAPE); pageOrientation = PageFormat.LANDSCAPE; } else if ("Reverse_Landscape".equals(orientation)) { atr.add(OrientationRequested.REVERSE_LANDSCAPE); pageOrientation = PageFormat.REVERSE_LANDSCAPE; } else { atr.add(OrientationRequested.PORTRAIT); pageOrientation = PageFormat.PORTRAIT; } if ("LONG_EDGE".equals(duplexMode)) { atr.add(Sides.TWO_SIDED_LONG_EDGE); } else { atr.add(Sides.TWO_SIDED_SHORT_EDGE); } //Printing to the default printer PrintService printer = javax.print.PrintServiceLookup .lookupDefaultPrintService(); //Creating the printing job PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintService(printer); Book book = new Book(); PageFormat pageFormat = printJob.defaultPage(); pageFormat.setOrientation(pageOrientation); // Appending a exampledocument to the book book.append(new ExampleDocument(), pageFormat); // Appending another exampledocument to the book book.append(new ExampleDocument(), pageFormat); // Setting the Pageable to the printjob printJob.setPageable(book); try { // Here a could show the print dialog // printJob.printDialog(atr); // Here I pass the previous defined attributes printJob.print(atr); } catch (Exception PrintException) { PrintException.printStackTrace(); } } catch (PrinterException ex) { ex.printStackTrace(); } } public static final int MARGIN_SIZE = 72; private static class ExampleDocument implements Printable { public int print(Graphics g, PageFormat pageFormat, int page) { Graphics2D g2d = (Graphics2D) g; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // Only on the first two documents... if (page <= 1) { // Prints using one inch margin g2d.drawString("Printing page " + page + " - duplex...", MARGIN_SIZE, MARGIN_SIZE); return (PAGE_EXISTS); } return (NO_SUCH_PAGE); } } }
This works fine on PCL6 , but when testing on PCL5, I noticed that the LONG_EDGE and SHORT_EDGE rules are simply ignored. And in both cases, the job is sent as LONG_EDGE . This will not be a problem, except that the Java AWT Printing API allows landscape printing by rotating all pages 90 Β° counterclockwise, giving the impression that it was printed in SHORT_EDGE mode.
Obs: I was able to print correctly from both configurations, SHORT_EDGE and LONG_EDGE , in portrait and landscape orientations using the SWT API. But I canβt convert the print request for jasper into a print request compatible with SWT.
My question is: has anyone ever encountered such a situation? What decision was given to him?
From my observation, I found these possible solutions:
Instead of allowing AWT to turn the page and send a print request, force it to be sent as a landscape print request;
Find a way when the page is in landscape mode, invert the LONG_EDGE and SHORT_EDGE commands . Please note that for this I would be obligated to fix a problem in which both are treated as LONG_EDGE .
Convert JasperReports to use the SWT API.
source share