2015-05-26 87 views
0

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

我的程序用pdf接收一个byte []并打印出来,我想录制一个tempfile来从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; 
      } 
     } 

他向打印机发送一个命令,但是就这样产生没有内容。我检查了临时文件,它正在完美生成。

有什么想法?

在此先感谢。

回答

0

首先,而非

PrintRequestAttributeSet aset HashPrintRequestAttributeSet = new(); 

应该

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 

其次你不检查,可以根据设定的属性的说明打印格式的打印机。

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

Java Print Service API

+0

我补充说,我得到的打印服务的代码。我还修复了aset的代码。谢谢 – Gannis