itextpdf生成表格pdf+背景图片(可转图片)

  • 导入jar,manven引用
    <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><!-- pdf转图片 --><dependency><groupId>org.icepdf.os</groupId><artifactId>icepdf-core</artifactId><version>6.1.2</version></dependency>
  • 定义全局变量和创建表格方法
 // 定义全局的字体静态变量private static Font keyfont;private static Font textfont;// 最大宽度private static int maxWidth = 580;// 静态代码块static {try {// 不同字体(这里定义为同一种字体:包含不同字号、不同style)BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 15为字体,可根据实际情况调整keyfont = new Font(bfChinese, 15, Font.BOLD, BaseColor.YELLOW);textfont = new Font(bfChinese, 15, Font.NORMAL, BaseColor.YELLOW);} catch (Exception e) {e.printStackTrace();}}/*** 创建指定列宽、列数的表格** @param widths* @return*/public 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;}/*** 创建单元格(指定字体)** @param value* @param font* @return*/public 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));cell.setBorderWidthTop(1f);// 设置列表线颜色cell.setBorderColorTop(BaseColor.YELLOW);cell.setBorderColorRight(BaseColor.YELLOW);cell.setBorderColorBottom(BaseColor.YELLOW);cell.setBorderColorLeft(BaseColor.YELLOW);return cell;}/*** 创建单元格(指定字体、水平..)** @param value* @param font* @param align* @return*/public 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));cell.setBorderWidthTop(1f);// 设置列表线颜色cell.setBorderColorTop(BaseColor.YELLOW);cell.setBorderColorRight(BaseColor.YELLOW);cell.setBorderColorBottom(BaseColor.YELLOW);cell.setBorderColorLeft(BaseColor.YELLOW);return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并)** @param value* @param font* @param align* @param colspan* @return*/public 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));cell.setBorderWidthTop(1f);// 设置列表线颜色cell.setBorderColorTop(BaseColor.YELLOW);cell.setBorderColorRight(BaseColor.YELLOW);cell.setBorderColorBottom(BaseColor.YELLOW);cell.setBorderColorLeft(BaseColor.YELLOW);return cell;}
  • 生成图片方法
