【问题标题】:Using a PrintDialog使用打印对话框
【发布时间】:2016-10-23 01:20:28
【问题描述】:

我有一个实现 Printable 的类。在那个类中,我有一个公共方法,它创建一个 BufferedImages 列表,每个都打印在一页上。现在我想添加一个 PrintDialog,以允许用户选择要打印的页面和要打印的份数。

我在互联网上做了一些研究,发现我可能必须使用 Book 类,但我不知道如何在我的情况下使用它。

谁能提供一个例子?感谢您的帮助...

好的。这是我正在使用的代码:

/**
 * Starts the print job
 * Allows variable scaling
 */
public void startPrint(float scale, JTable rowHeader, JTable mainTable, boolean includeRowHeaders, boolean includeColumnHeaders) throws PrinterException{
    //getPages returns a List<BufferedImage
    this.pages = getPages(scale, rowHeader, mainTable, includeRowHeaders, includeColumnHeaders);
    this.numberOfPages = this.pages.size();

    HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
    attr.add(new MediaPrintableArea(0f, 0f, 612/72f, 792/72f, MediaPrintableArea.INCH));
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    job.print(attr);
}

编辑 好的。我已经取得了一些进展。现在新问题。我现在可以选择页面范围,但是 PrintDialog 复制 JSpinner 没有响应,并且无论 JSpinner 文本字段包含什么,打印方法总是为每个页面调用两次。这是我的代码...

/**
 * Starts the print job
 * Allows variable scaling
 */
public void startPrint(float scale, JTable rowHeader, JTable mainTable, boolean includeRowHeaders, boolean includeColumnHeaders) throws PrinterException{
    //getPages returns a List<BufferedImage>
    this.pages = getPages(scale, rowHeader, mainTable, includeRowHeaders, includeColumnHeaders);
    this.numberOfPages = this.pages.size();

    HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
    attr.add(new JobName("Test Print", null));
    attr.add(new MediaPrintableArea(0f, 0f, 612/72f, 792/72f, MediaPrintableArea.INCH));
    attr.add(new PageRanges(1, this.numberOfPages));
    attr.add(new Copies(1));
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);

    if(job.printDialog(attr)){
        job.print(attr);
    }
}

/**
 * The actual print routine
 * Prints the class level List<BufferedImage>
 * one after another
 */
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
    if (pageIndex > this.numberOfPages - 1){
        return NO_SUCH_PAGE;
    }
    else{
        graphics.drawImage(this.pages.get(pageIndex), 0, 0, null);
        JOptionPane.showMessageDialog(null, pageIndex);
        return PAGE_EXISTS;
    }
}

【问题讨论】:

  • 请编辑您的问题以显示您当前拥有的代码。描述情况很好,但如果人们能够真正看到您正在处理的内容并在他们自己的系统上进行尝试,那么他们会更容易提供帮助。
  • 在您进行编辑时,如果您为所使用的语言添加标签会很有帮助。你在说什么Book类?

标签: java printing range bufferedimage printdialog


【解决方案1】:

我在编辑的问题中弄错了。我在 Microsoft XPS Document Writer 上进行测试。当我将项目转移到另一台带有真实物理打印机的 PC 时,它工作正常......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多