java:设置打印对话框的页面范围

发布于 2024-11-13 08:04:56 字数 1060 浏览 3 评论 0原文

我刚刚开始 学习如何在 Java/Swing 中打印窗口。 (编辑:刚刚找到 Java 打印指南

当我执行此操作时:

protected void doPrint() {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print();
        } 
        catch (PrinterException ex) {
            ex.printStackTrace();
        } 
        finally {

        }
    }
}

我收到此打印机对话框(在 Windows XP 上):

在此处输入图像描述

如何更改页面范围使其不再是 1-9999?

编辑:使用 Pageable/Book 设置页面范围(正如 @t_barbz 有用指出的那样)需要 PageFormat,在这种情况下我有一个 catch-22,因为我想要打印对话框来选择它,我似乎没有从打印对话框中获得返回值。

I'm just starting to learn how to print a window in Java/Swing.
(edit: just found the Java Printing Guide)

When I do this:

protected void doPrint() {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print();
        } 
        catch (PrinterException ex) {
            ex.printStackTrace();
        } 
        finally {

        }
    }
}

I get this printer dialog (on Windows XP):

enter image description here

How do I change the page range so it's not 1-9999?

edit: using Pageable/Book to set the page range (as @t_barbz helpfully points out) requires a PageFormat, in which case I have a catch-22, since I'd like the Print dialog to select that, and I don't seem to get a return value from the print dialog.

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

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

发布评论

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

评论(2

琉璃繁缕 2024-11-20 08:04:56

对于页面范围,我相信您需要使用 PrinterJob 的 setPageable(Pageable document) 方法。看起来应该可以解决问题。

protected void doPrint() {
PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(this, job.defaultPage());
printJob.setPageable(book);

boolean ok = job.printDialog();
if (ok) {
    try {
        job.print();
    } 
    catch (PrinterException ex) {
        ex.printStackTrace();
    } 
    finally {

    }
}
}

For the page range i believe you need to use the PrinterJob's setPageable(Pageable document) method. Looks like it should do the trick.

protected void doPrint() {
PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(this, job.defaultPage());
printJob.setPageable(book);

boolean ok = job.printDialog();
if (ok) {
    try {
        job.print();
    } 
    catch (PrinterException ex) {
        ex.printStackTrace();
    } 
    finally {

    }
}
}
妥活 2024-11-20 08:04:56

最后是一个简单的代码:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
PrintRequestAttributeSet printAttribute = new HashPrintRequestAttributeSet();
printAttribute.add(new PageRanges(1, 100));        
boolean ok = job.printDialog(printAttribute);
if (ok) {
     try {
          job.print();
     } catch (PrinterException ex) {
      /* The job did not successfully complete */
     }
}

Finally here is a simple code:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
PrintRequestAttributeSet printAttribute = new HashPrintRequestAttributeSet();
printAttribute.add(new PageRanges(1, 100));        
boolean ok = job.printDialog(printAttribute);
if (ok) {
     try {
          job.print();
     } catch (PrinterException ex) {
      /* The job did not successfully complete */
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文