Search code examples
javaprintingprinters

Java print, Letter format instead of A4


As far as I know, Java Print uses the OS (windows) settings (format type) when sending a task to the printer. However, no matter how I change my code and despite the fact that all my settings are set for an A4, after sending a task to the printer it always accepts the task with a Letter format and I get a printer out of paper error. Although, when I press the restart printing button on the printer it prints out the file correctly. I have tried a lot of different methods from StackOverflow but they didn't really work out. I tried my application on another PC connected to the same model of printer - same problem. Maybe I'm missing something small or looking for the solution in the wrong place?

This is a piece of my code:

private PrinterJob createPDFPrinterJob(PDDocument pdfDocument) {

    PrinterJob printJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = new PageFormat();
    pageFormat.setOrientation(PageFormat.PORTRAIT);

    Paper paper = new Paper();
    pageFormat.setPaper(paper);

    try {
        printJob.setPrintable(new PDPageable(pdfDocument), pageFormat);
    }
    catch(PrinterException ex) {
        ex.printStackTrace();
    }

    return printJob;
}

Solution

  • So, I did a quick test using:

    Paper paper = new Paper();
    

    Which generates a page of 21.59x27.94 cms, which is consistent with Letter

    I then had a look at:

    PrinterJob.getPrinterJob().defaultPage().getPaper()
    

    Which generates a page of 21x29.7 cms, which is consistent with A4.

    So, my recommendation would be to use the PrinterJob's default PageFormat as a bases of your work