项目中需要导出生成的报告为PDF,原来的版本是前端截图生成PDF,当时会产生内容因为分页被截断的问题,因此换成使用itext根据前端传回的数据来生成PDF文件。

     <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

所需的itextPDF依赖

https://www.coderanch.com/how-to/javadoc/itext-2.1.7/allclasses-noframe.html

对应的API文档
先创建相应的工具类

// 定义全局的字体静态变量private static Font titlefont;private static Font headfont;private static Font keyfont;private static Font textfont;// 最大宽度private static int maxWidth = 520;// 静态代码块static {try {// 不同字体(这里定义为同一种字体:包含不同字号、不同style)BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);titlefont = new Font(bfChinese, 16, Font.BOLD);headfont = new Font(bfChinese, 14, Font.BOLD);keyfont = new Font(bfChinese, 10, Font.BOLD);textfont = new Font(bfChinese, 10, Font.NORMAL);} catch (Exception e) {e.printStackTrace();}}/**------------------------创建表格单元格的方法start----------------------------*//*** 创建单元格(指定字体)* @param value* @param font* @return*/public static PdfPCell createCell(String value, Font font) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setPhrase(new Phrase(value, font));return cell;}/*** 创建单元格(指定字体、水平..)* @param value* @param font* @param align* @return*/public static PdfPCell createCell(String value, Font font, int align) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value, font));return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并)* @param value* @param font* @param align* @param colspan* @return*/public static PdfPCell createCell(String value, Font font, int align, int colspan) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)* @param value* @param font* @param align* @param colspan* @param boderFlag* @return*/public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));cell.setPadding(3.0f);if (!boderFlag) {cell.setBorder(0);cell.setPaddingTop(15.0f);cell.setPaddingBottom(8.0f);} else if (boderFlag) {cell.setBorder(0);cell.setPaddingTop(0.0f);cell.setPaddingBottom(15.0f);}return cell;}/*** 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距)* @param value* @param font* @param align* @param borderWidth* @param paddingSize* @param flag* @return*/public static PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value, font));cell.setBorderWidthLeft(borderWidth[0]);cell.setBorderWidthRight(borderWidth[1]);cell.setBorderWidthTop(borderWidth[2]);cell.setBorderWidthBottom(borderWidth[3]);cell.setPaddingTop(paddingSize[0]);cell.setPaddingBottom(paddingSize[1]);if (flag) {cell.setColspan(2);}return cell;}/**------------------------创建表格单元格的方法end----------------------------*//**--------------------------创建表格的方法start------------------- ---------*//*** 创建默认列宽,指定列数、水平(居中、右、左)的表格* @param colNumber* @param align* @return*/public static PdfPTable createTable(int colNumber, int align) {PdfPTable table = new PdfPTable(colNumber);try {table.setTotalWidth(maxWidth);table.setLockedWidth(true);table.setHorizontalAlignment(align);table.getDefaultCell().setBorder(1);} catch (Exception e) {e.printStackTrace();}return table;}/*** 创建指定列宽、列数的表格* @param widths* @return*/public static PdfPTable createTable(float[] widths) {PdfPTable table = new PdfPTable(widths);try {table.setTotalWidth(maxWidth);table.setLockedWidth(true);table.setHorizontalAlignment(Element.ALIGN_CENTER);table.getDefaultCell().setBorder(1);} catch (Exception e) {e.printStackTrace();}return table;}/*** 创建空白的表格* @return*/public static PdfPTable createBlankTable() {PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(0);table.addCell(createCell("", keyfont));table.setSpacingAfter(20.0f);table.setSpacingBefore(20.0f);return table;}/**--------------------------创建表格的方法end------------------- ---------*/

开始创建PDF文件

     // 路径String PDFPath = "E:" + File.separator + "分析报告.pdf";Document document = new Document(PageSize.A4);// 建立一个Document对象

