在企业的信息系统中,报表处理一直占比较重要的作用,iText是一种生成PDF报表的Java组件。通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超链接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。

iText简介

iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。

一、关于Itext的安装

IText的安装非常方便,在http://itextpdf.com/ 网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。本次示例所使用的jar包有三个。

下载地址:https://pan.baidu.com/s/1VO4J74BK5m7eRLf73KN-4Q

二、项目搭建。

本示例使用SSM架构,具体项目搭建流程请访问博客查看:https://blog.csdn.net/Soulmate_Min

三、构建代码

首先,将下载好的jar包导入项目,将jar包Build Path。

1.写测试类:本示例创建class名为GetReportController

2.添加两张图片,一张为LOGO,一张为水印图片。作为备用。

3.测试类代码展示:

       代码如下:

package com.gcx.test.controller;import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gcx.test.util.NewCreditReportUtil;@Controller
@RequestMapping("/CreditReporterController")public class GetReportController {@RequestMapping("/getreport")@ResponseBodypublic String getreport(String corpname,HttpServletRequest request) throws Exception{//报告准备工作//生成的PDF报告编号String reportNo = "GCX-2"+System.currentTimeMillis();//PDF文件名称String pdfFileName = reportNo + ".pdf";//水印图片地址String watermarkFilePath = request.getSession().getServletContext().getRealPath("/images/watermark.png");//公司LOGO地址String reportLogoFilePath = request.getSession().getServletContext().getRealPath("/images/credit_report_logo.png");//定义一个Map,将生成的PDF需要的参数填入Map, Map将作为参数传到PDF模板中。Map<String, Object> NewCorpReportMap=new  HashMap<>();NewCorpReportMap.put("reportLogoFilePath", reportLogoFilePath);NewCorpReportMap.put("reportNo", reportNo);NewCorpReportMap.put("watermarkFilePath", watermarkFilePath);NewCorpReportMap.put("corpname", corpname);//调取PDF工具类中的方法,将Map参数传入。NewCreditReportUtil.generateDeepthCreditReport(NewCorpReportMap);String s1 = "C://Users//sungm//Desktop//Test" + pdfFileName;//给文件加水印NewCreditReportUtil.addPdfMark("C:/Users/sungm/Desktop/Test/GCX-creditreport-template.pdf", s1, NewCorpReportMap.get("watermarkFilePath").toString());return reportNo;}
}

4.生成PDF所需工具类展示:

/**
 * 用iText生成PDF文档需要5个步骤:
 * 
 * ①建立com.lowagie.text.Document对象的实例。
 * Document document = new Document(); 
 * 
 * ②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
 * PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF")); 
 * 
 * ③打开文档。
 * document.open(); 
 * 
 * ④向文档中添加内容。
 * document.add(new Paragraph("Hello World")); 
 * 
 * ⑤关闭文档。
 * document.close(); 
 *
 */

添加水印方法:

paragraph格式:

居中无边框的cell:

不居中无边框的cell:

居中有边框的cell:

居中有边框的cell:

对doc文件添加页眉页脚方法:

设置字体:

插入图片:

首页--固定格式布局:本示例固定的格式,具体样式可以根据需求自己修改

非空判断:

完整工具类代码(包含以上所有示例):

package com.gcx.test.util;import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;/*** 用iText生成PDF文档需要5个步骤:* * ①建立com.lowagie.text.Document对象的实例。* Document document = new Document(); * * ②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。* PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF")); * * ③打开文档。* document.open(); * * ④向文档中添加内容。* document.add(new Paragraph("Hello World")); * * ⑤关闭文档。* document.close(); **/
public class NewCreditReportUtil {//定义静态变量,用于生成水印文件名称private final static String RESULT_FILE = "C:/Users/sungm/Desktop/Test/GCX-creditreport-template.pdf";public static boolean generateDeepthCreditReport(Map<String, Object> NewCorpReportMap)throws MalformedURLException, IOException, DocumentException {//①建立com.lowagie.text.Document对象的实例。Document doc = new Document();doc.setMargins(20, 20, 30, 30);String fontPath = "C:/Users/sungm/Desktop/Test";//②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。PdfWriter.getInstance(doc, new FileOutputStream(RESULT_FILE));// 设置中文字体BaseFont fontChinese = BaseFont.createFont(fontPath + "/simfang.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font chinese = new Font(fontChinese, 10, Font.NORMAL);// 添加页眉页脚String headertitle = "*****报告" + "         " + "*******";addheaderandfooter(doc, chinese, headertitle);//③打开文档。doc.open();// 18.0F是字体大小,0表示字体倾斜度, font.setStyle(1); 0:无变化1:加粗;2:斜体...// font.setFamily("微软雅黑"); // 设置字体Font myfont  = setfont(fontPath + "/msyh.ttf", 13.0F, 0, Color.BLACK, 0);// 基本字体//Font myfont1 = setfont(fontPath + "/msyh.ttf", 36.0F, 0, Color.BLACK, 1);// 标头字体(一级字体)//Font myfont2 = setfont(fontPath + "/msyh.ttf", 27.0F, 0, Color.BLACK, 1);// 标头字体(二级字体)Font myfont3 = setfont(fontPath + "/msyh.ttf", 18.0F, 0, Color.BLACK, 1);// 标头字体(三级字体)//Font myfont4 = setfont(fontPath + "/msyh.ttf", 13.0F, 0, Color.BLACK, 1);// 标头字体(四级字体)//Font myfont5 = setfont(fontPath + "/msyh.ttf", 12.0F, 0, Color.BLACK, 0);// 标头字体(五级字体)// 初始化pdf基本功能性文本Image image = null;PdfPTable table;PdfPCell cell = null;Paragraph paragraph = null;// 准备工作结束,进行文档内容填充:// 添加公司logo图片table = new PdfPTable(1);String picpath = NewCorpReportMap.get("reportLogoFilePath").toString();addpicture(table, image, picpath, cell, doc);// 添加报告信息firstpage(cell, table, paragraph, NewCorpReportMap.get("corpname").toString(), "企业信用报告",NewCorpReportMap.get("reportNo").toString(), myfont, myfont3, doc);// 第二页 (固定死页面)doc.newPage();doc.add(new Paragraph("       ", myfont));paragraph = new Paragraph("报告说明", myfont3);paragraph.setAlignment(1);doc.add(paragraph);doc.add(new Paragraph("       ", myfont));geshi1(new Paragraph("1. 内容1",myfont), doc);geshi1(new Paragraph("2. 内容2", myfont), doc);geshi1(new Paragraph("3. 内容3", myfont), doc);geshi1(new Paragraph("4. 内容4", myfont), doc);// 第三页 报告摘要,每页空2行留给页眉doc.newPage();doc.add(new Paragraph("       ", myfont));doc.add(new Paragraph("       ", myfont));doc.close();return false;}/*** 给pdf文件添加水印* * @param InPdfFile*            要加水印的原pdf文件路径* @param outPdfFile*            加了水印后要输出的路径* @param object*            水印图片路径* @param pageSize*            原pdf文件的总页数(该方法是我当初将数据导入excel中然后再转换成pdf所以我这里的值是用excel的行数计算出来的,*            如果不是我这种可以 直接用reader.getNumberOfPages()获取pdf的总页数)* @throws Exception*/public static void addPdfMark(String InPdfFile, String outPdfFile, String readpicturepath) throws Exception {PdfReader reader = new PdfReader(InPdfFile);int pageSize = reader.getNumberOfPages();PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));Image img = Image.getInstance(readpicturepath);// 插入水印img.setAbsolutePosition(0, 0);for (int i = 1; i <= pageSize; i++) {PdfContentByte under = stamp.getUnderContent(i);under.addImage(img);}stamp.close();// 关闭File tempfile = new File(InPdfFile);if (tempfile.exists()) {tempfile.delete();}}// paragraph的格式public static void geshi1(Paragraph paragraph, Document doc) throws DocumentException {// 段落的格式paragraph.setIndentationLeft(30);paragraph.setIndentationRight(30);paragraph.setFirstLineIndent(20f);paragraph.setSpacingAfter(10f);paragraph.setSpacingBefore(10f);doc.add(paragraph);}// 居中无边框的cellpublic static void geshi2(PdfPCell cell, PdfPTable table) throws DocumentException {// 表格的格式cell.setBorder(PdfPCell.NO_BORDER);cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);table.addCell(cell);}// 不居中无边框的cellpublic static void geshi12(PdfPCell cell, PdfPTable table) throws DocumentException {// 表格的格式cell.setBorder(PdfPCell.NO_BORDER);table.addCell(cell);}// 居中有边框的cellpublic static void geshi22(PdfPCell cell, PdfPTable table) throws DocumentException {// 表格的格式cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);table.addCell(cell);}// 居中有边框的cellpublic static void geshi32(PdfPCell cell, PdfPTable table) throws DocumentException {// 表格的格式cell.setColspan(3);cell.setBorder(0);table.addCell(cell);}// 对doc文件添加页眉页脚方法public static void addheaderandfooter(Document doc, Font chinese, String headertitle) {// 设置页眉页脚方法/*** HeaderFooter的第2个参数为非false时代表打印页码 页眉页脚中也可以加入图片,并非只能是文字*/HeaderFooter header = new HeaderFooter(new Phrase(headertitle, chinese), false);header.setBorder(Rectangle.NO_BORDER);header.setBorder(Rectangle.BOTTOM);header.setAlignment(1);header.setBorderColor(Color.red);doc.setHeader(header);HeaderFooter footer = new HeaderFooter(new Phrase("第-", chinese), new Phrase("-页", chinese));/*** 0是靠左 1是居中 2是居右*/footer.setAlignment(1);footer.setBorderColor(Color.red);footer.setBorder(Rectangle.NO_BORDER);doc.setFooter(footer);/*** 页眉页脚的设置一定要在open前设置好*/}// 设置字体public static Font setfont(String fonttype, float fontsize, int fontflag, Color fontcolor, int fontstyle)throws DocumentException, IOException {BaseFont baseFont5 = BaseFont.createFont(fonttype, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font font = new Font(baseFont5, fontsize, fontflag);font.setColor(fontcolor);if (fontstyle != 0) {// 如果传参为0不设置字体font.setStyle(fontstyle);}return font;}// 插入图片public static void addpicture(PdfPTable table, Image image, String picpath, PdfPCell cell, Document doc)throws MalformedURLException, IOException, DocumentException {image = Image.getInstance(picpath);cell = new PdfPCell(image);geshi2(cell, table);doc.add(table);}// 首页--固定格式布局public static void firstpage(PdfPCell cell, PdfPTable table, Paragraph paragraph, String corpname,String reporttype, String reportNo, Font myfont, Font myfont3, Document doc) throws DocumentException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");String newyear = sdf.format(new Date());paragraph = new Paragraph(corpname + newyear, myfont3);// 公司名paragraph.setAlignment(1);doc.add(paragraph);paragraph = new Paragraph("企业信用报告", myfont3);// 信用报告类型paragraph.setAlignment(1);doc.add(paragraph);doc.add(new Paragraph("       ", myfont));doc.add(new Paragraph("       ", myfont));doc.add(new Paragraph("       ", myfont));doc.add(new Paragraph("       ", myfont));doc.add(new Paragraph("       ", myfont));doc.add(new Paragraph("       ", myfont));table = new PdfPTable(2);cell = new PdfPCell(new Phrase("报告编号:" + reportNo, myfont));cell.setBorder(PdfPCell.NO_BORDER);// Rectangle.NO_BORDERtable.addCell(cell);cell = new PdfPCell(new Phrase("报告生成时间:" + sdf.format(new Date()), myfont));cell.setBorder(PdfPCell.NO_BORDER);// Rectangle.NO_BORDERtable.addCell(cell);cell = new PdfPCell(new Phrase("报告生成机构:*****有限公司", myfont));cell.setBorder(PdfPCell.NO_BORDER);// Rectangle.NO_BORDERtable.addCell(cell);cell = new PdfPCell(new Phrase("报告结论机构:******研究院", myfont));cell.setBorder(PdfPCell.NO_BORDER);// Rectangle.NO_BORDERtable.addCell(cell);doc.add(table);}//非空判断public static String isnull(Object a) {if (a != null && a != "" && a != "null" && a.toString() != "null") {return a.toString();} else {return "";}}
}

完成后,启动项目测试功能是否存在问题:

打开浏览器,在地址栏中访问接口:

http://localhost:8080/test/CreditReporterController/getreport.do?corpname=*****有限公司

访问成功后,返回文件路径:

文件自动下载到桌面:

打开PDF文件,查看格式是否正确:

成功生成PDF文件。

接口测试成功。

Java生成PDF文件(Itext篇)相关推荐

  1. Java生成PDF文件_自用

    Java生成PDF文件 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iText--用于生成PDF文档的一个Java类库.废话不多说,进入正题. 二.iText简介 ...

  2. iText5实现Java生成PDF文件完整版,二维码

    iText5实现Java生成PDF文件完整版 vue 项目中分别使用 vue-pdf 插件和内嵌 iframe 实现 PDF 文件预览,缩放,旋转,下载,保存等功能 ? Vue打印文件(v-print ...

  3. # Java 生成pdf文件

    Java 生成pdf文件 引入依赖 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <depend ...

  4. [itext]Java生成PDF文件

    一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...

  5. Java生成PDF文件,java面试题,java初级笔试题

    写在最前面,我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家.扫码加微信好友进[程序员面试学习交流群],免费领取.也欢迎各位一起在群里探讨技术. 一. ...

  6. Itext生成pdf文件,itext+Freemarker生成pdf,(中文空白解决)

    来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...

  7. java生成pdf字体居中,Java生成pdf文件,解决中文乱码问题

    如下代码使用itext生成pdf文件,通过设置中文字体避免乱码. /** * AsianTest.java */ import java.io.FileOutputStream; import jav ...

  8. Java按照固定pdf模板生成pdf文件——itext

    第一步:生成pdf模板 先用word写好固定格式的文档,再利用wps将文档转成pdf文件,然后利用Adobe Acrobat DC(此工具是收费的,可以百度搜索进行破解)进行表单域编辑(如下图),编辑 ...

  9. java 生成pdf文件,添加图片

    所需要的包: 1.  iText    下载地址:  http://nchc.dl.sourceforge.net/sourceforge/itext/itext-2.0.3.jar 2.  iTex ...

最新文章

  1. 好的视频编解码网址和博文地址
  2. python语言及其应用-python语言及其应用
  3. linux中rpm命令管理
  4. Gitlab怎样添加组、创建用户、创建项目与推送代码
  5. oracle 去除重复的信息
  6. 四十一、Vue项目上手 | 用户管理系统 实现用户修改和删除功能(完成篇)
  7. mysql事务操作_mysql的事务操作
  8. android inputmethodmanager 不自动弹出,Android中软键盘InputMethodManager的弹出和隐藏,以及显示和隐藏的监听...
  9. Maven项目POM文件错误,提示“Plugin execution not covered by lifecycle configuration”的解决方案...
  10. 【iOS之轮播视图、自定义UIPageControl】
  11. 23. 从上往下打印二叉树[层序遍历](C++版本)
  12. fid和is_【GAN】用于生成图像的评价指标——IS和FID
  13. SAT写作例子之Frank Lloyd Wright
  14. 随机森林和多元线性回归R语言实现代码
  15. 计算机组成原理重点考试范围,计算机组成原理重点整理(白中英版) 考试必备
  16. 天然产物来源的新型除草剂研究取得进展
  17. 【高性价比】初学者入门吉他值得推荐购买的民谣单板吉他品牌—VEAZEN费森吉他
  18. AutoCAD .NET 二次开发实例:批量文本查找替换
  19. Motorola和Borland结成联盟,以帮助开发者为Motorola下一代无线电话创建应用程序 (转)...
  20. 程序员分界线:30岁,30岁转行、35岁转行、35岁被迫裁员

热门文章

  1. 貝塞爾 Layer 入門指南
  2. 节点重要度 matlab,复杂网络节点重要度常用指标及其计算
  3. kf 和 ekf 和 ukf 入门
  4. mysql查询优化-查询缓存
  5. 如何正确的看待人工智能?只有编程基础的人可以学吗?
  6. Python编程IDE的选择
  7. BigDecimal源码分析。
  8. 前端推荐的学习资源网站
  9. [整理]svn commit obstructed
  10. 地理空间数据共享资源大汇总