3

I am trying to implement the printable interface in order to print a receipt. The following code is working but the problem it is adding a blank paper to the top of the needed receipt. Why are two pages printed instead of one (the blank paper have an A4 size)?

package slg.stock.util.print;

public class ForumPost implements Printable {
    private Paper receiptPaper;
    private double paperWidth = 2.2;
    private double paperHeight = 7;
    double leftMargin = 0.05;
    double rightMargin = 0.1;
    double topMargin = 0.4;
    double bottomMargin = 0.1;

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 1) {
            return Printable.NO_SUCH_PAGE;
        }
        receiptPaper = new Paper();
        receiptPaper.setSize(paperWidth * 72.0, paperHeight * 72.0);
        receiptPaper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
                (paperWidth - leftMargin - rightMargin) * 72.0,
                (paperHeight - topMargin - bottomMargin) * 72.0);

        pageFormat.setPaper(receiptPaper);
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        g2d.drawString("helo \n"
                + new Date()+ "  ", 5, 5);

        return Printable.PAGE_EXISTS;
    }

    public static void main(String args[]) {
        try {
            PrinterJob job = PrinterJob.getPrinterJob();
            ForumPost printer = new ForumPost();
            job.setPrintable(printer);
            job.print();
        } catch (PrinterException ex) {
        }
    }
}

I am not familiar with print API.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
kartor
  • 43
  • 4
  • `"I am not familiar with print Api."` -- your first step should be to get better familiarized with the API. At least that's what I would do if I were in your shoes. Much luck! – Hovercraft Full Of Eels Nov 10 '13 at 21:58
  • Not familiar mean , it is the first time, i have almost read all the oracle documentation. – kartor Nov 10 '13 at 22:02
  • OK, so having read the documentation, what in the documentation has you confused? What is your *specific* question other than asking others to fix "borrowed" code? Note that we'd much prefer to see *your* code attempts to solve this. – Hovercraft Full Of Eels Nov 10 '13 at 22:03
  • 2
    no confusion, my problem is to print a receipt i borrowed a code in order to evade searching the receipt paper size. the used code is working but the problem it is adding a blank paper to the top of the needed receipt why two page is printed instead of one. – kartor Nov 10 '13 at 22:07
  • Please put your last comment into your question since that is key information I think. You can edit the question and place it in there. – Hovercraft Full Of Eels Nov 10 '13 at 22:08
  • @Hovercraft Full Of Eels i hope that i have edited the question to be more understandable. – kartor Nov 10 '13 at 22:15
  • 1
    You shouldn't be trying to change the `Paper` within the `print` method, the `Paper` will already have being negotiated between Java and the printer. I also think `if (pageIndex > 1) {` is wrong, I think it should be `if (pageIndex > 0) {` if you only want one page... – MadProgrammer Nov 10 '13 at 23:24
  • Take a look at [this example](http://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237) which deals with setting a custom page size (in this case a label printer) – MadProgrammer Nov 10 '13 at 23:27

0 Answers0