iText PDF 系列文章之一:continued, page3

--------------------------------------------------------------------------------------

原著:Bruno Lowagie  翻译:吴晓明 kfc@seu.edu.cn 东南大学

--------------------------------------------------------------------------------------


下面的代码示范了如何生成一个‘周报表(weekly report)’。如果你需要一个更为真实的例子,只需要将你对数据库的某个查询结果替换当前生成随机(Random)数据的方法:

PdfPTable table = new PdfPTable(10);
int[] COLUMNS = {5, 18, 15, 10, 10, 10, 10, 10, 10, 12 };
table.setWidths(COLUMNS);
table.setWidthPercentage(100);
PdfPCell cell;
for (int i = 0; i < 100; i++) {
table.addCell(String.valueOf(i + 1));
table.addCell(getRandomName());
table.addCell("DPT" + (int)(1 + Math.random() * 5));
int n = 0;
for (int j = 0; j < 6; j++) {
if (Math.random() < 0.5) {
n++;
cell = new PdfPCell(new Phrase("true"));
cell.setCellEvent(new RandomTable(true));
}
else {
cell = new PdfPCell(new Phrase("false"));
cell.setCellEvent(new RandomTable(false));
}
table.addCell(cell);
}
table.addCell(String.valueOf(n));
}

在上述代码片断中,你会发现 PdfPTable 和 PdfPCell 作为高层对象完成了所有工作。(注意,我将代码简化了;你从 CD-Rom 上面会找到完整的代码。) 看看生成的 PDF 报表;代表一周天数的单元格(从周一到周六)用一个小球作了标记。这个小球源自于某个包含低层 iText 代码的单元格事件。

public void cellLayout(
PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
if (present) cb.setRGBColorFill(0x00, 0xFF, 0x00);
else cb.setRGBColorFill(0xFF, 0x00, 0x00);
cb.ellipse(rect.left() + 3,
rect.bottom() + 3,
rect.left() + (rect.top() - rect.bottom() - 6),
rect.top() - 3);
cb.fill();
cb.resetRGBColorFill();
}

假设你有一个包含足够信息的数据库需要发布到 Internet 上,那么如果能将刚刚讨论的这些代码集成到某个Servlet 上面是多么美妙?

Mozilla 公开许可证

回答一下人们最最频繁提出的问题:下载 iText不但免费 (没有购买价格), 而且使用也是免费的(没有许可证付费)。 iText 是一个免费的/开源软件库。 这并不意味着你可以拿它为所欲为,我得告诉你不能骚扰开发人员或者幻想他们按照你的意愿做事情(这是目前我们所面临的最大问题之一)。的确需要一个许可证来保护开发人员。iText 的第一版本依据 LGPL 发布,但是一旦 iText 在 ICT 业务领域的主要人员之间受到普遍关注,那么向另一个许可证转变的压力将会越来越大。很多公司的律师对 LGPL 的某些诡诈的条款意见重重, 因此我们选择 MPL 和 LGPL 作为可选许可证。(为了向后兼容。)使用 iText 的应用程序可能保持不公开代码, 但是对 iText 的改进和修正必须是公开的。这样就会促成一种双赢局面:你可以从中获益,因为从官方发布的版本中你可以得到修正,从而减少了升级相关的问题, iText 社区可以从中获益,因为它可以从你对它的改进中进步。因为许可证的缘故,iText 已经从一个小的类文件包成长为一个专业库,嵌入到了无数的产品和应用程序当中。IBM 在它的 iSeries 400 系列机器上装载了 iText 作为打印池(print spooler)。这个库也作为PDF 引擎应用于许多报表工具中。例如JasperReports。它被集成到了 MacroMedia 公司的(现在是Adobe System) 服务器产品 Cold Fusion 当中。并且没有:没有收费。

将 iText 集成到 Web 应用程序

伴随此文、在线教程以及今后的书籍发布的所有事例代码都编排成了独立的小应用程序,因而可以很方便地在你的计算机上进行编译、运行和测试。但是据我目前所知,iText 主要应用于 web 应用程序的环境。下一段代码告诉了我们如何将 iText 集成到某个 Servlet 的 doGet 方法中。

public void doGet(
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String msg = (String) request.getParameter("msg");
if (msg == null || msg.trim().length() <= 0)
msg = "[ specify a 'msg' parameter in your URL ]";
Document document = new Document();
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter.getInstance(document, os);
document.open();
document.add(new Paragraph(msg));
document.close();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setContentLength(baos.size()); // workaround!
ServletOutputStream out = response.getOutputStream();
os.writeTo(out);
out.flush();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
原文地址:http://www.pdfdev.com/page/articles_1_3/
posted on 2006-08-20 12:45  OrientalDragon  阅读(775)  评论(0)    收藏  举报