我正在尝试在我的打印机 CANON MB5000 上使用 java print 打印一个文本文件。 它适用于 Linux 但不适用于 Windows。 在 Windows 上,这里支持文档风格:
for (DocFlavor f : printService.getSupportedDocFlavors()){
System.out.println("media type : "+f.getMediaType());
System.out.println("mime type : "+f.getMimeType());
}
我得到:
media type : image
mime type : image/gif
media type : image
mime type : image/gif
media type : image
mime type : image/gif
media type : image
mime type : image/jpeg
media type : image
mime type : image/jpeg
media type : image
mime type : image/jpeg
media type : image
mime type : image/png
media type : image
mime type : image/png
media type : image
mime type : image/png
media type : application
mime type : application/x-java-jvm-local-objectref
media type : application
mime type : application/x-java-jvm-local-objectref
media type : application
mime type : application/octet-stream
media type : application
mime type : application/octet-stream
media type : application
mime type : application/octet-stream
我尝试用八位字节流打印:
if (service != null){
FileInputStream fis = new FileInputStream("c:/mytxt.txt");
Doc pdfDoc = new SimpleDoc(fis, new DocFlavor.INPUT_STREAM ("application/octet-stream"), null);
DocPrintJob printJob = service.createPrintJob();
printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
fis.close();
}
没有打印。
如果我使用新的 DocFlavor.INPUT_STREAM ("image/jpeg") 并且打印 JPEG 图像,它就可以工作。
我应该怎么做才能打印文本文件(或 PDF 文件)?
最佳答案
不确定,但猜测解决方法是 application/x-java-jvm-local-objectref;class="java.awt.print.Pageable"或 应用程序/x-java-jvm-local-objectref;class="java.awt.print.Printable"。也许对于 pdf 可以重用 https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java?view=markup ) 或者如果不想添加外部依赖项,请创建实现 Printable 和 Pageable 接口(interface)的 UDT...希望这会有所帮助。
关于Java 打印 : which DocFlavor to choose for my printer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36939461/