java中如何设置打印页面的大小?

发布于 2024-12-18 15:01:46 字数 1007 浏览 2 评论 0原文

我编写了一个程序,它使用 Java 打印 API 从打印机打印页面。我相信我已经输入代码将页面大小设置为字母,但它仍然以打印机默认的任何大小打印。这是我的 printPage() 方法:

public void printPage() {
    getTot();
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    Paper paper = pf.getPaper();
    paper.setSize(8.5 * 72, 11 * 72);
    paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 10.5 * 72);
    pf.setPaper(paper);
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
        if (cov)
            try {
                for (j = 0; j < printPaths.size(); j++)
                    job.print();
                cov = false;
            } catch (PrinterException ex) {
                System.out.println(ex.getMessage());
            }
        if (summ)
            try {
                job.print();
                summ = false;
            } catch (PrinterException ex) {
                System.out.println(ex.getMessage());
            }
    }

}

我做错了什么?

I have written a program that uses the Java print API to print pages from a printer. I believe I have put in code to set the page size to letter, but it still prints on whatever size is default for the printer. Here is my printPage() method:

public void printPage() {
    getTot();
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    Paper paper = pf.getPaper();
    paper.setSize(8.5 * 72, 11 * 72);
    paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 10.5 * 72);
    pf.setPaper(paper);
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
        if (cov)
            try {
                for (j = 0; j < printPaths.size(); j++)
                    job.print();
                cov = false;
            } catch (PrinterException ex) {
                System.out.println(ex.getMessage());
            }
        if (summ)
            try {
                job.print();
                summ = false;
            } catch (PrinterException ex) {
                System.out.println(ex.getMessage());
            }
    }

}

What am I doing wrong?

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

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

发布评论

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

评论(2

月朦胧 2024-12-25 15:01:46

您可以尝试添加这段代码并重新运行:

Book book = new Book();//java.awt.print.Book
book.append(this, pf);
job.setPageable(book);

而不是

job.setPrintable(this);

Can you try adding this piece of code and rerun :

Book book = new Book();//java.awt.print.Book
book.append(this, pf);
job.setPageable(book);

instead of

job.setPrintable(this);
昨迟人 2024-12-25 15:01:46

本书对我的多页示例不起作用(结果是仅打印第一页),但它给了我另一个想法。而不是:

Book book = new Book();//java.awt.print.Book
book.append(this, pf);
job.setPageable(book);

我尝试了另一种重载方法,它适用于所有页面:

PageFormat pf = job.defaultPage();
pf.setPaper(paper);
job.setPrintable(this, pf);

如果您想要自定义纸张尺寸,则需要将其作为参数转发给 print 方法(Java 会为您执行此操作,但您必须将其传递给 setPrintable 方法)。方法开始执行后更改 PaperFormat 仅对第二页及以后的页面有影响,或者根本没有任何影响。

不管怎样,谢谢你们俩。

Book didn't work for me with my multiple pages example (the result was the printing of only the first page), but it gave me another idea. Instead of :

Book book = new Book();//java.awt.print.Book
book.append(this, pf);
job.setPageable(book);

I tried another overloaded method and it worked for all pages:

PageFormat pf = job.defaultPage();
pf.setPaper(paper);
job.setPrintable(this, pf);

If you want custom paper size, you need to forward it to print method as an argument (which Java will do for you but you must pass it to setPrintable method). Changing PaperFormat after method starts to execute will have an effect only on the second and further pages or not have any effect at all.

Anyway, thanks to you both.

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