PDFBox: How to print pdf with specified printer?

后端 未结 5 953
余生分开走
余生分开走 2020-12-14 10:32

I want to use PDFBox for printing PDF files created by iText. I have tried this successfully with PDDocument class and its method print(). You can find docu

5条回答
  •  清歌不尽
    2020-12-14 10:49

     import java.awt.print.PrinterException;
    
     import java.io.IOException;
    
     import org.apache.pdfbox.pdmodel.PDDocument;
    
     public class Print {
    
    public static void main(String[] args) throws IOException, PrinterException
    {
        PDDocument pdf=PDDocument.load("d:\\filename.pdf");
                pdf.print();
    }
    
    }
    

    use the above code to print pdf using apache Pdfbox

    EDIT: version 2.0.0

    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.io.File;
    import java.io.IOException;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.printing.PDFPageable;
    
    public class JPrint {
    
      public static void main(String[] args) throws IOException, PrinterException {
        String filename;
        filename = "C:\\pdf.pdf";
    
        try {
          PDDocument pdf = PDDocument.load(new File(filename));
          PrinterJob job = PrinterJob.getPrinterJob();
          job.setPageable(new PDFPageable(pdf));
          job.print();
        } catch (Exception e) {
          System.out.println(e);
        }
      }
    }
    

提交回复
热议问题