4

I'm using the following code to print some text on a thermal printer with 80mm roll paper:

public class printnow {

    public static void printCard(final String bill) {
        final PrinterJob job = PrinterJob.getPrinterJob();

        Printable contentToPrint = new Printable() {
            @Override
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws  PrinterException {
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                g2d.setFont(new Font("Monospaced", Font.BOLD, 7));
                pageFormat.setOrientation(PageFormat.PORTRAIT);

                Paper pPaper = pageFormat.getPaper();
                pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
                pageFormat.setPaper(pPaper);

                if (pageIndex > 0) 
                    return NO_SUCH_PAGE; //Only one page

                String Bill [] = bill.split(";");
                int y = 0;
                for (int i = 0; i < Bill.length; i++) {
                    g2d.drawString(Bill[i], 0, y);
                    y = y + 15;
                }

                return PAGE_EXISTS;
            }
        };  

        boolean don = job.printDialog();

        job.setPrintable(contentToPrint);

        try {
            job.print();
        } catch (PrinterException e) {
            System.err.println(e.getMessage());
        }
    }
}

This is printing extremely fine and is exactly what I want. But when I remove the following line to disable the print dialog box and automate the process of printing, my print messes up and the printer automatically takes some margin in the left.

boolean don = job.printDialog();

Any idea on why this is happening and how can it be solved?

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85

1 Answers1

2

After a lot of research and a applying a little brain, I found out the solution. It was a very small but silly mistake. Read the following source code:

public class printnow {

    public static void printCard(final String bill ) {
        final PrinterJob job = PrinterJob.getPrinterJob();

        Printable contentToPrint = new Printable() {
            @Override
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                g2d.setFont(new Font("Monospaced", Font.BOLD, 7));

                if (pageIndex > 0) {
                    return NO_SUCH_PAGE;
                } //Only one page

                String Bill [] = bill.split(";");
                int y = 0;
                for (int i = 0; i < Bill.length; i++) {
                    g2d.drawString(Bill[i], 0, y);
                    y = y + 15;
                }

                return PAGE_EXISTS;
            }
        };

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

        Paper pPaper = pageFormat.getPaper();
        pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
        pageFormat.setPaper(pPaper);

        job.setPrintable(contentToPrint, pageFormat);

        try {
            job.print();
        } catch (PrinterException e) {
            System.err.println(e.getMessage());
        }
    }
}

In the previous source code (the wrong one), when the application triggers a print dialog box and the user clicks OK, the default printing preferences are transferred to the Java app and it prints what is required. but when we disable the print dialog box by removing this line: boolean don = job.printDialog();

an unknow PageFormat is transferred which arises out of nowhere. The problem was not with our defined PageFormat, the problem was that the pageFormat is passed to a printing method which we were not doing initially:

job.setPrintable(contentToPrint, pageFormat);

Notice the second parameter being passed to the above method. Hope this helps everyone. :)

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53