以下的方法只是一个模板,有些功能并没有完全覆盖。可根据自己的需求查询其他资料。

1.引入jar包或者pom依赖

        <dependency>  <groupId>com.itextpdf</groupId>  <artifactId>itextpdf</artifactId>  <version>5.5.10</version>  </dependency><dependency>  <groupId>com.itextpdf</groupId>  <artifactId>itext-asian</artifactId>  <version>5.2.0</version>  </dependency>

2.工具类 PDFCommon.java

package com.pdf.common;import java.text.SimpleDateFormat;
import java.util.Date;import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;public class PDFCommon {// 生成空表格public static PdfPCell getEmptyCell() throws Exception {PdfPCell pdfPCell = getBodyPdfPCell_center();pdfPCell.setPhrase(new Paragraph("  ", getPdfBodyFont()));return pdfPCell;}// 设置表体的表格样式 字体居右public static PdfPCell getBodyPdfPCell_right() {PdfPCell pdfPCell = new PdfPCell();pdfPCell.setMinimumHeight(12);pdfPCell.setHorizontalAlignment(Element.ALIGN_RIGHT);pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);return pdfPCell;}// 设置表体的表格样式 字体居左public static PdfPCell getBodyPdfPCell_left() {PdfPCell pdfPCell = new PdfPCell();pdfPCell.setMinimumHeight(12);pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);return pdfPCell;}public static PdfPCell getBodyPdfPCell_image() {PdfPCell pdfPCell = new PdfPCell();pdfPCell.setMinimumHeight(40);pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);return pdfPCell;}// 设置表体的表格样式 字体居左public static PdfPCell getBodyPdfPCell_left_top() {PdfPCell pdfPCell = new PdfPCell();pdfPCell.setMinimumHeight(12);pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);pdfPCell.setVerticalAlignment(Element.ALIGN_TOP);return pdfPCell;}// 设置表体的表格样式 字体居中public static PdfPCell getBodyPdfPCell_center() {PdfPCell pdfPCell = new PdfPCell();pdfPCell.setMinimumHeight(12);pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);return pdfPCell;}// 设置表头public static Font getPdfTitleFont() throws Exception {BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, 14F, Font.NORMAL);fontChinese.setStyle("bold");return fontChinese;}// 设置表体public static Font getPdfBodyFont() throws Exception {BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, 10F, Font.NORMAL);return fontChinese;}// 设置表体-字体加粗public static Font getPdfBodyFont_bold() throws Exception {BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, 8.5F, Font.NORMAL);fontChinese.setStyle("bold");return fontChinese;}// 设置下划线 -> 内容下方public static Chunk setUnderlineBlank(String content, float thickness, float yPosition) throws Exception {Chunk chunk = new Chunk(content, PDFCommon.getPdfBodyFont());chunk.setUnderline(thickness, yPosition);return chunk;}}

3.导出的实现方法

package com.pdf;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.pdf.common.PDFCommon;public class ExportPDF {public void export(HttpServletResponse response, HttpServletRequest request) throws Exception {// 创建文件Document document = new Document();document.setPageSize(PageSize.A4); // 设置A4// 导出到浏览器// response.reset();// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");// String pdfFileName = "导出pdf-" + PDFCommon.getYYYYMMDD(nowDate);//文件的名称// response.setHeader("Content-Disposition",// "attachment;filename=" + URLEncoder.encode(pdfFileName, "UTF-8") + ".pdf");// // 为了解决在IE浏览器中汉字乱码的问题// response.setHeader("Last-Modified", new// SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));//// PdfWriter writer = PdfWriter.getInstance(document,// response.getOutputStream());// 导出到本地PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File("D:/导出pdf.pdf")));// 设置页面布局writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);// 打开文件document.open();float[] widths = { 125, 125, 125, 170, 125, 160, 125 };// 有几个表格PdfPTable table = new PdfPTable(widths);table.setWidthPercentage(95); // 设置表格占A4纸的95%// 设置表头PdfPCell total_1 = new PdfPCell();total_1.setRowspan(1);total_1.setColspan(10);total_1.setHorizontalAlignment(Element.ALIGN_CENTER);total_1.setVerticalAlignment(Element.ALIGN_MIDDLE);total_1.disableBorderSide(13);//消除周围的表格线// total_1.setBackgroundColor(BaseColor.YELLOW);Paragraph paragraph = new Paragraph("导出pdf的表格 \r\n", PDFCommon.getPdfTitleFont());total_1.setPhrase(paragraph);table.addCell(total_1);// 第1行PdfPCell total_1_1 = PDFCommon.getBodyPdfPCell_center();total_1_1.setRowspan(1);total_1_1.setColspan(1);total_1_1.setPhrase(new Paragraph("人员姓名", PDFCommon.getPdfBodyFont()));table.addCell(total_1_1);PdfPCell total_1_2 = PDFCommon.getBodyPdfPCell_center();total_1_2.setRowspan(1);total_1_2.setColspan(1);total_1_2.setPhrase(new Paragraph("张三", PDFCommon.getPdfBodyFont()));table.addCell(total_1_2);PdfPCell total_1_3 = PDFCommon.getBodyPdfPCell_center();total_1_3.setRowspan(1);total_1_3.setColspan(1);total_1_3.setPhrase(new Paragraph("身份证号码", PDFCommon.getPdfBodyFont()));table.addCell(total_1_3);PdfPCell total_1_4 = PDFCommon.getBodyPdfPCell_center();total_1_4.setRowspan(1);total_1_4.setColspan(1);total_1_4.setPhrase(new Paragraph("100002100110121211", PDFCommon.getPdfBodyFont()));table.addCell(total_1_4);PdfPCell total_1_5 = PDFCommon.getBodyPdfPCell_center();total_1_5.setRowspan(1);total_1_5.setColspan(1);total_1_5.setPhrase(new Paragraph("户籍地址", PDFCommon.getPdfBodyFont()));table.addCell(total_1_5);PdfPCell total_1_67 = PDFCommon.getBodyPdfPCell_center();total_1_67.setRowspan(1);total_1_67.setColspan(2);total_1_67.setPhrase(new Paragraph("北京市金融街", PDFCommon.getPdfBodyFont()));table.addCell(total_1_67);for (int q = 0; q < 8; q++) {// 画8个大表格PdfPCell total_Q = PDFCommon.getBodyPdfPCell_center();total_Q.setRowspan(3);// 每一个大表格里有三个小表格total_Q.setColspan(2);total_Q.setPhrase(new Paragraph("大表格" + (q + 1), PDFCommon.getPdfBodyFont()));table.addCell(total_Q);for (int c = 0; c < 3; c++) {PdfPCell total_Q_d = PDFCommon.getBodyPdfPCell_left();total_Q_d.setRowspan(1);total_Q_d.setColspan(4);total_Q_d.setPhrase(new Paragraph("小表格" + (c + 1), PDFCommon.getPdfBodyFont()));table.addCell(total_Q_d);if (c == 0) {//第一列进行合并PdfPCell total_Q_score = PDFCommon.getBodyPdfPCell_center();total_Q_score.setRowspan(3);total_Q_score.setColspan(1);Chunk sigUnderline = new Chunk("  " + 5 + "  ");sigUnderline.setUnderline(0.1f, -2f);// 设置下划线Paragraph pp = new Paragraph("", PDFCommon.getPdfBodyFont());pp.add(sigUnderline);pp.add("分");total_Q_score.setPhrase(pp);table.addCell(total_Q_score);}}}// 总分PdfPCell total_ht = PDFCommon.getBodyPdfPCell_center();total_ht.setRowspan(1);total_ht.setColspan(6);total_ht.setPhrase(new Paragraph("合计", PDFCommon.getPdfBodyFont()));total_ht.setMinimumHeight(16F);table.addCell(total_ht);PdfPCell total_h = PDFCommon.getBodyPdfPCell_center();total_h.setRowspan(1);total_h.setColspan(1);Chunk sigUnderline = new Chunk("  " + 100 + "  ");sigUnderline.setUnderline(0.1f, -2f);// 设置下划线Paragraph pp = new Paragraph("", PDFCommon.getPdfBodyFont());pp.add(sigUnderline);pp.add("分");total_h.setPhrase(pp);table.addCell(total_h);// 大专家意见PdfPCell total_kb = PDFCommon.getBodyPdfPCell_center();total_kb.setRowspan(1);total_kb.setColspan(7);total_kb.disableBorderSide(2);total_kb.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));table.addCell(total_kb);PdfPCell total_zj = PDFCommon.getBodyPdfPCell_left_top();total_zj.setRowspan(3);total_zj.setColspan(2);total_zj.disableBorderSide(9);total_zj.setPhrase(new Paragraph(" 大专家:", PDFCommon.getPdfBodyFont()));table.addCell(total_zj);PdfPCell total_zj_1 = PDFCommon.getBodyPdfPCell_left();total_zj_1.setRowspan(1);total_zj_1.setColspan(5);total_zj_1.disableBorderSide(7);total_zj_1.setPhrase(new Paragraph("  ", PDFCommon.getPdfBodyFont()));table.addCell(total_zj_1);// 专家签字图片Image zjImage = loadingPicture();PdfPCell imCell = PDFCommon.getBodyPdfPCell_image();imCell.setPhrase(new Paragraph(new Chunk(zjImage, 0, 0)));imCell.setHorizontalAlignment(Element.ALIGN_LEFT);imCell.setRowspan(1);imCell.setColspan(5);imCell.disableBorderSide(7);table.addCell(imCell);// 专家评估日期PdfPCell total_zjdate = PDFCommon.getBodyPdfPCell_right();total_zjdate.setRowspan(1);total_zjdate.setColspan(5);total_zjdate.disableBorderSide(5);total_zjdate.setPhrase(new Paragraph("第一个时间:   " + PDFCommon.getYYYYMMDD_f(new Date()), PDFCommon.getPdfBodyFont()));table.addCell(total_zjdate);// 集体评审PdfPCell total_jtyj_1 = PDFCommon.getBodyPdfPCell_left();total_jtyj_1.setRowspan(1);total_jtyj_1.setColspan(7);total_jtyj_1.disableBorderSide(2);total_jtyj_1.setPhrase(new Paragraph("小专家意见:   ", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_1);PdfPCell total_jtyj_2 = PDFCommon.getBodyPdfPCell_left();total_jtyj_2.setRowspan(1);total_jtyj_2.setColspan(7);total_jtyj_2.disableBorderSide(3);;total_jtyj_2.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_2);PdfPCell total_jtyj_3_1 = PDFCommon.getBodyPdfPCell_left();total_jtyj_3_1.setRowspan(1);total_jtyj_3_1.setColspan(2);total_jtyj_3_1.disableBorderSide(11);total_jtyj_3_1.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_3_1);PdfPCell total_jtyj_3_2 = PDFCommon.getBodyPdfPCell_left();total_jtyj_3_2.setRowspan(1);total_jtyj_3_2.setColspan(2);total_jtyj_3_2.disableBorderSide(15);total_jtyj_3_2.setPhrase(new Paragraph("通过  [ √ ]", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_3_2);PdfPCell total_jtyj_3_3 = PDFCommon.getBodyPdfPCell_left();total_jtyj_3_3.setRowspan(1);total_jtyj_3_3.setColspan(3);total_jtyj_3_3.disableBorderSide(7);total_jtyj_3_3.setPhrase(new Paragraph("不通过  [  ]", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_3_3);// 补充意见 头PdfPCell total_jtyj_4_1 = PDFCommon.getBodyPdfPCell_left();total_jtyj_4_1.setRowspan(1);total_jtyj_4_1.setColspan(7);total_jtyj_4_1.disableBorderSide(3);total_jtyj_4_1.setPhrase(new Paragraph("补充意见:", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_4_1);// 补充意见 体PdfPCell total_jtyj_5_1 = PDFCommon.getBodyPdfPCell_left();total_jtyj_5_1.setRowspan(1);total_jtyj_5_1.setColspan(7);total_jtyj_5_1.disableBorderSide(3);total_jtyj_5_1.setPhrase(new Paragraph(new Paragraph("             专家的意见是通过! \r\n \r\n ", PDFCommon.getPdfBodyFont())));table.addCell(total_jtyj_5_1);// 专家PdfPCell total_jtyj_6_1 = PDFCommon.getBodyPdfPCell_left();total_jtyj_6_1.setRowspan(1);total_jtyj_6_1.setColspan(2);total_jtyj_6_1.disableBorderSide(11);total_jtyj_6_1.setPhrase(new Paragraph("专家1: ", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_6_1);// 专家PdfPCell total_jtyj_6_2 = PDFCommon.getBodyPdfPCell_left();total_jtyj_6_2.setRowspan(1);total_jtyj_6_2.setColspan(2);total_jtyj_6_2.disableBorderSide(15);total_jtyj_6_2.setPhrase(new Paragraph("    专家2:", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_6_2);// 专家PdfPCell total_jtyj_6_3 = PDFCommon.getBodyPdfPCell_left();total_jtyj_6_3.setRowspan(1);total_jtyj_6_3.setColspan(3);total_jtyj_6_3.disableBorderSide(7);total_jtyj_6_3.setPhrase(new Paragraph("专家3:", PDFCommon.getPdfBodyFont()));table.addCell(total_jtyj_6_3);// 图片1Image image_1 = loadingPicture();PdfPCell imCell_1 = PDFCommon.getBodyPdfPCell_image();imCell_1.setRowspan(1);imCell_1.setColspan(2);imCell_1.setPhrase(new Paragraph(new Chunk(image_1, 0, 0)));imCell_1.disableBorderSide(11);table.addCell(imCell_1);// 图片2Image image_2 = loadingPicture();PdfPCell imCell_2 = PDFCommon.getBodyPdfPCell_image();imCell_2.setRowspan(1);imCell_2.setColspan(2);imCell_2.setPhrase(new Paragraph(new Chunk(image_2, 0, 0)));imCell_2.disableBorderSide(15);table.addCell(imCell_2);// 图片3Image image_3 = loadingPicture();PdfPCell imCell_3 = PDFCommon.getBodyPdfPCell_image();imCell_3.setRowspan(1);imCell_3.setColspan(3);imCell_3.setPhrase(new Paragraph(new Chunk(image_3, 0, 0)));imCell_3.disableBorderSide(7);table.addCell(imCell_3);// 专家评估日期PdfPCell total_jtdate = PDFCommon.getBodyPdfPCell_right();total_jtdate.setRowspan(1);total_jtdate.setColspan(7);total_jtdate.disableBorderSide(1);total_jtdate.setPhrase(new Paragraph("第二个时间:   " + PDFCommon.getYYYYMMDD_f(new Date()), PDFCommon.getPdfBodyFont()));table.addCell(total_jtdate);document.add(table);document.close();}// 查询图片组装imageprivate Image loadingPicture() throws BadElementException, MalformedURLException, IOException {File file = new File("E:/aa.png");byte[] by = File2byte(file);Image image = Image.getInstance(by);image.scaleAbsolute(60, 20);// 调整图片大小(宽度 高度)return image;}private byte[] File2byte(File tradeFile) {byte[] buffer = null;try {FileInputStream fis = new FileInputStream(tradeFile);ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] b = new byte[1024];int n;while ((n = fis.read(b)) != -1) {bos.write(b, 0, n);}fis.close();bos.close();buffer = bos.toByteArray();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return buffer;}}

4.测试方法

   public static void main(String[] args) {ExportPDF exportPDF = new ExportPDF();try {exportPDF.export(null, null);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}

5.导出的效果

java生成pdf表格并插入图片相关推荐

  1. 记录 java 往 word 表格里边插入图片 的坑

    坑了两次,还是记录下 免得下次又忘记.java 在往word 表格里边插入图片的时候,如果循环插入多个表格,第一个表格里边的图片正常显示,但是第二个表格里边的图片总是展示为上一个表格的最后一张图片,经 ...

  2. java 生成PDF表格,添加页码,加盖水印(包括图片水印和文字水印)

    最近公司要求后端实现PDF导出功能,并且还要要求能够加盖水印.网上搜寻了一下,大多帖子比较老旧,并且随着开发包的版本更新,都不能用,所以有了这篇博客. 生成PDF所用到的jar包主要是itext,现在 ...

  3. java生成pdf表格_java在pdf中生成表格的方法

    1.目标 在pdf中生成一个可变表头的表格,并向其中填充数据.通过泛型动态的生成表头,通过反射动态获取实体类(我这里是User)的get方法动态获得数据,从而达到动态生成表格. 每天生成一个文件夹存储 ...

  4. java生成pdf表格示例代码

    使用itext代码直接生成pdf文件 用到jar包       itextpdf-5.2.1.jar   itext-asian-5.2.0.jar 优缺点 优点:生成快,代码直接能用 缺点:代码要写 ...

  5. Java生成PDF表格

    PdfBox读取PDF加载pdf文件出错 下载相关Jar包(pdfbox和fontbox为主) 网址http://pdfbox.apache.org/download.cgi 准备pdf格式文件(代码 ...

  6. java生成PDF(图片,模板,表格)

    刚接到了一个需求,生成一个pdf,一开始以为挺简单的,通过模板生成嘛,我也发过相应的文章,根据模板直接生成pdf,响应到前端或者根据模板生成pdf,直接指定下载位置,这两种方案都可以,不过这篇文章主要 ...

  7. Java—将数据生成pdf表格

    由于时间问题,所以粗略的封装了一个生成pdf表格的工具包(不喜欢讲废话,直接上代码!!!) package com.sgcc.dlsc.jibei.commons.utils;import com.i ...

  8. 向pdf文件中插入图片及文字 java实现

    向pdf文件中插入图片及文字 引入itextpdf相关依赖 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> ...

  9. Java生成PDF文档(表格)

    Java生成PDF文档(表格) package org.jeecg.modules.esi.utils;import com.itextpdf.text.*; import com.itextpdf. ...

  10. # Java 生成pdf文件

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

最新文章

  1. Forefront_TMG_2010-TMG建立站点间***
  2. 不要在桌面保存长期修改的文件否则系统挂了时候面临文件丢失的问题,长期总结面试资料(公司 题目 地址 氛围 加班情况 薪资情况)毁于一旦
  3. 华为荣耀30pro鸿蒙内测版,荣耀手机用户放心了 消息称荣耀30 Pro正在内测华为鸿蒙OS...
  4. php 远程图片大小,PHP下载远程图片并保存到本地方法总结
  5. sql 生成一列1到10的数字_SQL 打印矩阵(三)
  6. win7语言文件夹c盘什么位置,Win7系统C盘中ProgramData文件夹在哪?
  7. JAVA 类和对象的实例
  8. Eclipse启动问题:An error is occurred
  9. 笔记本java稳定wifi信号_笔记本wifi网速不稳定的解决方法
  10. Node+puppeteer学习笔记 (二)--环境搭建Win、MAC、Linux环境,以及使用Sublime Text3运行
  11. vue-pdf插件不翻页预览
  12. 【胡侃系列】基于多元回归模型的双十一购物狂欢节天猫商城销售额预测
  13. 电子招投标采购系统之电子招标投标的全流程!企业电子招投标系统源码
  14. 网络安全知识竞赛题库及答案(多选题1-100题)
  15. linux服务器集群群发邮件,爱博邮件群发服务器(Linux版本)
  16. C++背包问题——完全背包必须装满的方案数
  17. HTML+CSS写个人简历
  18. 英国物理学家冷嘲中微子超光速发现
  19. WinRAR - 分卷压缩
  20. 最新计算机系统smart图形,SmartArt逆天了,排得了版拼得了图

热门文章

  1. [Android] 小男孩短视频去水印新版来了,支持18多个短视频平台去水印例如抖音快手微视皮皮虾等...
  2. 目标追踪(一)环境搭建
  3. WPF另类实现摄像头录像并预览
  4. 绕过tp路由器管理密码_普联(TPLink)路由器管理员密码是什么?
  5. Python处理Excel数据分组
  6. IDA遇到mojava,crash的情况
  7. Latex常用数学公式整理——导数
  8. golang 将数据导入excel
  9. 开博第一天,在日本做开发的日子(生活-吃货篇)
  10. 在日本做开发的日子(序,生活篇的吃货,穿与住)