【问题标题】:Java printing : which DocFlavor to choose for my printer?Java 打印:为我的打印机选择哪种 DocFlavor?
【发布时间】:2016-04-29 13:20:05
【问题描述】:

我正在尝试在我的打印机 CANON MB5000 上使用 java print 打印文本文件。 它适用于 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

我尝试使用八位字节流进行打印:

    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 文件)?

【问题讨论】:

  • 你有没有碰巧看到documentation for the DocFlavor.INPUT_STREAM class?有很多预定义的 DocFlavor 以静态常量的形式出现,包括您提到的每种类型的常量以及 AUTOSENSE。
  • 是的,但我已经尝试过使用 AUTOSENSE,它应该与八位字节流一起使用,但使用 AUTOSENSE 没有任何打印效果
  • 其他人呢? DocFlavor.INPUT_STREAM.JPEG、DocFlavor.INPUT_STREAM.PDF、DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST?
  • 我也有同样的问题。你找到解决办法了吗?

标签: java printing


【解决方案1】:

不确定,但猜测一种解决方法是 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 接口的 UDT...希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2018-01-24
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多