Проблема печати ICEPDF - PullRequest
       13

Проблема печати ICEPDF

1 голос
/ 27 февраля 2011

Я использую ICEPDF для показа и печати документа PDF в своем приложении Java.

Я получаю следующее исключение:

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);
    }
}

Выше показано, что зритель в порядке, и он позволяет все операции, кроме печати!(см. исключение выше).

Любая помощь высоко ценится.

Спасибо

1 Ответ

1 голос
/ 09 марта 2012

К сожалению, это исключение появляется, когда на вашей ОС нет принтера. Вот исходный код 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". Я не знаю, как, если это легко сделать ...

...