基于BS的Java+iText导出PDF报表
常常会需要按照模板把数据库的数据整理打印出来,尝试过ireport、word、PDF,各有各得优点,pdf在使用上是非常方便的,只要制作好模板,基本上就算是成功了一半了。下面完整的把web工程中到处pdf的流程列出来,希望对网友们有点帮助。
1.制作模板
1)首先,在word里面画好表格(注意是表格,刚开始的时候觉得表格对格式不好控制,就用画图工具里面的矩形画,那样是不行的),按照需要打印的格式。这一步是很重要的,因为如果word表格需要修改的话,会引起后面很多修改,工作量是很大的,所有提醒网友一定要确定最终模板后再进行操作。
2)然后就是导出pdf了。安装Adobe Acrobat (我用的是Adobe Acrobat 8 Professional),网上可以找到安装程序。安装后word里面会出现导出为pdf
的控件,,如果没有的话也可以选择文件--打印,打印机中选择Adobe pdf,打印出pdf。
3)修改pdf模板。用Adobe Acrobat打开pdf模板,运行“表单--运行表单域识别”,该工具就会自动识别出需要填写内容的表格。如果有个别地方不能识
别,可以点击“视图--工具栏--表单”找出表单工具,点击,可以在需要输入文本的地方画出文本域。双击识别出的文本域
(),修改“名称(当作该文本域的标志,程序中给该字段赋值就是通过这个标志进行的,所以这个名字不能重复,而且最好是有一定意义的)”、“外观--字体
()”,所有文本域都修改好保存。
2.导出代码
1)加载所需jar包,iText-2.1.3.jar(导出pdf所需包),iTextAsian.jar(亚洲语言包)。
2)整段代码如下:
1.public void genQRB() throws IOException {
2.
3.
4. HttpServletRequest request = ServletActionContext.getRequest();
5. HttpServletResponse response = ServletActionContext.getResponse();
6. response.reset();
7. ByteArrayOutputStream ba = new ByteArrayOutputStream();
8. ByteArrayOutputStream totalba = new ByteArrayOutputStream();
9. PushbuttonField pushbuttonField = null;
10. String path = request.getSession().getServletContext().getRealPath("/");
11.try {
12./* 打开已经定义好字段以后的pdf模板 */
13. String TemplatePDF = path + "pdf//qrb_zsb.pdf";
14.
15./* 使用中文字体 */
16. BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
17. BaseFont.NOT_EMBEDDED);
18. Font FontChinese = new Font(bf, 8, Font.NORMAL);
19. Font fontChinesesmall = new Font(bf, 6, Font.NORMAL);
20.
21. PdfReader reader = null;
22. PdfStamper stamp = null;
23. AcroFields form = null;
24. PdfCopyFields copy = new PdfCopyFields(totalba);
25. reader = new PdfReader(TemplatePDF);
26./* 将要生成的目标PDF文件名称 */
27. ba = new ByteArrayOutputStream();
28. stamp = new PdfStamper(reader, ba);
29.
30./* 取出报表模板中的所有字段 */
31. form = stamp.getAcroFields();
32./* 为字段赋值,注意字段名称是区分大小写的 */
33. form.setFieldProperty("name", "textfont", bf, null);
34.
35. ... ...
36.
37.//获取数据库中的数据
38. Bmxx bmxx = listBmxx();
39.
40. form.setField("xm", bmxx.getXm());
41. stamp.setFormFlattening(true);
42. reader.close();
43. stamp.close();
44. copy.addDocument(new PdfReader(ba.toByteArray()));
45. ba.close();
46. copy.close();
47. reader = new PdfReader(totalba.toByteArray());
48. stamp = new PdfStamper(reader, ba);
49. form = stamp.getAcroFields();
50. stamp.close();
51. reader.close();
52. System.out.println("------------正在导出:ba.size:" + ba.size()
53. + "||totalba.size():" + totalba.size());
54.
55. } catch (DocumentException de) {
56. de.printStackTrace();
57. System.err.println("A Document error:" + de.getMessage());
58. }
59.// setting some response headers
60. response.setHeader("Expires", "0");
61. response.setHeader("Cache-Control",
62."must-revalidate, post-check=0, pre-check=0");
63. response.setHeader("Pragma", "public");
64.
65. response.setContentType("application/pdf");
66.
67.
68./* 如果想出来让IE提示你是打开还是保存的对话框,加上下面这句就可以了 */
69. response.setHeader("Content-disposition", "attachment; filename="
70. + "bmxxqrb.pdf");
71.
72. response.setContentLength(ba.size());
73.try {
74. ServletOutputStream out = response.getOutputStream();
75. ba.writeTo(out);
76.// ba1.writeTo(out);
77. out.flush();
78. out.close();
79. ba.close();
80. totalba.close();
81. } catch (IOException e) {
82. e.printStackTrace();
83. System.err.println("A Document error:" + e.getMessage());
84. } finally {
85.if (ba != null)
86. ba.close();
87.if (totalba != null)
88. totalba.close();
89. }
90. }