2011-02-27 140 views
1

我正在使用ICEPDF在我的Java應用程序中顯示和打印PDF文檔。ICEPDF打印問題

我得到以下異常:

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

以上可以看出,觀察者罰款,它除了允許所有的操作從印刷! (見上述例外)。

任何幫助,高度讚賞。

謝謝

+0

您是否嘗試過icepdf幫助論壇?他們通常很擅長回答問題。 – 2011-02-27 17:27:00

+1

我對它排序。問題是我的打印機沒有被設置爲默認打印機! 沒有「沒有默認打印機」的處理,如果它不存在(即使你有打印機),icepdf拋出異常。 感謝您的幫助無論如何:) – user1265230 2011-02-27 18:47:00

回答

1

不幸的是,當您的操作系統上沒有可用的打印機時會出現此異常。 這裏是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; 
} 

如果沒有相符的服務, 「服務」 陣列是零長度: http://docs.oracle.com/javase/6/docs/api/javax/print/PrintServiceLookup.html#lookupPrintServices(javax.print.DocFlavor,使用javax。 print.attribute.AttributeSet)

也許解決方案是創建一個「/ dev/null」打印機。我不知道如果這很容易做...