java - ICEPDF打印问题

标签 java pdf printing icepdf

我正在使用 ICEPDF 在我的 Java 应用程序中显示和打印 PDF 文档。

我收到以下异常:

org.icepdf.core.pobjects.Catalog <clinit>
INFO: ICEsoft ICEpdf Core 4.1.4 
Exception in thread "Thread-4" java.lang.ArrayIndexOutOfBoundsException: 0
    at org.icepdf.ri.common.PrintHelper.getSetupDialog(PrintHelper.java:526)
    at org.icepdf.ri.common.PrintHelper.setupPrintService(PrintHelper.java:199)
    at org.icepdf.ri.common.SwingController.initialisePrinting(SwingController.java:2590)
    at org.icepdf.ri.common.SwingController.access$400(SwingController.java:102)
    at org.icepdf.ri.common.SwingController$3.run(SwingController.java:2548)
    at java.lang.Thread.run(Thread.java:680)

我使用的代码是:

public class ViewerComponentExample {
    public static void main(String[] args) {
        // Get a file from the command line to open
        String filePath = "boll.pdf";

        // build a component controller
        SwingController controller = new SwingController();

        SwingViewBuilder factory = new SwingViewBuilder(controller);

        JPanel viewerComponentPanel = factory.buildViewerPanel();

        // add interactive mouse link annotation support via callback
        controller.getDocumentViewController().setAnnotationCallback(
                new org.icepdf.ri.common.MyAnnotationCallback(
                        controller.getDocumentViewController()));

        JFrame applicationFrame = new JFrame();
        applicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        applicationFrame.getContentPane().add(viewerComponentPanel);

        // Now that the GUI is all in place, we can try openning a PDF
        controller.openDocument(filePath);

        // show the component
        applicationFrame.pack();
        applicationFrame.setVisible(true);
    }
}

上面显示的查看器很好,它允许除打印之外的所有操作! (参见上面的异常(exception))。

非常感谢任何帮助。

谢谢

最佳答案

不幸的是,当您的操作系统上没有可用的打印机时,会出现此异常。 这是icepdf源代码:

return ServiceUI.printDialog(graphicsConfiguration,
    point.x,
    point.y,
    services, services[0],
    DocFlavor.SERVICE_FORMATTED.PRINTABLE,
    printRequestAttributeSet);

“服务”的定义如下:

private PrintService[] lookForPrintServices() {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(
        DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    if (defaultService != null && services.length &gt; 1) {
        PrintService printService;
        for (int i = 1, max = services.length; i &lt; max; i++) {
            printService = services[i];
            if (printService.equals(defaultService)) {
                PrintService tmp = services[0];
                services[0] = defaultService;
                services[i] = tmp;
                break;
            }
        }
    }
    return services;
}

如果没有服务匹配,“services”数组的长度为零: http://docs.oracle.com/javase/6/docs/api/javax/print/PrintServiceLookup.html#lookupPrintServices(javax.print.DocFlavor , javax.print.attribute.AttributeSet)

也许解决方案是创建一个“/dev/null”打印机。我不知道这是否容易做到......

关于java - ICEPDF打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131394/

相关文章:

java - 如何在 onCreate 中使用 glide 获取位图?

java - 然后,Object.toString(),编码为UTF-8

asp.net - 如何在aspx页面内显示pdf?

java - 将页面添加到现有PDF com.itextpdf.text.exceptions.InvalidPdfException : PDF header signature not found

javascript - 使用 javascript 打印自定义 url 不起作用

perl - 如何一次打印成两个文件?

html - 网页内的 Firefox 打印选项

java - Spring-Data-JPA - 将一条记录插入数据库后无限递归

java - Spring - 是否可以将多个字段映射到单个表单错误?

Java iText 使用外部签名签署 PDF - 容器大小估计