缩小边距 - Java 打印

发布于 2024-12-06 06:25:13 字数 894 浏览 2 评论 0原文

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

//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 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-12-13 06:25:13

例如,US Letter 尺寸的纸张尺寸为 8.5 x 11 英寸。每英寸 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

很少有消费者打印机是全出血,因此可打印区域小于纸张的区域物理尺寸会建议。实际上,打印机无法将墨水放在无法打印的地方。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文