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

在java中,如何在没有用户交互的情况下将pdf打印到特定托盘,java,pdf,printing,Java,Pdf,Printing,我正在尝试设置一个在夜间运行的服务,将一组发票和其他文档自动打印到一组打印机上。到目前为止,我可以很好地打印文档,但我需要能够指定一个托盘(一个带有我们公司的信头,一个带有库存白纸)。到目前为止,我尝试的所有方法都没有奏效,我在PrintRequestAttribute集中指定了MediaTray属性,但这似乎没有任何作用。有人有过这样的经历吗 我当前用于测试的代码如下所示 // Create a PDFFile from a File reference File f = new File("

我正在尝试设置一个在夜间运行的服务,将一组发票和其他文档自动打印到一组打印机上。到目前为止,我可以很好地打印文档,但我需要能够指定一个托盘(一个带有我们公司的信头,一个带有库存白纸)。到目前为止,我尝试的所有方法都没有奏效,我在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);

您实际使用什么来打印PDF?仅当打印机直接支持PDF时,才可将PDF直接发送到打印机。否则,您需要使用Java库进行光栅化。有一篇博客文章建议如何从JAva打印PDF,我使用jasper report。这是代码

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();
        }
    }
我正在使用将PDF文件转换为PS以发送到打印机。可能的副本