Как напечатать JTable на определенном принтере (Microsoft Print to PDF) и определенном имени файла без отображения диалогового окна печати? - PullRequest
0 голосов
/ 05 марта 2019

Название понятно, я думаю.У меня есть java.lang.ClassCastException в строке printerName, и я не знаю, как напрямую указать путь для вывода 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 Ответ

0 голосов
/ 05 марта 2019

PrinterName нельзя добавить к вашему HashPrintRequestAttributeSet.Вы должны добавить его к HashPrintServiceAttributeSet.

. HashPrintRequestAttributeSet может содержать только объекты типа PrintRequestAttribute или его подклассы.Вот почему Java пытается привести объект PrintName (который имеет тип PrintServiceAttribute ) к PrintRequestAttribute, который не может быть выполнен, поскольку он не наследуется от этого.-> ClassCastException

...