2005 IR Help Posted August 26, 2006 Share Posted August 26, 2006 By: Adrian Jetzer - tripletuned Landscape with JRPrintServiceExporter 2004-05-25 00:52 Hi there! I've got a problem with printing a report with landscape directy to the printer with the JRPrintServiceExporter. If I'm printing the same report with a preview dialog and print via the windows printer dialog, all works fine. Even export to PDF works and in iReport to. When I print the report with the JRPrintServiceExporter (set alle parameters right, I guess), I always get a left margin about 7cm. So there's always a huge withe space on the left, the report is not align left. The XML Header looks like: ... orientation="Landscape" pageWidth="842" pageHeight="595" ... Then I print with the following code: PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.ISO_A4); PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); printServiceAttributeSet.add(new PrinterName(drucker, null)); JasperPrint jasperPrint = (JasperPrint)dori.jasper.engine.util.JRLoader.loadObject(reportName); System.out.println(jasperPrint.getOrientation()); if (jasperPrint.getOrientation() == JRReport.ORIENTATION_LANDSCAPE) { printRequestAttributeSet.add(OrientationRequested.LANDSCAPE); } else { printRequestAttributeSet.add(OrientationRequested.PORTRAIT); } JRPrintServiceExporter exporter = new JRPrintServiceExporter(); exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, reportName); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE); exporter.exportReport(); Any help is welcome! Adrian By: Adrian Jetzer - tripletuned RE: Landscape with JRPrintServiceExporter 2004-05-28 01:31 Am I the only one on this planet with this problem? By: Lucian Comanescu - lucicom RE: Landscape with JRPrintServiceExporter 2004-05-28 04:16 Hi! >Am I the only one on this planet with this problem? No, don't cry so loud ... you're just one of the unlucky ones that are printing from java in a non-english installed printer (are you from Germany?). We had the same problem. It has something to do with the decimal separator, but we couldn't really trace it out. Our "solution" was to read the MediaPrintableArea values from the printer and use EXACTLY the same settings when printing from jasper. If JRPrintServiceExporter gets a MediaPrintableArea object it uses it (not creates another one that can have decimal values from x, y, h, w). And it worked :( Get the MediaPrintableArea from your printer: public void getMediaPrintableArea() { PrintRequestAttributeSet printRequestAttributeSet = null; PrintServiceAttributeSet printServiceAttributeSet = null; AttributeSet attributeSet = new HashAttributeSet(); printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.ISO_A4); printRequestAttributeSet.add(OrientationRequested.LANDSCAPE); printServiceAttributeSet = new HashPrintServiceAttributeSet(); attributeSet.addAll(printRequestAttributeSet); attributeSet.addAll(printServiceAttributeSet); PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet); for (int i = 0; i < services.length; i++) { System.out.println("exportReport:" + services.getName()); Object[] o = (Object[]) services.getSupportedAttributeValues(MediaPrintableArea.class, null, attributeSet); for (int j = 0; j < o.length; j++) { MediaPrintableArea pa = (MediaPrintableArea) o[j]; System.out.println("exportReport:" + "x=" + pa.getX(MediaPrintableArea.MM) + ", " + "y=" + pa.getY(MediaPrintableArea.MM) + ", " + "w=" + pa.getWidth(MediaPrintableArea.MM) + ", " + "h=" + pa.getHeight(MediaPrintableArea.MM)); } } } get the values and set the EXACTLY when you export the project: if (TASK_PRINT_SERVICE.equals(taskName)) { String jprint = DOWNLOAD_FOLDER + File.separator + reportName + ".jrprint"; JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(jprint); PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.ISO_A4); PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); //printServiceAttributeSet.add(new PrinterName("Epson Stylus 800 ESC/P 2", null)); //printServiceAttributeSet.add(new PrinterName("HP LaserJet 4P", null)); log.debug("Orientation: " + jasperPrint.getOrientation()); if (jasperPrint.getOrientation() == JRReport.ORIENTATION_LANDSCAPE) { printRequestAttributeSet.add(OrientationRequested.LANDSCAPE); printRequestAttributeSet.add(new MediaPrintableArea( // HP LaserJet 4100 PCL 6: x=4.233, y=4.233, w=200.829, h=287.782 4.233f, 4.233f, 200.829f, 287.782f, MediaPrintableArea.MM)); } else { printRequestAttributeSet.add(OrientationRequested.PORTRAIT); } JRPrintServiceExporter exporter = new JRPrintServiceExporter(); exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, jprint); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); exporter.exportReport(); log.info("Printing time : " + (System.currentTimeMillis() - start)); } I know it's not exactly what you wanted to hear, but maybe it helps. Lucian By: Adrian Jetzer - tripletuned RE: Landscape with JRPrintServiceExporter 2004-06-01 00:56 Hi Lucian Thanks a lot for your posting it worked! >>Am I the only one on this planet with this >>problem? >No, don't cry so loud ... you're just one of the >unlucky ones that are printing from java in a non->english installed printer (are you from Germany?). No, but I'm from switzerland. Well, I don't understand the problem, but your solution seems to work. So I'm lucky with it. Thanks again!! Greetings, Triple Tuned check out my latest tracks at: http://www.tripletuned.com/album.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now