打印“.pdf”文件 - PrinterJob

时间:2015-05-26 13:56:36

标签: java pdf printing

我在尝试打印“.pdf”时遇到问题。我用这个代码打印字符串没有问题,但pdf不会。

我的程序收到带有pdf的byte []并打印出来,我想记录一个从InputStream打印的临时文件,但也失败了。

按照我用来打印的代码:

    FileChannel fc = null;
    ByteBuffer bb = ByteBuffer.wrap(pdf);
    FileOutputStream fos = null;
    RandomAccessFile fis = null;
    try {
        File Tempfile = File.createTempFile("portfolios-temp", ".pdf");

        fos = new FileOutputStream(tempfile);
        fos.write(pdf);

        fos.close();

        FileInputStream psStream = new FileInputStream(tempfile);

        DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
        MyDoc doc = new SimpleDoc(psStream, psInFormat, null);

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add (new Copies (1));
        aset.add (OrientationRequested.PORTRAIT);
        aset.add (Sides.ONE_SIDED);
        aset.add (MediaSizeName.ISO_A4);

        DocPrintJob job printService.createPrintJob =();
        try {
            job.print (myDoc, aset);
        } Catch (Exception pe) {
            pe.printStackTrace ();
        }
    } [...]

我以这种方式获得打印服务:

        PrintService printService = null;
        for(PrintService printServiceCurrent : PrinterJob.lookupPrintServices()) {
            if(printServiceCurrent.getName().equals(PRINTER_NAME)) {
                printService = printServiceCurrent;
                break;
            }
        }

他向打印机发送命令,但是没有内容。我检查了临时文件,它正在生成。

有什么想法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

首先,而不是

PrintRequestAttributeSet aset HashPrintRequestAttributeSet = new();

应该是

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 

其次,您没有根据属性集中的说明​​检查可以打印格式的打印机。

查看此链接以获取更多详细信息:

Java Print Service API

相关问题