private void printText2Action() { printStr = area.getText().trim(); if(printStr != null && printStr.length() > 0) { PAGES = getPagesCount(printStr); DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob job = printService.createPrintJob(); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(this, flavor, das); try { job.print(doc, pras); } catch(PrintException pe) { pe.printStackTrace(); } } else { JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty", -1, 2); } }
/** * Creates a new <code>PrintHelper</code> instance using the specified * doc and print attribute sets. This constructor offers the most flexibility * as it allows the attributes sets to be pre configured. This method * should only be used by advanced users. * * @param container parent container uses to center print dialog. * @param pageTree document page tree. * @param userRotation rotation of view * @param docAttributeSet MediaSizeName constant of paper size to print to. * @param printRequestAttributeSet quality of the print job, draft, quality etc. */ public PrintHelper(Container container, PageTree pageTree, float userRotation, HashDocAttributeSet docAttributeSet, HashPrintRequestAttributeSet printRequestAttributeSet) { this.container = container; this.pageTree = pageTree; this.userRotation = userRotation; // blindly assign doc and print attribute sets. this.docAttributeSet = docAttributeSet; this.printRequestAttributeSet = printRequestAttributeSet; // find available printers services = lookForPrintServices(); // default setup, all pages, shrink to fit and no dialog. setupPrintService(0, this.pageTree.getNumberOfPages(), 1, true, false); }
public static void main(String[] args) throws Exception { GraphicsEnvironment.getLocalGraphicsEnvironment(); DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; String mime = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType(); StreamPrintServiceFactory[] factories = StreamPrintServiceFactory. lookupStreamPrintServiceFactories(flavor, mime); if (factories.length == 0) { System.out.println("No print service found."); return; } FileOutputStream output = new FileOutputStream("out.ps"); StreamPrintService service = factories[0].getPrintService(output); SimpleDoc doc = new SimpleDoc(new PrintSEUmlauts(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, new HashDocAttributeSet()); DocPrintJob job = service.createPrintJob(); job.addPrintJobListener(new PrintJobAdapter() { @Override public void printJobCompleted(PrintJobEvent pje) { testPrintAndExit(); } }); job.print(doc, new HashPrintRequestAttributeSet()); }
public void printContent() { printStr = "打印测试内容";// 获取需要打印的目标文本 if (printStr != null && printStr.length() > 0) // 当打印内容不为空时 { PAGES = 1; // 获取打印总页数 // 指定打印输出格式 DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); // 定位默认的打印服务 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); // Toolkit.getDefaultToolkit().getPrintJob // 创建打印作业 PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras); //PrintService service = ServiceUI.printDialog(null, 200, 200, printService, printService , flavor, pras); //DocPrintJob job = printService.createPrintJob(); DocPrintJob job = service.createPrintJob(); // 设置打印属性 // PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); // 设置纸张大小,也可以新建MediaSize类来自定义大小 pras.add(MediaSizeName.ISO_A4); DocAttributeSet das = new HashDocAttributeSet(); // 指定打印内容 Doc doc = new SimpleDoc(this, flavor, das); // 不显示打印对话框,直接进行打印工作 try { job.print(doc, pras); // 进行每一页的具体打印操作 } catch (PrintException pe) { pe.printStackTrace(); } } else { // 如果打印内容为空时,提示用户打印将取消 JOptionPane.showConfirmDialog(null,"Sorry, Printer Job is Empty, Print Cancelled!", "Empty", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE); } }
private void printFileAction() { // 构造一个文件选择器,默认为当前目录 JFileChooser fileChooser = new JFileChooser(); int state = fileChooser.showOpenDialog(null);//弹出文件选择对话框 if (state == fileChooser.APPROVE_OPTION)//如果用户选定了文件 { File file = fileChooser.getSelectedFile();//获取选择的文件 //构建打印请求属性集 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); //设置打印格式,因为未确定文件类型,这里选择AUTOSENSE DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; //查找所有的可用打印服务 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); //定位默认的打印服务 PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); //显示打印对话框 PrintService service = ServiceUI.printDialog(null, 200, 200, printService , defaultService, flavor, pras); if (service != null) { try { DocPrintJob job = service.createPrintJob();//创建打印作业 FileInputStream fis = new FileInputStream(file);//构造待打印的文件流 DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(fis, flavor, das);//建立打印文件格式 job.print(doc, pras);//进行文件的打印 } catch(Exception e) { e.printStackTrace(); } } } }
private void printFileAction() { JFileChooser fileChooser = new JFileChooser(System.getProperty("USER_DIR")); // fileChooser.setFileFilter(new JavaFilter()); int state = fileChooser.showOpenDialog(this); if(state == 0) { File file = fileChooser.getSelectedFile(); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras); if(service != null) try { DocPrintJob job = service.createPrintJob(); FileInputStream fis = new FileInputStream(file); DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(fis, flavor, das); job.print(doc, pras); } catch(Exception e) { e.printStackTrace(); } } }
public void testLookupMultiDocPrintServices() throws Exception { System.out .println("============= START LookupMultiDocPrintServicesTest ================"); DocFlavor psFlavor = DocFlavor.INPUT_STREAM.GIF; MultiDocPrintService[] services; FileInputStream fis; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; aset.add(new Copies(2)); aset.add(MediaSizeName.ISO_A4); daset.add(MediaName.ISO_A4_WHITE); daset.add(Sides.TWO_SIDED_LONG_EDGE); services = PrintServiceLookup.lookupMultiDocPrintServices( new DocFlavor[] { psFlavor }, aset); if (services != null && services.length > 0) { fis = new FileInputStream("/Resources/1M.GIF"); doc = new SimpleDoc(fis, psFlavor, daset); pj = services[0].createPrintJob(); pj.print(doc, aset); System.out.println(fis.toString() + " printed on " + services[0].getName()); } else { System.out.println("services not found"); } System.out.println("============= END LookupMultiDocPrintServicesTest ================"); }
public void testPrintJpeg() throws Exception { System.out.println("======== START PrintJpegTest ========"); PrintService[] services; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; daset.add(MediaSizeName.ISO_A4); DocFlavor df = DocFlavor.INPUT_STREAM.JPEG; InputStream fis = this.getClass().getResourceAsStream( "/Resources/JPEG.jpg"); services = PrintServiceLookup.lookupPrintServices(df, aset); TestUtil.checkServices(services); for (int j = 0; j < services.length; j++) { PrintService printer = services[j]; if (printer.toString().indexOf("print-to-file") >= 0) { doc = new SimpleDoc(fis, df, daset); pj = printer.createPrintJob(); pj.print(doc, aset); System.out.println(fis.toString() + " printed on " + printer.getName()); break; } } System.out.println("====== END PrintJpegTest ========"); }
public void testLookupDefaultPrintService() throws Exception { System.out .println("======= START LookupDefaultPrintServiceTest ======"); DocFlavor psFlavor = DocFlavor.INPUT_STREAM.GIF; PrintService service; InputStream fis; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; aset.add(new Copies(2)); aset.add(MediaSizeName.ISO_A4); daset.add(MediaName.ISO_A4_WHITE); daset.add(Sides.TWO_SIDED_LONG_EDGE); service = PrintServiceLookup.lookupDefaultPrintService(); if (service != null) { if (service.isDocFlavorSupported(psFlavor)) { if (service.getUnsupportedAttributes(psFlavor, aset)==null) { fis = this.getClass().getResourceAsStream( "/Resources/GIF.gif"); doc = new SimpleDoc(fis, psFlavor, daset); pj = service.createPrintJob(); pj.print(doc, aset); System.out.println(fis.toString() + " printed on " + service.getName()); } } else { System.out.println("flavor is not supported"); } } else { System.out.println("service not found"); } System.out.println("======= END LookupDefaultPrintServiceTest ======="); }
public void testSimpleDoc() { startTest("SimpleDoc class testing..."); DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF; InputStream reader = getClass().getResourceAsStream("/Resources/picture.gif"); if (reader == null) { fail("/Resources/picture.gif resource is not found!"); } DocAttributeSet aSet = new HashDocAttributeSet(); Doc doc = new SimpleDoc(reader, flavor, aSet); assertEquals(doc.getAttributes(), aSet); aSet.add(OrientationRequested.LANDSCAPE); aSet.add(MediaName.NA_LETTER_WHITE); assertEquals(doc.getAttributes(), aSet); assertEquals(doc.getDocFlavor(), DocFlavor.INPUT_STREAM.GIF); try { assertTrue(doc.getPrintData() instanceof java.io.InputStream); assertNull(doc.getReaderForText()); assertEquals(doc.getStreamForBytes(), reader); } catch(Exception e) { e.printStackTrace(); fail("Exception found: "+e); } }
public void testPrintAutosense() throws Exception { System.out.println("======== START PrintAutosenseTest ========"); PrintService[] services; HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; daset.add(MediaSizeName.ISO_A4); DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE; InputStream fis = this.getClass().getResourceAsStream( "/Resources/hello_ps.ps"); services = PrintServiceLookup.lookupPrintServices(df, null); TestUtil.checkServices(services); for (int j = 0; j < services.length; j++) { PrintService printer = services[j]; if (printer.toString().indexOf("print-to-file") >= 0) { doc = new SimpleDoc(fis, df, null); pj = printer.createPrintJob(); pj.print(doc, null); System.out.println(fis.toString() + " printed on " + printer.getName()); break; } } System.out.println("====== END PrintAutosenseTest ========"); }
public void testLookupPrintServices() throws Exception { System.out.println("======== START LookupPrintServicesTest ========"); PrintService[] services; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; Object[][] filetoprint = { { "/Resources/JPEG.jpg", DocFlavor.INPUT_STREAM.JPEG }, { "/Resources/GIF.gif", DocFlavor.INPUT_STREAM.GIF } }; DocFlavor df; InputStream fis; for (int i = 0; i < filetoprint.length; i++) { df = (DocFlavor) filetoprint[i][1]; services = PrintServiceLookup.lookupPrintServices(df, aset); TestUtil.checkServices(services); for (int j = 0; j < services.length; j++) { fis = this.getClass().getResourceAsStream( (String) filetoprint[i][0]); doc = new SimpleDoc(fis, df, daset); PrintService printer = services[j]; pj = printer.createPrintJob(); pj.print(doc, aset); System.out.println(fis.toString() + " printed on " + printer.getName()); } } System.out.println("====== END LookupPrintServicesTest ========"); }
public void testLookupMultiDocPrintServices() { System.out .println("============= START LookupMultiDocPrintServicesTest ================"); DocFlavor psFlavor = DocFlavor.INPUT_STREAM.GIF; MultiDocPrintService[] services; FileInputStream fis; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; aset.add(new Copies(2)); aset.add(MediaSizeName.ISO_A4); daset.add(MediaName.ISO_A4_WHITE); daset.add(Sides.TWO_SIDED_LONG_EDGE); try { services = PrintServiceLookup.lookupMultiDocPrintServices( new DocFlavor[] { psFlavor }, aset); if (services != null && services.length > 0) { fis = new FileInputStream("/Resources/1M.GIF"); doc = new SimpleDoc(fis, psFlavor, daset); pj = services[0].createPrintJob(); pj.print(doc, aset); System.out.println(fis.toString() + " printed on " + services[0].getName()); } else { System.out.println("services not found"); } } catch (Exception e) { e.printStackTrace(); fail(); } System.out .println("============= END LookupMultiDocPrintServicesTest ================"); }
public void testPrintJpeg() { System.out.println("======== START PrintJpegTest ========"); PrintService[] services; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; daset.add(MediaSizeName.ISO_A4); try { DocFlavor df = DocFlavor.INPUT_STREAM.JPEG; InputStream fis = this.getClass().getResourceAsStream( "/Resources/JPEG.jpg"); services = PrintServiceLookup.lookupPrintServices(df, aset); TestUtil.checkServices(services); for (int j = 0; j < services.length; j++) { PrintService printer = services[j]; if (printer.toString().indexOf("print-to-file") >= 0) { doc = new SimpleDoc(fis, df, daset); pj = printer.createPrintJob(); pj.print(doc, aset); System.out.println(fis.toString() + " printed on " + printer.getName()); break; } } } catch (Exception e) { e.printStackTrace(); fail(); } System.out.println("====== END PrintJpegTest ========"); }
public void testPrintAutosense() { System.out.println("======== START PrintAutosenseTest ========"); PrintService[] services; HashDocAttributeSet daset = new HashDocAttributeSet(); DocPrintJob pj; Doc doc; daset.add(MediaSizeName.ISO_A4); try { DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE; InputStream fis = this.getClass().getResourceAsStream( "/Resources/hello_ps.ps"); services = PrintServiceLookup.lookupPrintServices(df, null); TestUtil.checkServices(services); for (int j = 0; j < services.length; j++) { PrintService printer = services[j]; if (printer.toString().indexOf("print-to-file") >= 0) { doc = new SimpleDoc(fis, df, null); pj = printer.createPrintJob(); pj.print(doc, null); System.out.println(fis.toString() + " printed on " + printer.getName()); break; } } } catch (Exception e) { e.printStackTrace(); fail(); } System.out.println("====== END PrintAutosenseTest ========"); }
public DocAttributeSet getAttributes() { return new HashDocAttributeSet(); }
/************************************************************************ * 打印 */ public void print() throws Exception { try { //搜寻打印机 PrintService printer = this.lookupPrinter(); if (printer == null) { throw new NullPointerException("printer == null. 没有找到默认打印机!!!"); } //日志输出打印机的各项属性 AttributeSet attrs = printer.getAttributes(); logger.info("****************************************************"); for (Attribute attr : attrs.toArray()) { String attributeName = attr.getName(); String attributeValue = attrs.get(attr.getClass()).toString(); logger.info("*"+attributeName + " : " + attributeValue); } logger.info("****************************************************"); //创建打印数据 // DocAttributeSet docAttr = new HashDocAttributeSet();//设置文档属性 // Doc myDoc = new SimpleDoc(psStream, psInFormat, docAttr); DocAttributeSet das = new HashDocAttributeSet(); Object printData = this.prepareData(); logger.info("#print. 开始打印, 数据资源-printData: "+printData); if (printData == null) { throw new NullPointerException("printData == null. 准备数据失败!!!"); } doc = new SimpleDoc(printData, printFormat, das); //创建文档打印作业 long start = System.currentTimeMillis(); logger.info("#print. 开始打印, 请稍候..."); DocPrintJob job = printer.createPrintJob(); job.print(doc, attributeSet); logger.info("#print. 完成打印, 共耗时: "+(System.currentTimeMillis() - start)+" 毫秒."); } catch (Exception e) { // TODO: handle exception logger.error("#print. print error.", e); throw new Exception("打印过程中出现异常情况,打印没有完成,请检查!!!", e); } }