2017-07-07 229 views
0

我使用PDFBox打印PDF。面对一些打印机在PrinterJob Sides.DUPLEX中发送到双面打印机时不打印双面打印的问题。但是,如果这些打印机向他们发送准备好的PCL或PS文件,它们可以打印双面打印件。我在这里找到了这样一个链接Duplex Printing of PDF document with T&C at the Back作者创建了一个第三方ps文件并在其中添加了一个双工。我想知道PDFBox是否可以直接将双工添加到输出流。或者,也许有一些替代PDFBox,能够在打印时添加双面打印。打印PDF双面打印

import java.awt.print.PrinterJob; 
import java.io.File; 
import javax.print.attribute.HashPrintRequestAttributeSet; 
import javax.print.attribute.PrintRequestAttributeSet; 
import javax.print.attribute.standard.Copies; 
import javax.print.attribute.standard.Sides; 


import org.apache.pdfbox.pdmodel.PDDocument; 

import org.apache.pdfbox.printing.PDFPageable; 


public class PrintPDFMain 
{ 

    public static void main(String args[]) throws Exception { 


     try 
     { 
     File file ; 
     int qty; 
     if (args.length < 2) 
     { 
     System.out.println("1st param File 2nd param printer name 3rd param qty of copies 4th param duplex (yes/no) \n1st and 2nd params are mandatory"); 
     return; 
     } 
     file = new File(args[0]); 
     if (!file.exists()) return; 
     PDDocument document = PDDocument.load(file); 

     PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 

     if (args.length > 2) 
     { 

      if (args.length >= 3) 
      { 
       qty = 1; 
       try 
       { 
       qty = Integer.parseInt (args[2]); 
       } 
       catch (NumberFormatException e) 
       { 
        qty = 1; 
       } 
       System.out.println(qty); 
      pras.add(new Copies(qty)); 
      } 
      if (args.length >=4) 
      { 
       if (args[3].length() == 3) {System.out.println(args[3]); pras.add(Sides.DUPLEX);}} 
     } 


     PrinterJob job = PrinterJob.getPrinterJob(); 
       job.setPageable(new PDFPageable(document)); 
     job.print(pras); 

     System.out.println("print"); 
     System.out.println(); 


    } 
     catch (Exception e) 
     { 
      System.out.println("error " + e); 
     } 
    } 





} 
+0

PDF规范提到它......但上面的打印不会将PDF文件发送到打印机,它通过读取PDF将渲染命令发送到打印机。所以我的理解是,PDF应用程序应该查询设置(从“查看器首选项字典中的条目”),然后设置像您提到的“Sides.DUPLEX”选项。该链接显示直接从PS流到打印机,PDFBox不知道也不关心使用什么打印机语言。 –

回答

0

一个全面的检查后,我发现在系统中没有双工模式检测到打印机。重新安装驱动程序后,一切正常。