Java打印:为我的打印机选择哪个DocFlavor?

时间:2016-04-29 13:20:06

标签: java printing

我正在尝试在我的打印机CANON MB5000上打印带有java打印的文本文件。 它适用于Linux但不适用于Windows。 在Windows上这里支持doc风格:

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

我尝试使用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文件)?

1 个答案:

答案 0 :(得分:0)

不确定,但猜测方法是application / x-java-jvm-local-objectref; class =" java.awt.print.Pageable"要么 应用程序/ x-java的JVM-本地objectref;类=" java.awt.print.Printable&#34 ;.也许对于pdf可以像重用https://material.angular.io/components/chips/api)或者如果不想添加外部依赖,创建实现Printable和Pageable接口的UDT ......希望这有帮助。