0

Hello I'm tring to print directly a PDF file using PDFBox 2, but if I print the file using pdfbox this is printed rotated by 90° I have the same issue using an A4 standard printer and also a label printer. If I try to print using Adobe Reader the same pdf file is printed correctly.

I'm using this code:

static void printPDF(String completeFileName, String printerName) {

    try {
        PDDocument doc = PDDocument.load(new FileInputStream(completeFileName));  //read pdf file.

        javax.print.PrintService[] service = PrinterJob.lookupPrintServices();
        DocPrintJob docPrintJob = null;

        int count = service.length;
        for (int i = 0; i < count; i++) {
            if (service[i].getName().trim().equalsIgnoreCase(printerName)) {
                docPrintJob = service[i].createPrintJob();
                i = count;
            }
        }//for

        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setPrintService(docPrintJob.getPrintService());

        pjob.setJobName("job");

        PDFPageable pAble =  new PDFPageable(doc);
        pjob.setPageable(pAble);
        //pjob.printDialog();
        pjob.print();

    } catch (Exception  er) {
        er.printStackTrace();
    }

}

I don't know where I made the error... I don't find how disable autorotate (in pdfbox v2).

Best regards Fabio

  • What PDFBox version are you using (hopefully 2.0.5), and what size is your PDF (is it a label)? Can you share it? Btw PDFPageable has several constructors. Try the others. Nevertheless, label printing is a known problem (https://issues.apache.org/jira/browse/PDFBOX-3117)... maybe we can help you. – Tilman Hausherr May 02 '17 at 18:05
  • Hello I'm using PDFBox 2.0.5 version, the size of the label is 80x30 mm Using the contructor PDFPageable pAble = new PDFPageable(doc, Orientation.PORTRAIT); The label is printed horizzontally but is truncated... problably I have to set the page size ? – Fabio Canella May 03 '17 at 07:36
  • The page size (call `PDPage.setMediaBox()`) must be set, 1 unit = 1/72 inches, 1 inch = 2.54 cm. – Tilman Hausherr May 03 '17 at 08:17
  • How can I set the margin to 0 ? – Fabio Canella May 16 '17 at 14:58
  • https://stackoverflow.com/questions/25283110/how-to-set-printer-margin-in-java – Tilman Hausherr May 16 '17 at 16:36

0 Answers0