public String pdf2Pic(String pdfPath, String path, String fileName) throws Exception {
// pdfPath:pdf路径  path:保存图片路径org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();document.setFile(pdfPath);//缩放比例float scale = 2.5f;String imgName = null;//旋转角度float rotation = 0f;// for循环为pdf多页情况时使用
//        for (int i = 0; i < document.getNumberOfPages(); i++) {
// 0为i,多页使用,注意BufferedImage image = (BufferedImage)document.getPageImage(0, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);RenderedImage rendImage = image;try {File fileDir = new File(path);if (!fileDir.exists()) {fileDir.mkdirs();}imgName = fileName + 0 + ".jpg";File file = new File(path + imgName);ImageIO.write(rendImage, "jpg", file);} catch (IOException e) {e.printStackTrace();}image.flush();
//        }document.dispose();// 此为返回的保存图片路径~~return path + imgName;~~ }
  • 生成带表格的pdf
 try {// 1.新建document对象Document document = new Document(PageSize.A4);// 建立一个Document对象// 2.建立一个书写器(Writer)与document对象关联Long longTime = System.currentTimeMillis();String url = "路径";// 保存的pdf名称String name = longTime + ".pdf";File file = new File(url + name);file.createNewFile();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
//        // 设置页面布局writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);// 3.打开文档document.open();Date date = new Date();Calendar calendar = Calendar.getInstance();calendar.setTime(date);int weekYear = calendar.getWeekYear();int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);calendar.add(Calendar.DAY_OF_MONTH, +4);// 表格,150, 120, 120表格列比例PdfPTable table = createTable(new float[]{150, 120, 120});// 表格列名以及表头// pdfPCell 为三列合并PdfPCell pdfPCell = createCell(weekYear+"年"+(date.getMonth() + 1) + "月第" + weekOfMonth + "周签约奖", keyfont, Element.ALIGN_CENTER, 3);pdfPCell.setFixedHeight(40);PdfPCell pdfPCell1 = createCell("xxx", keyfont, Element.ALIGN_CENTER);pdfPCell1.setFixedHeight(40);PdfPCell pdfPCell2 = createCell("xxxx", keyfont, Element.ALIGN_CENTER);pdfPCell2.setFixedHeight(40);PdfPCell pdfPCell3 = createCell("奖金/元", keyfont, Element.ALIGN_CENTER);pdfPCell3.setFixedHeight(40);table.addCell(pdfPCell);table.addCell(pdfPCell1);table.addCell(pdfPCell2);table.addCell(pdfPCell3);// list为数据源for (int i = 0; i < list.size(); i++) {PdfPCell pdfPCell4 = createCell(list.get(i).get("deptName").toString(), textfont);// FixedHeight列高pdfPCell4.setFixedHeight(40);table.addCell(pdfPCell4);PdfPCell pdfPCell5 = createCell(list.get(i).get("userName").toString(), textfont);// FixedHeight列高pdfPCell5.setFixedHeight(40);table.addCell(pdfPCell5);PdfPCell pdfPCell6 = createCell(list.get(i).get("jj").toString(), textfont);// FixedHeight列高pdfPCell6.setFixedHeight(40);table.addCell(pdfPCell6);}PdfPCell pdfPCell7 = createCell("祝贺以上同事荣登英雄榜,奖金于" + (calendar.get(Calendar.MONTH) + 1) + "月"+ calendar.get(Calendar.DAY_OF_MONTH) + "日发放,请注意查收!", keyfont, Element.ALIGN_CENTER, 3);pdfPCell7.setFixedHeight(20);table.addCell(pdfPCell7);// 查找图片并插入Image tImgCover = Image.getInstance(url+"图片名称");/* 设置图片的位置 */tImgCover.setAbsolutePosition(0, 0);/* 设置图片的大小,A4不需要改动 */tImgCover.scaleAbsolute(595, 842);// 插入图片document.add(tImgCover);PdfContentByte canvas = writer.getDirectContent();// 设置表格宽度table.setTotalWidth(500f);/*** rowStart         0   起始行* rowEnd           -1  表示全部行* xPos             表格横坐标- 从左向右开始计算* yPos             表格纵坐标- 从下向上开始计算* canvas           画布*/table.writeSelectedRows(0, -1, 50, 500, canvas);// 关闭document对象document.close();String pngNmae = longTime + "_";// 调用生成图片方法imgName =图片路径String imgName = pdf2Pic(url + name, url, pngNmae);} catch (Exception e) {e.printStackTrace();}
  • 此代码可直接使用,亲测无问题


itextpdf 生成表格pdf+背景图片(可转图片)相关推荐

  1. java使用itext生成表格pdf文件

    以下主要讲解的是java使用itext生成表格pdf文件,话不多说,直接上代码 一.首先引入itext所使用的包 <dependency><groupId>com.itextp ...

  2. 【Java中级篇】使用itextpdf生成PDF

    我们可以发现很多求职网站都会将我们录入的信息来生成一个PDF简历文件.所以我这里提供了用itextpdf生成的PDF的代码. 一.步骤 1.1.使用Adobe Acrobat Pro工具编辑PDF模板 ...

  3. itextpdf添加表格元素_itext生成pdf文件-表格

    生成pdf常用的插件有iReport.和itext,这里将使用itext生成pdf文件. 多于的话不说直接上demo和需要的jar,如果pdf中有图片要画的话可以用jfreeChart画. packa ...

  4. Java 生成各种 PDF 实战方案(图片、模板、表格)

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

  5. java使用itextpdf生成pdf并填充自定义数据

    项目中有个需求,对于已有的数据生成对应的发票pdf或者合同pdf,这些pdf具有一些特性,就是pdf有固定的格式,类似于表格,我们只要往表格里面填充数据即可.当然,也会涉及到签章,二维码等需求. 总体 ...

  6. 使用itextpdf生成pdf

    因为工作需要,最近项目中有一个需求需要生成带有项目信息的pdf,并且pdf中还需要有附带项目信息的二维码方便用户扫码.         做这个功能中踩了不少坑,写个博客提醒一下 springboot版 ...

  7. Python数据展示 - 生成表格图片

    前言 本文来讲讲如何用Python生成表格图片. 选一个合适库 Python最大的优点就是第三方库丰富,基本你要什么功能,都能找到别人实现好的库,几行代码一调用就完事了. Pytable 最先找到的是 ...

  8. 利用.ftl模板生成word之后再转pdf发现 填充的印章图片显示不全,没有自动移到下一页处理办法

    因为公司业务需要,利用一个ftl模板生成word在转为pdf.然后发现印章图片在页底那边显示不全,没有如文字一般自动换到下一页去.例如如下: 最后折腾了半天,在网上试了各种办法.最后只能通过 java ...

  9. .net core 使用 iTextSharp 导入 Adobe Acrobat Pro 创建的PDF模板及生成表格,然后导出PDF

    因为是第一次接触这种东西,对于我这个工作经验不到一年的新人程序员来说,头已经快爆炸了,在查询了"巨多"的资料后,终于给弄出来了 = = ,代码应该不是很好看,能跑就行( 创建PDF ...

最新文章

  1. 计算机视觉顶会上的灌水文都有哪些特征?
  2. linux——NFS+AUTOFS服务的基本配置
  3. couchbase_Spring Data Couchbase 1.0 GA发布
  4. 定时器和promise_手写Promise核心原理,再也不怕面试官问我Promise原理
  5. Spring 整合junit
  6. 交换两个数不是三个数。
  7. JS 创建对象方法
  8. 13.PHP核心技术与最佳实践 --- Hash 算法与数据库实现
  9. ListView的item监听事件,并且把值传递给另一个activity
  10. python自定义函数如何命名_Python 自定义类之函数和运算符重载
  11. iTextSharp显示中文
  12. 苹果7pnfc功能门禁卡_苹果7plus怎么打开nfc这个功能
  13. AndroidStudio实现按钮按下时状态改变以及选择器属性及基本用法
  14. Google Chrome 浏览器——隐藏功能及快捷键
  15. 淘宝商品点赞可以增加权重吗?
  16. 由xubuntu桌面系统恢复到ubuntu桌面系统
  17. HTML入门笔记12-HTML中备注写法
  18. Wireshark分析IP数据报
  19. PYthon将图片合成视频方法一:OpenCV
  20. Vue鼠标移入(悬停)显示-移出隐藏

热门文章

  1. 考勤管理系统(考勤员函数与管理员部分相同)
  2. EasyDL入驻社区果蔬店 离线识别秒级智能结算
  3. [状压dp] 最短Hamilton路径(模板题+状压dp)
  4. 苹果手机开启关闭信号数字
  5. 苹果WWDC:2016大会前瞻,好想去啊,只能网上看直播了
  6. 无线蓝牙耳机排行榜_2021公认性价比最高蓝牙耳机品牌
  7. HNOI2018寻宝游戏
  8. 在数字世界复刻现实:与5G同频共振的XR远景
  9. Android 【真机】与【模拟器】触摸屏事件的模拟差异分析
  10. HTML 页面跳转时传递参数(jquery.params.js)