第一种打印方式是通过网页正常工作,但不是第二种方式.成功检索默认打印机以进行自动打印,但是不会打印,我也没有得到任何错误.以下是我的分析:
>最初,我以为DocFlavor不被支持.然后我列出
下载该打印机支持的DocFlavor,其中一个是
应用程序/八位字节流,即DocFlavor.INPUT_STREAM.AUTOSENSE.
所以打印机支持风味.
>然后,我添加了PrintJobListener来检查打印作业是否是
失败.当我将该监听器添加到printJob时,它会打印
No_More_Events和DATA_TRANSFER_COMPLETE,应该打印
JOB_COMPLETE,如果工作成功.
>最后一步是调试Java代码.当我执行行
job.print(),它进入Win32PrintJob.print()方法.我做了F6
执行每行,看看它在做什么.我将其与代码进行了比较
在GrepCode,因为源代码没有加载在eclipse.它
没事了,我看不到任何错误.它唯一的地方
没有进入这个块,它检查mDestination
价值,因为我没有提供,它没有通过.
请看下面的代码:
if (mDestination != null) { // if destination attribute is set try { FileOutputStream fos = new FileOutputStream(mDestination); byte [] buffer = new byte[1024]; int cread; while ((cread = instream.read(buffer,buffer.length)) >= 0) { fos.write(buffer,cread); } fos.flush(); fos.close(); } catch (FileNotFoundException fnfe) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException(fnfe.toString()); } catch (IOException ioe) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException(ioe.toString()); } notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); notifyEvent(PrintJobEvent.JOB_COMPLETE); service.wakeNotifier(); return; }
这是JOB_COMPLETE唯一的地方.我认为这个块是写入一个文件,这对我来说是不需要的.
我认为实际的打印在同一个方法Win32PrintJob.print()中发生在以下行中.
private native boolean More ...printRawData(byte[] data,int count);
但是这是一个本地的方法,所以我不知道这里发生了什么.
请让我知道为什么我无法打印PDF.
编辑:
附上打印文件的代码:
PrintService pss = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob job = pss.createPrintJob(); DocAttributeSet das = new HashDocAttributeSet(); Doc document; try { document = new SimpleDoc(new FileInputStream(new File(fileName)),DocFlavor.INPUT_STREAM.AUTOSENSE,das); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); job.addPrintJobListener(new PrintJobWatcher()); job.print(document,pras); }
注意:我尝试过不同的口味,如PDF,PCL.没有任何工作,我得到不支持的运行时错误.
解决方法
final PdfBook pdfBook = new PdfBook(pdfDecoder,printJob.getPrintService(),attributes); pdfBook.setChooseSourceByPdfPageSize(false); final SimpleDoc doc = new SimpleDoc(pdfBook,DocFlavor.SERVICE_FORMATTED.PAGEABLE,null); // used to track print activity printJob.addPrintJobListener(new PDFPrintJobListener()); try { printJob.print(doc,attributes); } catch (final Exception e) { LogWriter.writeLog("Exception " + e + " printing"); // <end-demo> }
除此之外,您还可以提供打印机名称,并添加列表PDFPrintJobListener.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。