0
 public static String printer(String filePath, String printerName)
            throws InvalidPasswordException, IOException, PrinterException {

        String flag = "Error";
        PDDocument document = PDDocument.load(new File(filePath));

        for (int p = 0; p < document.getNumberOfPages(); ++p)
        {
            PDRectangle mediaBox = document.getPage(p).getMediaBox();
            mediaBox.transform(Matrix.getScaleInstance(0.5f, 0.5f));
            document.getPage(p).setMediaBox(mediaBox);
        }

        PrintService myPrintService = findPrintService(printerName);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new PDFPageable(document));
        job.setPrintService(myPrintService);
        job.print();
        System.out.println("printed!!");
        flag = "Success";
        job = null;
        myPrintService = null;
        document.close();
        return flag;
   }

I tried to print out local pdf files to printer. But I need to press a button "Ok" directly in printer.

I think problem is page size. So I need to set the size as A4 and then print right away.

Anyone can help me?

greg-449
  • 109,219
  • 232
  • 102
  • 145
mogi
  • 1
  • https://stackoverflow.com/questions/20904191/pdfbox-find-page-dimensions#:~:text=A4%20is%20a%20document%20format,X%20297mm%20%40%20300%20dpi%22) – Arun Sudhakaran Aug 08 '22 at 06:59
  • try `document.getPage(p).setCropBox(PDRectangle.A4)` but I'm not sure that this is the cause. Try printing a PDF of which you know that it is A4, does it work? Also, if the page content isn't A4 then weird things will happen. Try also using `PDFPrintable` instead of `PDFPageable`, this has resize options. – Tilman Hausherr Aug 08 '22 at 07:55
  • if PDF file is A4 then it prints but A3 to A4 doesnt work – mogi Aug 08 '22 at 08:24
  • Then you should really try to use `PDFPrintable` and read its javadoc. https://pdfbox.apache.org/docs/2.0.12/javadocs/org/apache/pdfbox/printing/PDFPrintable.html#PDFPrintable-org.apache.pdfbox.pdmodel.PDDocument-org.apache.pdfbox.printing.Scaling- – Tilman Hausherr Aug 08 '22 at 09:15
  • Oh I did it! It is matter of size I put 210 x 270 mm but it was wrong measures, put MediaBox().getWidth() and MediaBox().getHeight() then it works – mogi Aug 08 '22 at 09:33

0 Answers0