2015-09-04 55 views
1

雖然有幾個關於托盤選擇的問題,但沒有一個與我的問題有關。Java印花托盤選擇佐賀

下面是我用打印的代碼:

private static void finalPrint(PDDocument pdoc, boolean pbStationary) 
    throws BigBangJewelException 
{ 
    PrintService lrefSvc; 
    PrinterJob lrefPJob; 
    Media lrefMedia; 
    HashPrintRequestAttributeSet lobjSet; 

    lrefSvc = getPrinter(); 

    lrefPJob = PrinterJob.getPrinterJob(); 

    try 
    { 
     lrefPJob.setPrintService(lrefSvc); 
     lrefPJob.setPageable(pdoc); 

     lrefMedia = null; 
     if (pbStationary) 
      lrefMedia = getTray(lrefSvc); 
     if (lrefMedia != null) 
     { 
      lobjSet = new HashPrintRequestAttributeSet(); 
      lobjSet.add(lrefMedia); 
      lrefPJob.print(lobjSet); 
     } 
     else 
      lrefPJob.print(); 
    } 
    catch (Throwable e) 
    { 
     throw new BigBangJewelException(e.getMessage(), e); 
    } 
} 

private static PrintService getPrinter() 
    throws BigBangJewelException 
{ 
    String lstrPrinter; 
    PrintService[] larrServices; 
    int i; 

    try 
    { 
     lstrPrinter = (String)Engine.getUserData().get("Printer"); 

     larrServices = PrinterJob.lookupPrintServices(); 

     for (i = 0; i < larrServices.length; i++) 
     { 
      if (larrServices[i].getName().indexOf(lstrPrinter) != -1) 
       return larrServices[i]; 
     } 
    } 
    catch (Throwable e) 
    { 
     throw new BigBangJewelException(e.getMessage(), e); 
    } 

    throw new BigBangJewelException("Impressora definida (" + lstrPrinter + ") não encontrada."); 
} 

private static Media getTray(PrintService prefSvc) 
{ 
    Media[] larrMedia; 
    String lstrAux; 
    int i; 

    larrMedia = (Media[])prefSvc.getSupportedAttributeValues(Media.class, null, null); 

    if (larrMedia == null) 
     return null; 

    for (i = 0; i < larrMedia.length; i++) 
    { 
     lstrAux = larrMedia[i].toString().toLowerCase(); 
     if (lstrAux.contains("tray") && lstrAux.contains("3")) 
     { 
      return larrMedia[i]; 
     } 
    } 

    return null; 
} 

莫明其妙的事是,曾經工作此代碼。機器上有一堆Xerox打印機,代碼可以正確識別想要的打印機和想要的托盤,並且一切都很好用。

然後,有一天,一夜之間,它停止工作。它仍然找到了正確的打印機,但現在,它總是打印到紙盤1

,改變是一個額外的HP打印機添加到機器的唯一的事。

我可以證實,該代碼是找到托盤,並將其發送到打印作業,但它變得忽略不計。

同樣,也有很多問題,在那裏對這個問題,但我的問題是,代碼運行良好了四年,然後停止工作,原因不明。

任何人都可以在這個問題上談一談?

編輯:新信息:卸載HP打印機使得施樂打印機再次正常工作。爲什麼安裝一個驅動程序會影響Java與另一個驅動程序通信的能力?

編輯2:更多信息:如果我們安裝HP全局打印機驅動程序而不是特定的打印機驅動程序,則一切正常。我將不回答這個問題,看看在賞金到期之前是否有人能夠提出一個好的解釋,然後我將把這個編輯作爲答案並接受它。

+1

來自例外的日誌等可以是你的「光」...這個代碼幾乎不可能測試SO –

+0

這是事情,沒有例外。該設置只是默默的忽略。 –

+0

我喜歡異常名稱'BigBangJewelException',但是做這樣的事情是沒有意義的:'拋出新的BigBangJewelException(e.getMessage(),e);'。簡單地省略第一個參數。 –

回答

1

我們特殊情況的解決方案是更改HP打印機的打印機驅動程序。

本來我們安裝了打印機有問題,導致了這一問題的特定的驅動程序。安裝惠普全球驅動程序反而使問題消失。

不幸的是,我們不知道爲什麼。 Jens Schauder的回答包含了如何去尋找的線索。

3

如果我給你問正確的,你的lobjSet內容是不變的,但它的打印結果不同的是,在安裝了新的驅動程序。

我檢查了代碼PnterJob.print(PrintRequestAttributeSet)而感到驚訝,它完全忽略設置屬性。

所以我看了看PrintService來自哪裏,代碼有點冗長,但我猜想它與安裝的打印機驅動程序以某種方式交互以創建適當的實例。所以新的驅動程序改變這個,返回一個不同的PrintService。我無法判斷這件事發生了什麼變化,但如果你可以重新創建這兩種情況(看起來你可以),使用調試器來找到代碼行爲的確切位置應該相當容易變化。

+0

這聽起來像個好主意,但不幸的是,這隻發生在生產機器上,我無法對其進行調試。我確實看了一下你提到的代碼,它的確很長。 +1花時間看看它。 –

+0

嗯,我不希望這個獎金浪費,而且也不像任何人會想出更好的東西,所以,就這樣吧。 :) –