如何获得要打印的总页数? [英] How can I get the total number of pages to be printed?

查看:175
本文介绍了如何获得要打印的总页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自 Sun教程的基本打印程序示例a>:

This is the basic printing program example from the Sun tutorial:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new HelloWorldPrinter());
boolean doPrint = job.printDialog();
if (doPrint) {
    try {
        job.print();
    } catch (PrinterException e) {
        /* The job did not successfully complete */
    }
}

当用户显示打印对话框(在第二行)时,他可以选择仅打印文档中的一系列页面。我可以以某种方式获得要打印的页数吗?例如,如果我有25页文档,但用户选择打印4-10范围,则只打印7页。有没有办法访问这些信息?

When the user is shown the print dialog (on the second line), he can choose to print only a range of pages from the document. Can I somehow get the number of pages that are to be printed? For example, if I have a 25 page document, but the user chooses to print the range 4-10, then only 7 pages will be printed. Is there someway to access that information?

我需要这个以显示每个打印页面增加的进度条,但为此我需要知道总数将要打印的页面。

I need this in order to show a progress bar which increases with each page printed, but for that I need to know the total number of pages that are going to be printed.

那么我怎样才能得到这个数字?

So how can I get that number?

推荐答案

我设法找到了解决方案。

I've managed to find a solution.

显示 printDialog()方法本机打印对话框,但 printDialog(PrintRequestAttributeSet属性)方法显示跨平台对话框。使用用户的选择填写 PrintRequestAttributeSet 参数,包括选择要打印的页面范围。因此,从 printDialog 方法返回后,可以查询页面范围,如下面的代码序列:

The printDialog() method displays a native print dialog, but the printDialog(PrintRequestAttributeSet attributes) method shows a cross-platform dialog. The PrintRequestAttributeSet parameter is filled out with the user's selections, including the page range selected to be printed. Thus, after returning from the printDialog method, the page range can be queried, like in the following code sequence:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new HelloWorldPrinter());
HashPrintRequestAttributeSet printParams = new HashPrintRequestAttributeSet();
boolean ok = job.printDialog(printParams);
if (ok) {
    PageRanges pageRanges = (PageRanges) printParams.get(PageRanges.class);
    int pagesToBePrinted = getNumberOfPages(pageRanges);
    try {
        job.print(printParams);
    } catch (PrinterException e) {
        /* The job did not successfully complete */
    }
}

请注意 printParams 必须提供给 print()方法也是如此。从 PageRanges 对象,可以以数组格式获取页面范围,即1个长度数组的数组,每个数组意味着一个页面,或者2个长度数组意味着连续的范围页面。请参阅 javadoc 了解更多详情。计算总页数很简单:

Note that the printParams has to be given to the print() method as well. From the PageRanges object, the page ranges can be obtained in array format, i.e. an array of 1-length arrays meaning a single page each or 2-length arrays meaning contiguous ranges of pages. See the javadoc for more details. To calculate the total number of pages is straightforward:

int getNumberOfPages(PageRanges pageRanges) {
    int pages = 0;
    int[][] ranges = pageRanges.getMembers();
    for (int i = 0; i < ranges.length; i++) {
        pages += 1;
        if (ranges[i].length == 2) {
            pages += ranges[i][1] - ranges[i][0];
        }
    }
    pages = Math.min(pages, totalPagesOfDocument);
    return pages;
}

如果用户没有选择页面范围,但是'所有页面'而是选项,然后 PageRanges 将包含范围(1,Integer.MAX_VALUE)。所以我说如果计算出的值超过了文档的页数,那么要打印的页数就是文档页面的总数(我希望你从某个地方知道)。

If the user doesn't select a page range, but the 'All pages' option instead, then the PageRanges will contain the range (1, Integer.MAX_VALUE). So I say that if the computed value exceeds the number of pages of the document, then the number of pages to be printed is the total number of the document's pages (which I hope you know from somewhere).

该算法可能过度,因为 PageRanges 可能只是一个简单的 n - m 范围,但比抱歉更安全。

The algorithm is perhaps overkill, as probably the PageRanges will only be a simple n - m range, but better safe than sorry.

这篇关于如何获得要打印的总页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