创建文件并打开,开始进行操作

         file.createNewFile();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));// 打开文档document.open();document.addTitle(store.getStoreName());// 设置文档标题

创建需要使用的文体

            //设置字体 路径导向为本地的字体文件String fontPath = "C:/WINDOWS/Fonts/STSONG.TTF";String blackFontPath = "C:/WINDOWS/Fonts/simhei.ttf";String numberPath = "C:/WINDOWS/Fonts/calibri.ttf";BaseFont bfChinese = BaseFont.createFont( fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);BaseFont bfBlackChinese = BaseFont.createFont( blackFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);BaseFont bfNumber = BaseFont.createFont( numberPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font titleFont = new Font(bfChinese);Font font = new Font(bfChinese);Font blackFont = new Font(bfBlackChinese);Font lowTitleFont = new Font(bfChinese);Font numberFont = new Font(bfNumber);

设置字体样式(详细见API)

         blackFont.setSize(16);//设置字体大小blackFont.setStyle(Font.BOLD);//设置字体加粗

接下来开始正式拼接PDF文档
需要用到的几个重要的对象
Chunk:块(Chunk)是能被添加到文档的文本的最小单位。
Paragraph:段落是一系列块或短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。


Paragraph paragraph = new Paragraph();//创建段落
paragraph.setKeepTogether(true);//使段落内容尽可能在同一页中(可能失效)
paragraph.setAlignment(Element.ALIGN_CENTER);//设置段落中文字对齐格式Chunk titleChunk = new Chunk("项目基本信息", blackFont);//创建新的块并设置文字信息与字体
paragraph.add(chunk);//把块加入到段落中
paragraph.add(Chunk.NEWLINE);//加入新的一行,相当于换行
paragraph.add(new Chunk(new LineSeparator()));//加入一条实线
document.add(paragraph);//把段落加入到文档中

Image图片对象

         Image image = new Imageimage = Image.getInstance(mapImgPath);//参数为图片的路径//设置居中image.setAlignment(Image.ALIGN_CENTER);//设置宽高image.scaleAbsolute(385, 247);//去除边框image.disableBorderSide(15);//插入地图图片document.add(image);

Table创建

         //使用工具类编辑表格PdfPTable table = CreatePDFUtil.createTable(new float[]{120, 160, 120, 80});//设置表头table.addCell(CreatePDFUtil.createCell("所属部门", titleFont)).setFixedHeight(50);//设置单元格高度table.addCell(CreatePDFUtil.createCell("类型", titleFont));table.addCell(CreatePDFUtil.createCell("面积", titleFont));table.addCell(CreatePDFUtil.createCell("是否通过", titleFont));//设置表格内容table.addCell(CreatePDFUtil.createCell("A", font)).setFixedHeight(50);table.addCell(CreatePDFUtil.createCell("B", font)).setFixedHeight(50);table.addCell(CreatePDFUtil.createCell("C", font)).setFixedHeight(50);table.addCell(CreatePDFUtil.createCell("true", font)).setFixedHeight(50);document.add(table);//添加到文档中

操作完成进行关闭操作

         PdfContentByte cb = writer.getDirectContent();cb.fill();cb.sanityCheck();document.close();

