4

我正在尝试从 Java 小程序打印自定义纸张尺寸。我已经设置了纸张尺寸,但它被忽略了。

我也尝试过使用 book 方法,因为我已经看到了一些关于这有助于让它工作但是当我使用它时它只是打印一个空白页并且似乎仍然是关于 A4 的(我正在寻找打印显然很多的支票更小 (8" x 4"))。

如果这有什么不同,我正在尝试从 JEditorPane 打印 HTML。

如果您有任何想法,我将不胜感激,我正在用这个把头发扯掉。

我还应该补充一点,当谈到 Java 时,我是一个初学者。

这是我到目前为止所拥有的:

更新: 我现在已经获得了正确的页面大小,但似乎无法让我正在加载的 HTML 页面适合或对齐页面大小。

更新: 现在我无法让小程序在浏览器中运行。它适用于 Eclipse,而不是浏览器。我还需要从参数传递 URL。

这是我正在使用和更新的 Java 代码的 HTML 小程序标记:

<!DOCTYPE html>
<html>
<head><title>Printing Cheque</title></head>
<body>
<applet width=100 height=100 code="HTMLPrinter"
        archive="cheque_print.jar">
</applet>

</body>
</html>


package com.yunatech.pns.chequeprint;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JEditorPane;

public class HTMLPrinter extends Applet {

    /**
     * 
     */
    private static final long serialVersionUID = 8065834484717197790L;
    private static JEditorPane editor;

