2005 IR Help Posted August 21, 2006 Share Posted August 21, 2006 By: John Zoetebier - johnzoet Example setting print attributes 2004-01-17 02:01 In the mean time I figured out how to set print attributes like printer and page size. I have seen many, many questions unanswered on this forum. If everybody contributes the solution they found eventually, this may help others and yourself save some time . Code is below: /* * Created on Jan 17, 2004 * */ package test; import java.io.File; import java.util.Locale; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.HashPrintServiceAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.PrintServiceAttributeSet; import javax.print.attribute.standard.MediaSizeName; import javax.print.attribute.standard.OrientationRequested; import javax.print.attribute.standard.PrinterName; import util.Configuration; import dori.jasper.engine.JREmptyDataSource; import dori.jasper.engine.JRException; import dori.jasper.engine.JRExporterParameter; import dori.jasper.engine.JasperFillManager; import dori.jasper.engine.JasperPrint; import dori.jasper.engine.export.JRPrintServiceExporter; import dori.jasper.engine.export.JRPrintServiceExporterParameter; /** * @author John Zoetebier * */ public class TestPrintDialog { /** * */ public TestPrintDialog() { super(); } public void go() { // Check if jasper report is present // .jasper file can be created with iReports from xml report file // or java program using JasperCompileManager.compileReportToFile(fileName) String fileSeparator = System.getProperty("file.separator"); String jasperFileName = Configuration.getProperty("client.dir") + fileSeparator + "resource" + fileSeparator + "EnvelopReport.jasper"; File jasperFile = new File(jasperFileName); String msg = null; if (!jasperFile.exists()) { msg = "Cannot find jasper file: " + jasperFileName; System.out.println(msg); return; } JasperPrint jasperPrint = null; try { jasperPrint = JasperFillManager.fillReport( jasperFileName, null, new JREmptyDataSource()); } catch (JRException je) { System.out.println( "Error printing report:n" + je.getMessage()); return; } // For print attributes see API docs: j2sdk-1_4_2/docs/api/index.html PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(OrientationRequested.LANDSCAPE); printRequestAttributeSet.add(MediaSizeName.ISO_A4); PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); // Linux: print queues like "draft", "normal", "photo", etc // Windows: use physical printername like "Epson Stylus COLOR 680" or "HP LaserJet 5000 Series PCL" PrinterName printerName = new PrinterName("normal", Locale.getDefault()); printServiceAttributeSet.add(printerName); JRPrintServiceExporter exporter = new JRPrintServiceExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 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); try { exporter.exportReport(); } catch (JRException je) { System.out.println( "Error printing report:n" + je.getMessage()); } } public static void main(String[] args) { new TestPrintDialog().go(); } } By: Alexander Wallace - aows RE: Example setting print attributes 2004-01-17 07:50 You are absolutely right. I needed this example but gave up asking here becouse, out of 10 answers I?ve made (not counting the duplicates) i?ve only got a couple answered.... What you?ve done is a nice initiative, I shall follow it. Thanks! 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