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); } }
/** * Constructs a <code>SimpleDoc</code> with the specified * print data, doc flavor and doc attribute set. * @param printData the print data object * @param flavor the <code>DocFlavor</code> object * @param attributes a <code>DocAttributeSet</code>, which can * be <code>null</code> * @throws IllegalArgumentException if <code>flavor</code> or * <code>printData</code> is <code>null</code>, or the * <code>printData</code> does not correspond * to the specified doc flavor--for example, the data is * not of the type specified as the representation in the * <code>DocFlavor</code>. */ public SimpleDoc(Object printData, DocFlavor flavor, DocAttributeSet attributes) { if (flavor == null || printData == null) { throw new IllegalArgumentException("null argument(s)"); } Class repClass = null; try { repClass = Class.forName(flavor.getRepresentationClassName()); } catch (Throwable e) { throw new IllegalArgumentException("unknown representation class"); } if (!repClass.isInstance(printData)) { throw new IllegalArgumentException("data is not of declared type"); } this.flavor = flavor; if (attributes != null) { this.attributes = AttributeSetUtilities.unmodifiableView(attributes); } this.printData = printData; }
public SimpleDoc(Object printData, DocFlavor docflavor, DocAttributeSet docattributes) { /* * IllegalArgumentException - if flavor or printData is null, or the * printData does not correspond to the specified doc flavor--for * example, the data is not of the type specified as the representation * in the DocFlavor. */ if (docflavor == null || printData == null) { throw new IllegalArgumentException("Argument is null"); } try { Class<?> clazz = Class.forName(docflavor.getRepresentationClassName()); if (!clazz.isInstance(printData)) { throw new IllegalArgumentException(""); } } catch (Exception e) { throw new IllegalArgumentException("Wrong type of print data"); } this.printdata = printData; this.flavor = docflavor; this.attributes = docattributes; this.reader = null; this.instream = null; }
/** * Constructs a <code>SimpleDoc</code> with the specified * print data, doc flavor and doc attribute set. * @param printData the print data object * @param flavor the <code>DocFlavor</code> object * @param attributes a <code>DocAttributeSet</code>, which can * be <code>null</code> * @throws IllegalArgumentException if <code>flavor</code> or * <code>printData</code> is <code>null</code>, or the * <code>printData</code> does not correspond * to the specified doc flavor--for example, the data is * not of the type specified as the representation in the * <code>DocFlavor</code>. */ public SimpleDoc(Object printData, DocFlavor flavor, DocAttributeSet attributes) { if (flavor == null || printData == null) { throw new IllegalArgumentException("null argument(s)"); } Class repClass = null; try { String className = flavor.getRepresentationClassName(); sun.reflect.misc.ReflectUtil.checkPackageAccess(className); repClass = Class.forName(className, false, Thread.currentThread().getContextClassLoader()); } catch (Throwable e) { throw new IllegalArgumentException("unknown representation class"); } if (!repClass.isInstance(printData)) { throw new IllegalArgumentException("data is not of declared type"); } this.flavor = flavor; if (attributes != null) { this.attributes = AttributeSetUtilities.unmodifiableView(attributes); } this.printData = printData; }
/** * Constructs a {@code SimpleDoc} with the specified print data, doc flavor * and doc attribute set. * * @param printData the print data object * @param flavor the {@code DocFlavor} object * @param attributes a {@code DocAttributeSet}, which can be {@code null} * @throws IllegalArgumentException if {@code flavor} or {@code printData} * is {@code null}, or the {@code printData} does not correspond to * the specified doc flavor--for example, the data is not of the * type specified as the representation in the {@code DocFlavor} */ public SimpleDoc(Object printData, DocFlavor flavor, DocAttributeSet attributes) { if (flavor == null || printData == null) { throw new IllegalArgumentException("null argument(s)"); } Class<?> repClass = null; try { String className = flavor.getRepresentationClassName(); sun.reflect.misc.ReflectUtil.checkPackageAccess(className); repClass = Class.forName(className, false, Thread.currentThread().getContextClassLoader()); } catch (Throwable e) { throw new IllegalArgumentException("unknown representation class"); } if (!repClass.isInstance(printData)) { throw new IllegalArgumentException("data is not of declared type"); } this.flavor = flavor; if (attributes != null) { this.attributes = AttributeSetUtilities.unmodifiableView(attributes); } this.printData = printData; }
/** * Constructs a {@code SimpleDoc} with the specified * print data, doc flavor and doc attribute set. * @param printData the print data object * @param flavor the {@code DocFlavor} object * @param attributes a {@code DocAttributeSet}, which can * be {@code null} * @throws IllegalArgumentException if {@code flavor} or * {@code printData} is {@code null}, or the * {@code printData} does not correspond * to the specified doc flavor--for example, the data is * not of the type specified as the representation in the * {@code DocFlavor}. */ public SimpleDoc(Object printData, DocFlavor flavor, DocAttributeSet attributes) { if (flavor == null || printData == null) { throw new IllegalArgumentException("null argument(s)"); } Class<?> repClass = null; try { String className = flavor.getRepresentationClassName(); sun.reflect.misc.ReflectUtil.checkPackageAccess(className); repClass = Class.forName(className, false, Thread.currentThread().getContextClassLoader()); } catch (Throwable e) { throw new IllegalArgumentException("unknown representation class"); } if (!repClass.isInstance(printData)) { throw new IllegalArgumentException("data is not of declared type"); } this.flavor = flavor; if (attributes != null) { this.attributes = AttributeSetUtilities.unmodifiableView(attributes); } this.printData = printData; }
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(); } } }
/** * Constructs a SimpleDoc with the specified print data, doc flavor and doc attribute set. * @param printData the object with the data to print. * @param flavor the document flavor of the print data. * @param attributes the attributes of the doc (may be <code>null</code>). * * @throws IllegalArgumentException if either <code>printData</code> or * <code>flavor</code> are <code>null</code>, or the print data is not * supplied in the document format specified by the given flavor object. */ public SimpleDoc(Object printData, DocFlavor flavor, DocAttributeSet attributes) { if (printData == null || flavor == null) throw new IllegalArgumentException("printData/flavor may not be null"); if (! (printData.getClass().getName().equals( flavor.getRepresentationClassName()) || flavor.getRepresentationClassName().equals("java.io.Reader") && printData instanceof Reader || flavor.getRepresentationClassName().equals("java.io.InputStream") && printData instanceof InputStream)) { throw new IllegalArgumentException("data is not of declared flavor type"); } this.printData = printData; this.flavor = flavor; if (attributes != null) this.attributes = AttributeSetUtilities.unmodifiableView(attributes); else this.attributes = null; stream = null; reader = null; }
public SimpleDoc(Object printData, DocFlavor docflavor, DocAttributeSet docattributes) { /* * IllegalArgumentException - if flavor or printData is null, or the * printData does not correspond to the specified doc flavor--for * example, the data is not of the type specified as the representation * in the DocFlavor. */ if (docflavor == null || printData == null) { //print.00= Argument is null throw new IllegalArgumentException(Messages.getString("print.00")); //$NON-NLS-1$ } try { Class<?> clazz = Class.forName(docflavor.getRepresentationClassName()); if (!clazz.isInstance(printData)) { throw new IllegalArgumentException(""); } } catch (Exception e) { //print.01= Wrong type of print data throw new IllegalArgumentException(Messages.getString("print.01")); //$NON-NLS-1$ } this.printdata = printData; this.flavor = docflavor; this.attributes = docattributes; this.reader = null; this.instream = null; }
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 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); } }
public DocAttributeSet getAttributes() { return null; }