java后端使用itextPDF生成PDF文件相关推荐

  1. java用itextPDF生成PDF文件保存至本地并上传至ftp服务器

    标题java用itextPDF生成PDF文件保存至本地并上传至ftp服务器 所需jar :itext-asian-5.2.0.jar,itextpdf-5.5.5.jar,commons-net-3. ...

  2. java在linux生成pdf文件,从 Java 应用程序动态生成 PDF 文件

    简介: 如果您的应用程序需要动态生成 PDF 文档,那么您需要 iText 库.开源的 iText 库使得 PDF 的创建变得轻松易行.本文介绍了 iText 并提供了一个使用它从 Java 技术应用 ...

  3. Java使用itextpdf生成PDF文件,用浏览器下载

    浏览器下载生成PDF文件 1.引入jar包 <dependency><groupId>com.itextpdf</groupId><artifactId> ...

  4. Java使用iText5.0生成PDF文件

    前言 近段时间做开发,遇到生成PDF文件的需求,在此做一个总结: iText的5.0版本较2.0版本变化比较大,比如:颜色,页脚,字体,去掉table对象,文字位置 等等: 公司用的2.0,但我这里想 ...

  5. java 字符串转pdf文件_java中根据模板生成pdf文件

    原标题:java中根据模板生成pdf文件 阅读目录 简介 业务需求 引入jar包 pdf模板文件与方法参数 代码部分 总结归纳 回到顶部 简介 本文使用java引入apach提供的pdf操作工具生成p ...

  6. java 生成字体文件,java使用itext生成pdf文件-设置字体,itextpdf,import com.l

    java使用itext生成pdf文件-设置字体,itextpdf,import com.limport com.lowagie.text.Document;import com.lowagie.tex ...

  7. java使用itextpdf生成PDF批量打印荣誉证书(指定位置输出文字)

    最近公司项目有个需求,批量打印荣誉证书,一开始尝试过传统的网络打印,控件打印,JS调用浏览器打印方法,遇到各种问题,比如定位不准,分页问题,​​缩放问题等.然后就自己研究,整理了一套打印方案,项目已测 ...

  8. java 制作pdf模板,Java-pdf模板制作流程-使用pdf 模板生成pdf文件

    Java 使用pdf 模板生成pdf文件 --制作流程 1.      使用工具 adobe acrobat dc.word 2015 2.      使用 word 繪制一個 3*5 的表格並保存, ...

  9. 自动生成PDF文件(Java通过PDF模板自动生成PDF)

    思路: 1.创建PDF模板(先创建对应的excel文件,创建好后另存为PDF文件)即可. 2.使用Adobe Acrobat DC工具打开PDF文件,设置自己想要替换的内容. 3.maven项目引入依 ...

  10. Java生成PDF文件(Itext篇)

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

最新文章

  1. html请求接口_前端工程师吐后端工程师(第八讲)——接口的开发
  2. Jsch ssh登陆
  3. GridView 控件的执行顺序
  4. 四元数姿态解算及多传感器融合详细解析
  5. IO多路复用 select、poll、epoll
  6. Mybatis 的Log4j日志输出问题 - 以及有关日志的所有问题
  7. Codeforces Round #498 (Div. 3) F. Xor-Paths
  8. 数据仓库和数据集市 专业术语解释
  9. win2008 查询 tcp连接失败_TCP详解(转)
  10. TensorFlow可以“预装”数据集了,新功能Datasets出炉
  11. 1000米感知能力?!图森无人车说这是他们的最新突破
  12. zencart产品页面调用WordPress最新文章
  13. 每日一题 导数的应用
  14. vscode开发中绝对让你惊艳的插件!!!(个人在用) 持续更新。。。。
  15. nvcc: command not found
  16. 历史性一刻,中国航天器首次登上火星!!!
  17. IFS Applications架构
  18. Vue笔记(适合后端人员开发的快速入门)
  19. 如何从容迎接人工智能时代的到来?让我们听听专家的解读
  20. Gpsd pps移植

热门文章

  1. 为什么 BI 软件都搞不定关联分析?带你分析分析
  2. ngix 全局配置文件和子配置文件 配置项中文注释
  3. 十大气势背景音乐(适合战队,广告招商会场用)
  4. 双线性插值(超级易懂的)
  5. use mysql命令_mysql命令-use
  6. 9.计蒜客ACM题库.A1602 结果填空:开关灯
  7. 微信发红包的测试点有哪些? 评论/点赞/分享/收藏/收索/上传/下载
  8. 数据类产品设计和实现思路
  9. 获取VS2012离线语言包
  10. 打印预览和实际的打印不一致问题