缩小边距 – Java打印

我正在使用此代码在纸上打印:

//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;
    }

我试图以编程方式减小边距的大小.无论我做什么,图像的两侧似乎总有一个大块缺失(除非我从打印对话框中删除边距 – 但正如我所说,我想以编程方式删除它们以使整个过程自动化).

最佳答案 例如,
US Letter尺寸的纸张尺寸为8½x11英寸.每英寸72点,即612 x 792.

在选择了该尺寸纸张的典型打印机上,PageFormat对象报告以下区域.

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

很少有消费者打印机是full-bleed,因此可打印区域小于纸张的物理尺寸.实际上,打印机无法将墨水放在无法打印的地方.

点赞