亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不顯示打印對話框的情況下使用特定打印機和特定文件名打印 JTable?

如何在不顯示打印對話框的情況下使用特定打印機和特定文件名打印 JTable?

倚天杖 2022-07-20 19:37:32
我認為標題很清楚。我在printerName 的行有一個java.lang.ClassCastException,我不知道如何直接指定輸出pdf 的路徑。到目前為止,這是我的代碼:DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");    String date = sdf.format(new Date());    File file = null;    MessageFormat header = null;    MessageFormat footer = new MessageFormat("Page {0,number,integer}");    if(choix.equals("Lc")){        header = new MessageFormat("Liste des produits vendus le : " + date);        file = new File("Z:/Enregistrements/FeuillesActiviteJour/ListeProduitsVendus_"+sdf.format(new Date())+".pdf");    }    else{        header = new MessageFormat("Liste des Encaissements effectués le : " + date);        file = new File("Z:/Enregistrements/FeuillesActiviteJour/ListeEncaissements_"+sdf.format(new Date())+".pdf");    }    Destination destination = new Destination(file.toURI());    PrintService service = null;    PrintService[] services = PrinterJob.lookupPrintServices();for (PrintService service1 : services) {    if (service1.getName().equalsIgnoreCase("Microsoft Print to PDF")) {        service = service1;    }}    PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();    set.add(OrientationRequested.LANDSCAPE);    set.add(destination);try {    table.print(JTable.PrintMode.FIT_WIDTH, header, footer, false, set, false, service);} catch (PrinterException ex) {    Logger.getLogger(TraitementFeuilleActivite.class.getName()).log(Level.SEVERE, null, ex);} catch (HeadlessException ex) {    Logger.getLogger(TraitementFeuilleActivite.class.getName()).log(Level.SEVERE, null, ex);}}
查看完整描述

1 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

PrinterName無法添加到您的HashPrintRequestAttributeSet. 您應該將其添加到HashPrintServiceAttributeSet.

HashPrintRequestAttributeSet只能包含PrintRequestAttribute類型的對象或其子類。這就是為什么 Java 嘗試將PrintName(類型為PrintServiceAttribute的)對象強制轉換為PrintRequestAttribute無法完成的對象,因為它不是從該對象繼承的。-> ClassCastException


查看完整回答
反對 回復 2022-07-20
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號