    public HTMLPrinter() {


        try {

            editor = new JEditorPane();
            editor.setPage("http://localhost/print_test/test.html");

            PrinterJob pj = PrinterJob.getPrinterJob();
            if (pj.printDialog()) {
                PageFormat pf = pj.defaultPage();
                Paper paper = pf.getPaper();
                double width = 8d * 72d;
                double height = 4d * 72d;
                double margin = 1d * 72d;
                paper.setSize(width, height);
                paper.setImageableArea(
                        margin,
                        0,
                        width - (margin * 2),
                        height);
                System.out.println("Before- " + dump(paper));
                pf.setOrientation(PageFormat.PORTRAIT);
                pf.setPaper(paper);
                System.out.println("After- " + dump(paper));
                System.out.println("After- " + dump(pf));
                dump(pf);
                PageFormat validatePage = pj.validatePage(pf);
                System.out.println("Valid- " + dump(validatePage));

                Book pBook = new Book();
                pBook.append(new Page(), pf);
                pj.setPageable(pBook);

                try {
                    pj.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                .append("/").append(paper.getImageableX()).append("x").
                append(paper.getImageableY()).append(" - ").append(paper
                .getImageableWidth()).append("x").append(paper.getImageableHeight());
        return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class Page implements Printable {

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
            if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;

            Graphics2D g2d = (Graphics2D)graphics;
            g2d.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());

            editor.setSize((int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());
            editor.print(g2d);

            return Printable.PAGE_EXISTS;
        }
    }
}

提前感谢您提供的任何帮助。

4

1 回答 1

9

打印设计为以每英寸像素为单位。基本打印 API 使用 72 的 DPI。

您需要相应地转换您的测量值...

double paperWidth = 8 * 72d;
double paperHeight = 4 * 72d;
double margin = 1 * 72d;

更新示例

g2d.setClip(0, 0, (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());不建议,危险,通常不需要,除此之外,您使用了错误的宽度和高度值。可成像参数考虑了边距,但您没有翻译图形,这很可能会切割您必须打印到的区域的底部左侧部分......

我只是避免使用剪辑

在此处输入图像描述

public class TestPrinting01 {

    public static void main(String[] args) {

        PrinterJob pj = PrinterJob.getPrinterJob();
        if (pj.printDialog()) {
            PageFormat pf = pj.defaultPage();
            Paper paper = pf.getPaper();
            double width = 8d * 72d;
            double height = 4d * 72d;
            double margin = 1d * 72d;
            paper.setSize(width, height);
            paper.setImageableArea(
                    margin,
                    margin,
                    width - (margin * 2),
                    height - (margin * 2));
            System.out.println("Before- " + dump(paper));
            pf.setOrientation(PageFormat.LANDSCAPE);
            pf.setPaper(paper);
            System.out.println("After- " + dump(paper));
            System.out.println("After- " + dump(pf));
            dump(pf);
            PageFormat validatePage = pj.validatePage(pf);
            System.out.println("Valid- " + dump(validatePage));

            Book pBook = new Book();
            pBook.append(new Page(), pf);
            pj.setPageable(pBook);

            try {
                pj.print();
            } catch (PrinterException ex) {
                ex.printStackTrace();
            }
        }
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                .append("/").append(paper.getImageableX()).append("x").
                append(paper.getImageableY()).append(" - ").append(paper
                .getImageableWidth()).append("x").append(paper.getImageableHeight());
        return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class Page implements Printable {

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
            if (pageIndex >= 1) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2d = (Graphics2D) graphics;
            // Be careful of clips...
            g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());

            double width = pageFormat.getImageableWidth();
            double height = pageFormat.getImageableHeight();

            g2d.drawRect(0, 0, (int)pageFormat.getImageableWidth() - 1, (int)pageFormat.getImageableHeight() - 1);
            FontMetrics fm = g2d.getFontMetrics();
            String text = "top";
            g2d.drawString(text, 0, fm.getAscent());

            text = "bottom";
            double x = width - fm.stringWidth(text);
            double y = (height - fm.getHeight()) + fm.getAscent();
            g2d.drawString(text, (int)x, (int)y);

            return Printable.PAGE_EXISTS;
        }
    }
}

更新

打印组件时,您将负责其布局。

在此处输入图像描述

public class TestPrinting01 {

    private static JEditorPane editor;

    public static void main(String[] args) {

        try {

            editor = new JEditorPane();
            editor.setPage(new File("C:/hold/search.htm").toURI().toURL());

            PrinterJob pj = PrinterJob.getPrinterJob();
            if (pj.printDialog()) {
                PageFormat pf = pj.defaultPage();
                Paper paper = pf.getPaper();
                double width = 8d * 72d;
                double height = 4d * 72d;
                double margin = 1d * 72d;
                paper.setSize(width, height);
                paper.setImageableArea(
                        margin,
                        margin,
                        width - (margin * 2),
                        height - (margin * 2));
                System.out.println("Before- " + dump(paper));
                pf.setOrientation(PageFormat.LANDSCAPE);
                pf.setPaper(paper);
                System.out.println("After- " + dump(paper));
                System.out.println("After- " + dump(pf));
                dump(pf);
                PageFormat validatePage = pj.validatePage(pf);
                System.out.println("Valid- " + dump(validatePage));

                Book pBook = new Book();
                pBook.append(new Page(), pf);
                pj.setPageable(pBook);

                try {
                    pj.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                .append("/").append(paper.getImageableX()).append("x").
                append(paper.getImageableY()).append(" - ").append(paper
                .getImageableWidth()).append("x").append(paper.getImageableHeight());
        return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class Page implements Printable {

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
            if (pageIndex >= 1) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2d = (Graphics2D) graphics;
            // Be careful of clips...
//            g2d.setClip(0, 0, (int) pageFormat.getWidth(), (int) pageFormat.getHeight());
            g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());

            double width = pageFormat.getImageableWidth();
            double height = pageFormat.getImageableHeight();

            System.out.println("width = " + width);
            System.out.println("height = " + height);

            editor.setLocation(0, 0);
            editor.setSize((int)width, (int)height);
            editor.printAll(g2d);

            g2d.setColor(Color.BLACK);
            g2d.draw(new Rectangle2D.Double(0, 0, width, height));

            return Printable.PAGE_EXISTS;
        }
    }
}
于 2012-11-26T03:32:58.207 回答