gpt4 book ai didi

java - 如何在没有用户交互的情况下将 pdf 打印到特定托盘

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:08 24 4
gpt4 key购买 nike

我正在尝试设置一项在夜间运行的服务,以自动将一堆发票和其他文档打印到一堆打印机上。截至目前,我可以很好地打印文档,但我需要能够指定一个托盘(一个带有我们公司的信笺,一个带有库存白皮书)到目前为止我尝试过的一切都没有用,我指定PrintRequestAttribute 集中的 MediaTray 属性,但似乎没有任何作用。任何人都有过这样的经历吗?

我当前用于测试的代码如下所示。

// Create a PDFFile from a File reference
File f = new File("C:\\File.pdf");
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
PDFPrintPage pages = new PDFPrintPage(pdfFile);

// Create Print Job
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pjob.setJobName(f.getName());
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
// Send print job to default printer


PrintRequestAttributeSet aset=new HashPrintRequestAttributeSet();
aset.add(MediaTray.MIDDLE); //Used several of the tray options here
pjob.print(aset);

最佳答案

我用的是jasper报告。这是代码。

public void runReport()
{
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
jasperReport = JasperCompileManager.compileReport("C:/temp/jtest.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), new JREmptyDataSource());

PrinterJob job = PrinterJob.getPrinterJob();
/* Create an array of PrintServices */
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
job.defaultPage(pf);
int selectedService = 0;


String theUserPrinterName = "\\\\office1\\printer1";
AttributeSet attrSet = new HashPrintServiceAttributeSet(new PrinterName(theUserPrinterName, null));
services = PrintServiceLookup.lookupPrintServices(null, attrSet);
try {
job.setPrintService(services[selectedService]);
} catch (Exception e)
{
e.printStackTrace();
}
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
printRequestAttributeSet.add(new Copies(1));

exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
/* We set the selected service and pass it as a paramenter */
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
exporter.exportReport();
}
catch (JRException e)
{
System.out.println("Caught exception!!!");
e.printStackTrace();
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
try {
exporter.exportReport();
}
catch (JRException e2)
{
e2.printStackTrace();
}
}

关于java - 如何在没有用户交互的情况下将 pdf 打印到特定托盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5120795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com