如何在不显示打印对话框的情况下使用特定的打印机(Microsoft打印为PDF)和特定文件的名称来打印JTable?

时间:2019-03-05 09:06:53

标签: java printing jtable

我认为标题很清楚。 我在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 个答案:

答案 0 :(得分:0)

PrinterName无法添加到您的HashPrintRequestAttributeSet中。您应该将其添加到HashPrintServiceAttributeSet

HashPrintRequestAttributeSet只能包含类型PrintRequestAttribute的对象或其子类。 这就是Java试图将PrintName(类型为PrintServiceAttribute)对象强制转换为PrintRequestAttribute对象的原因,因为它不能继承自 public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is Entity) { Entity targetEntity = (Entity)pluginExecutionContext.InputParameters["Target"]; if (targetEntity.LogicalName != Incident.EntityLogicalName) { return; } IOrganizationServiceFactory orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService orgService = orgServiceFactory.CreateOrganizationService(pluginExecutionContext.UserId); try { string desc = (string)targetEntity.Attributes["description"]; string pattern = @"\b(?:https?://|www\.)[^ \f\n\r\t\v\]]+\b"; MatchCollection collection = Regex.Matches(desc, pattern); if (collection.Count > 0) { throw new Exception(collection[0].Value); } } catch (Exception ex) { tracingService.Trace("Error in CaseUrlPlugin {0}", ex.ToString()); throw ex; } } } 。 -> ClassCastException