Java 贾斯珀报道。如何使用指定的打印机驱动程序打印到文件?

Java 贾斯珀报道。如何使用指定的打印机驱动程序打印到文件?,java,printing,jasper-reports,Java,Printing,Jasper Reports,在控制面板中,我的打印机端口配置为“文件:”。 例如,我可以使用PrintServiceAttributeSet选择打印机,但无法在打印时设置输出文件名。我不使用JRXlsExporter或JRPdfExporter或类似的东西,只使用JRPrintServiceExporter PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); printServiceAttri

在控制面板中,我的打印机端口配置为“文件:”。 例如,我可以使用
PrintServiceAttributeSet
选择打印机,但无法在打印时设置输出文件名。我不使用
JRXlsExporter
JRPdfExporter
或类似的东西,只使用
JRPrintServiceExporter

  PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
  printServiceAttributeSet.add(new PrinterName("Xerox DocuPrint 100 EPS PS3", null));
  //...
  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
  //...
  exporter.exportReport();
驱动程序在PostScript中输出文件,但始终会出现“输出文件名”窗口。如果在窗口中手动设置文件名,则打印机将成功打印到文件


知道如何以编程方式设置文件名吗?

javax.print.attribute.standard
»有帮助

解决方案:

  //...
  import javax.print.attribute.standard.Destination;
  //...

  JRExporter exporter = new JRPrintServiceExporter();          

  //--- Set print properties
  PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
  printRequestAttributeSet.add(MediaSizeName.ISO_A4);   

  //----------------------------------------------------     
  printRequestAttributeSet.add(new Destination(new java.net.URI("file:d:/output/report.ps")));
  //----------------------------------------------------     

  PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
  printServiceAttributeSet.add(new PrinterName("Xerox DocuPrint 100 EPS PS3", null)); 

  //--- Set print parameters      
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
  exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);      
  exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);      
  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);      

  //--- Print the document
  try{
      exporter.exportReport();
  }
  catch(JRException e){
      e.printStackTrace();
  }