打印PDF双面打印

时间:2017-07-07 13:42:54

标签: java pdf printing pdfbox

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

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);
         }
    }





}

1 个答案:

答案 0 :(得分:0)

经过彻底检查后,我发现系统中检测到的打印机是没有双面打印的型号。重新安装驱动程序后,一切正常。