如何从Java.awt.print.PrinterJob获取PageFormat

时间:2023-01-28 20:00:16

I'm trying to use the PageFormat information to modify my javax.swing based printout prior to printing it. I am stumped as to how I can get the PageFormat from the PrintJob (which is obtained using getPrinterJob() and printDialog()). I know there is the getPageFormat method, but I can't figure out how to get the PrintRequestAttributeSet (which is not the printJob.getPrintService().getPrintAttributes()). Honestly, all I really want to know is the width and height of the page. Any ideas on how I can do that? Thanks.

我正在尝试使用PageFormat信息在打印之前修改基于javax.swing的打印输出。我很难理解如何从PrintJob(使用getPrinterJob()和printDialog()获得)获取PageFormat。我知道有getPageFormat方法,但我无法弄清楚如何获取PrintRequestAttributeSet(不是printJob.getPrintService()。getPrintAttributes())。老实说,我真正想知道的是页面的宽度和高度。关于我如何做到这一点的任何想法?谢谢。

2 个解决方案

#1


The approach of running a "fake" print job does NOT work, at least in the long run. I tried it. At first it worked great ... then some customers started reporting strange behaviors.

运行“假”打印作业的方法不起作用,至少从长远来看是这样。我尝试过这个。起初效果很好......然后一些客户开始报告奇怪的行为。

Turns out, with some printers and some configurations, even "fake" jobs cause activity at the printer. No document pages printed, but queue activity, job header pages, and sometimes a bit of garbage output (as I recall). The bogus queue entries were enough to interfere with normal printing, in some cases, and confusing to the user.

事实证明,对于某些打印机和一些配置,即使是“假”作业也会导致打印机活动。没有打印文档页面,但是队列活动,作业标题页面,有时还有一些垃圾输出(我记得)。在某些情况下,伪造队列条目足以干扰正常打印,并且使用户感到困惑。

Yes, getting the PageFormat ahead of time was a pain.

是的,提前获取PageFormat是一件痛苦的事。

#2


If you just need the page width and height, I think PageFormat.getImageableWidth() and PageFormat.getImageableHeight() are what you're looking for.

如果您只需要页面宽度和高度,我认为PageFormat.getImageableWidth()和PageFormat.getImageableHeight()是您正在寻找的。

Sorry, that's not what you wanted. How about this for a hack:

对不起,那不是你想要的。这对于黑客来说如何:

printable.setFakeJob(true);
job.print();
printable.setFakeJob(false);
job.print();

and in your printable,

在你的可打印的,

public int print(Graphics graphics, PageFormat pf, int pageIndex) {
    if (isFakeJob()) {
        /* use passed-in PageFormat to set up whatever you need to set up */
        return NO_SUCH_PAGE;
    } else {
        /* do your actual printing */
    }
}

Not pretty, admittedly.

不可否认,不是很好。

#1


The approach of running a "fake" print job does NOT work, at least in the long run. I tried it. At first it worked great ... then some customers started reporting strange behaviors.

运行“假”打印作业的方法不起作用,至少从长远来看是这样。我尝试过这个。起初效果很好......然后一些客户开始报告奇怪的行为。

Turns out, with some printers and some configurations, even "fake" jobs cause activity at the printer. No document pages printed, but queue activity, job header pages, and sometimes a bit of garbage output (as I recall). The bogus queue entries were enough to interfere with normal printing, in some cases, and confusing to the user.

事实证明,对于某些打印机和一些配置,即使是“假”作业也会导致打印机活动。没有打印文档页面,但是队列活动,作业标题页面,有时还有一些垃圾输出(我记得)。在某些情况下,伪造队列条目足以干扰正常打印,并且使用户感到困惑。

Yes, getting the PageFormat ahead of time was a pain.

是的,提前获取PageFormat是一件痛苦的事。

#2


If you just need the page width and height, I think PageFormat.getImageableWidth() and PageFormat.getImageableHeight() are what you're looking for.

如果您只需要页面宽度和高度,我认为PageFormat.getImageableWidth()和PageFormat.getImageableHeight()是您正在寻找的。

Sorry, that's not what you wanted. How about this for a hack:

对不起,那不是你想要的。这对于黑客来说如何:

printable.setFakeJob(true);
job.print();
printable.setFakeJob(false);
job.print();

and in your printable,

在你的可打印的,

public int print(Graphics graphics, PageFormat pf, int pageIndex) {
    if (isFakeJob()) {
        /* use passed-in PageFormat to set up whatever you need to set up */
        return NO_SUCH_PAGE;
    } else {
        /* do your actual printing */
    }
}

Not pretty, admittedly.

不可否认,不是很好。