65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ReportCat 中添加保存功能

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2013年9月12日

公共领域

2分钟阅读

viewsIcon

10675

为 Java 打印库 ReportCat 添加保存功能。

介绍  

我曾经有机会使用过 ReportCat (http://www.netcat.li/java-report-printing-library/)。ReportCat 是一个非常有效的 Java 报表打印库。它提供了一些很酷的功能来生成报表,例如预览/放大/缩小、缩放以及打印文档。它涵盖了所有功能,除了我认为非常重要的“保存”功能。

ReportCat 中,没有保存文档以供将来参考的功能。本文的目的是在 ReportCat API 中添加保存功能。可能还有其他保存方法,但我只想分享我所做的事情。

背景 

如果你查看 API 的源代码,你会发现 ReportCat 使用 Java 图形来写入(更准确地说,是绘制)所有内容。而这就是你可以挂钩来保存文档的地方。保存的主要概念是捕获图形对象,创建一个图像,最后将其保存为 PDF。

使用代码

首先,我们需要一个能够创建 PDF 的类。为了创建 PDF,我选择了 iTextpdf。它是一个开源且非常易于使用的库。你可以从 http://itextpdf.com/download.php 下载它。CreatePDf 是一个简单的类,它将 java.awt.Image 列表作为输入,并创建一个 PDF 作为输出。以下是 CreatePdf

//CreatePdf.java
//please add the neccessary imports
public class CreatePdf {
    public static void createPdf(int w, int h, String file, 
           List<Image> listImage) throws Exception {
        Rectangle pageSize = new Rectangle(0, 0, w, h);
        Document document = new Document(pageSize, 2, 2, 2, 2);
        try {
            PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
            com.itextpdf.text.Image image = null;
            boolean firstFlag = true;
            for (Image img : listImage) {
                try {
                    image = com.itextpdf.text.Image.getInstance(img, null);
                } catch (BadElementException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                try {
                    if (document != null && image != null) {
                        if (!firstFlag) {
                            document.newPage();
                        }
                        document.add(image);
                        firstFlag = false;
                    }
                } catch (DocumentException ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            if (document != null) {
                document.close();
            }

        }
    }
}

我们的 PDF 写入器类已经准备好使用了。现在我们必须将保存功能添加到 ReportCat 库中。如果你查看 api 源代码,你可以在 li.netcat.print 包中找到一个名为 PrintManager.java 的类。在这个类中,你需要添加以下代码

/**
* To save as PDF
*/	
public void save()
        throws PrinterException {

    JFileChooser fileChooser = new JFileChooser("C:/");

    int retrival = fileChooser.showSaveDialog(null);

    if (retrival == JFileChooser.APPROVE_OPTION) {
        System.out.println(fileChooser.getSelectedFile());

        try {
            List<Image> imageList = new ArrayList<Image>();
            BufferedImage bImg = null;
            Graphics2D cg = null;

            for (Object painter : d) {
                bImg = new BufferedImage(getPaperWidth(), 
                              getPaperHeight(), BufferedImage.TYPE_INT_RGB);
                cg = bImg.createGraphics();
                cg.setColor(Color.white);
                cg.fillRect(0, 0, getPaperWidth(), getPaperHeight());

                ((Painter) painter).paint(cg, getImageableX(), getImageableY());
                imageList.add(bImg);
                cg.dispose();
            }
            CreatePdf.createPdf(getPaperWidth(), getPaperHeight(), 
              fileChooser.getSelectedFile() + ".pdf", imageList);
            JOptionPane.showMessageDialog(null, "File saved successfully.");

        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}

最后的任务是在 GUI 端暴露此功能。为此,你需要修改 PrintPreview.java。这个类是一个示例类,你可以在 ReportCat 示例中找到它。这可能因你的应用程序而异。因此,基本目的是从 GUI 端调用保存按钮。为此,添加以下代码

// define a button	
private JButton  _saveButton;
// create the button
buttonPanel.add(_saveButton = createButton(SAVE, "Save as PDF"));

Finally call the save()

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == _saveButton) {
  try {
    _previewPanel.getPrintManager().save();
  }
  catch (PrinterException x) {
    x.printStackTrace();
  }
}
}

完成了。现在你的 ReportCat 具有将文档保存为 PDF 的功能。

环境

我已在 Linux 上使用 JDK 1.6 对其进行了测试,并且运行良好。

请注意

在修改 ReportCat API 源代码文件之前,请确保你拥有执行此操作的适当许可证

© . All rights reserved.