为了账号安全,请及时绑定邮箱和手机立即绑定

如何在不显示打印对话框的情况下使用特定打印机和特定文件名打印 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 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号