【问题标题】:Printing from swing on Mac & Windows - Where is postscript support?在 Mac 和 Windows 上从 swing 打印 - postscript 支持在哪里?
【发布时间】:2009-05-21 22:19:01
【问题描述】:

我正在通过机场将复杂的 Swing 应用程序 UI 打印到物理打印机。我有 Mac 和 Windows 机器都打印到同一台打印机。从 Mac 打印看起来很棒。从 Windows 打印看起来远非完美 - 一切都非常像素化,包括字体和图形线条。

一些挖掘发现可用的 PrintServices 对于不同的平台是不同的。

DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
PrintServiceLookup.lookupPrintServices(flavor, attrs);

当从 mac 执行时,上面返回一个单元素数组。从 Windows 中,它返回一个空数组。这让我相信 Windows 正在向打印机发送 72 DPI 图像,而不是 postscript 数据。

这是 mac 和 windows JVM 实现的区别吗?是否有任何解决方法可以在 Windows 上进行打印?我意识到我可以生成自己的 350dpi 光栅化图像并将其发送到打印机,但是这些内容会占用数百页,如果可能的话,我真的很想避免这条路线。

【问题讨论】:

  • 我偶然发现了这个问题,我知道它很古老,您可能已经意识到这一点,但是第二行代码是不必要的,lookupPrintServices 允许第二个参数是 null如果您不需要设置属性。

标签: java windows swing printing postscript


【解决方案1】:

我想我得到了答案:java.awt.printerjob 系统属性设置为 sun.awt.windows.WPrinterJob。如果您喜欢打印机上的块状像素化输出,显然这是一个方便的 PrinterJob 子类。相反,如果它可用,我会得到一个 sun.print.PSPrinterJob 的实例,如下所示:

PrinterJob printerJob = null;
try {
    if (System.getProperty("java.awt.printerjob").equals("sun.awt.windows.WPrinterJob")) {
        // WPrinterJob sends crappy GIF images to the printer, and everything looks all blocky
        // try to get an instance of a PSPrinterJob instead
        printerJob = (PrinterJob) Class.forName("sun.print.PSPrinterJob").newInstance();
    }
} catch (Throwable e1) {
    log.log(Level.SEVERE, "Could not instaniate sun.print.PSPrinterJob", e1);
}
if (printerJob == null) {
      printerJob = PrinterJob.getPrinterJob();
}

【讨论】:

  • 仅供参考,Mac 上的 Java 8 似乎将此值设置为 sun.lwawt.macosx.CPrinterJob
猜你喜欢
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多