4

I am using this code to print on paper:

//Overriden from printable interface
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
            throws PrinterException {

        if (pageIndex != 0) {
            return Printable.NO_SUCH_PAGE;
        }

        Paper a4 = new Paper();
        a4.setImageableArea(0, 0, a4.getWidth(), a4.getHeight());
        pageFormat.setPaper(a4);
        pageFormat.setOrientation(PageFormat.PORTRAIT);

        Graphics2D g2d = (Graphics2D)g;
        //g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        this.setDoubleBuffered(false);
        panel.print(g2d);
        JOptionPane.showMessageDialog(null, new ImageIcon(image));
        this.setDoubleBuffered(true);

        return Printable.PAGE_EXISTS;
    }

I am trying to reduce the size of the margins programmatically. Whatever I do, there always seems to be a large missing chunk from the image's sides (Unless I remove the margins from the print dialog - but as I said, I want to remove them programatically to automate the whole process).

mKorbel
  • 109,525
  • 20
  • 134
  • 319
David
  • 15,652
  • 26
  • 115
  • 156
  • 1
    hmm ... when "removing the margin from the print dialog" is it printing to the very edges of the paper? As opposed to making them as small ass the printer permits and then sscalr the image slightly to fit in? – kleopatra Sep 24 '11 at 08:06
  • @kleopatra Sorry for the misunderstanding. I wanted to make the margins as small as possible. The print dialog does not remove them completely, but almost does. I am also having trouble scaling the image. – David Sep 24 '11 at 09:05
  • You might see something you can use [here](http://stackoverflow.com/questions/7026822/printing-a-large-swing-component/7028497#7028497). – trashgod Sep 24 '11 at 18:55

1 Answers1

2

US Letter sized paper, for example, measures 8½ x 11 inches. At 72 dots per inch, that's 612 x 792.

On a typical printer having paper of that size selected, the PageFormat object reports the following area.

System.out.println(pf.getImageableX() + " " + pf.getImageableY()
    + " " + pf.getImageableWidth() + " " + pf.getImageableHeight());
18.0 18.0 576.0 734.0
18.0 18.0 576.0 734.0

Few consumer printers are full-bleed, so the pritable area is smaller than the paper's physical dimensions would suggest. In effect, the printer can't put ink where it can't print.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045