iText7根据html表格(table)代码生成表格、解决跨行跨列问题
来源:互联网 发布:网络集成商有哪些 编辑:程序博客网 时间:2024/06/18 12:37
由于要解析html代码,所以我还用了一个第三方的插件jsoup,可以自己百度一下,或者点击这里下载API,免费的。。
html表格的代码是没有格式的,我就不贴了,下面直接给代码
1.效果图
2.源码
- 1.TestTable .java (执行方法)
package test;import org.jsoup.Jsoup;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;import com.gsafety.plan.module.util.PDFUtil;import com.itextpdf.kernel.pdf.PdfDocument;import com.itextpdf.layout.Document;public class TestTable { public static String url="G:\\test4.pdf"; public static void main(String[] args) throws Exception { long old = System.currentTimeMillis(); PDFUtil pdfUtil=new PDFUtil(url);//传入url创建pdf Document doc=pdfUtil.createPdfDoc(); PdfDocument pdfDoc=doc.getPdfDocument(); pdfUtil.addTitle(doc,"表格"); //html代码 String pg="<table><thead><tr><td style='width:78.2000pt;'><p style='text-align:center;'><strong><span style='font-family:'Times New Roman';font-size:10.5pt;'>预警分级</span></strong></p></td><td colspan='4' style='width:336.8500pt;'><p style='text-align:center;'><strong><span style='font-family:'Times New Roman';font-size:10.5pt;'>4<span style='font-family:宋体;'>个等级标准划分</span></span></strong></p></td></tr></thead><tbody><tr><td style='width:78.2000pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>预警级别</span></p></td><td style='width:81.1500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>一级</span></p></td><td style='width:78.1500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>二级</span></p></td><td style='width:88.7500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>三级</span></p></td><td style='width:88.8000pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>四级</span></p></td></tr><tr><td style='width:78.2000pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>级别描述</span></p></td><td style='width:81.1500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>特别严重</span></p></td><td style='width:78.1500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>严重</span></p></td><td style='width:88.7500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>较重</span></p></td><td style='width:88.8000pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>一般</span></p></td></tr><tr><td style='width:78.2000pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>颜色标示</span></p></td><td style='width:81.1500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>红</span></p></td><td style='width:78.1500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>橙</span></p></td><td style='width:88.7500pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>黄</span></p></td><td style='width:88.8000pt;'><p style='text-align:center;'><span style='font-family:'Times New Roman';font-size:10.5000pt;'>蓝</span></p></td></tr></tbody></table>"; //----------这段是使用jsoup-----begin------ org.jsoup.nodes.Document docParse= Jsoup.parseBodyFragment(pg);//格式化html Element body = docParse.body();//放入body片段 //System.out.println(body.ownText()); Elements allTag=body.children();//获得儿子标签列表 for(Element e: allTag){ if("table".equals(e.nodeName())){ System.out.println("找到table了"); pdfUtil.addTable(doc, e);//使用工具类的方法 } } //----------这段是使用jsoup-----end------ pdfDoc.close(); doc.close(); long now = System.currentTimeMillis(); System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:"+url); } }
- PDFUtil.java (iText我自己写的工具类)
package com.gsafety.plan.module.util;import java.io.IOException;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;import org.apache.struts2.ServletActionContext;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;import com.itextpdf.io.font.PdfEncodings;import com.itextpdf.io.image.ImageData;import com.itextpdf.io.image.ImageDataFactory;import com.itextpdf.kernel.events.IEventHandler;import com.itextpdf.kernel.events.PdfDocumentEvent;import com.itextpdf.kernel.font.PdfFont;import com.itextpdf.kernel.font.PdfFontFactory;import com.itextpdf.kernel.pdf.PdfDocument;import com.itextpdf.kernel.pdf.PdfWriter;import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;import com.itextpdf.layout.Document;import com.itextpdf.layout.element.AreaBreak;import com.itextpdf.layout.element.Cell;import com.itextpdf.layout.element.Image;import com.itextpdf.layout.element.Paragraph;import com.itextpdf.layout.element.Table;import com.itextpdf.layout.property.TextAlignment;/** * * * * * @author Hui * */public class PDFUtil { public static String DEST2 = "";//文件路径 public static PdfFont sysFont =null; private Map<String, Integer> catalogMap = new LinkedHashMap<String, Integer>();//<标题,页数> public PDFUtil(String url){ try { //sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);//中文设置 sysFont = PdfFontFactory.createFont("c://windows//fonts//simsun.ttc,1",PdfEncodings.IDENTITY_H, false);//中文设置,解决特殊字符错误 DEST2=url; } catch (IOException e) { e.printStackTrace(); } } /** * 创建pdf doc * @return doc * */ public Document createPdfDoc() throws Exception{ PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST2)); Document doc = new Document(pdfDoc);//构建文档对象 //设置页码 //TextFooterEventHandler eh= new TextFooterEventHandler(doc); //pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE,eh); return doc; } /** * 添加大标题 * @param * * */ public void addTitle(Document doc,String title) throws Exception{ Paragraph paragraph = new Paragraph(); paragraph.add(title).setFont(sysFont).setBold().setFontSize(20).setTextAlignment(TextAlignment.CENTER); doc.add(paragraph); } /** * 添加一级标题 * @param * * */ public void addHeading1(Document doc,String pg) throws Exception{ Paragraph paragraph = new Paragraph(); paragraph.add(pg).setFont(sysFont).setBold().setFontSize(16); doc.add(paragraph); catalogMap.put(pg, doc.getPdfDocument().getNumberOfPages()); } /** * 添加二级标题 * @param * * */ public void addHeading2(Document doc,String pg) throws Exception{ Paragraph paragraph = new Paragraph(); paragraph.add(pg).setFont(sysFont).setBold().setFontSize(14).setFirstLineIndent(14); doc.add(paragraph); catalogMap.put(pg, doc.getPdfDocument().getNumberOfPages()); } /** * 添加三级标题 * @param * * */ public void addHeading3(Document doc,String pg) throws Exception{ Paragraph paragraph = new Paragraph(); paragraph.add(pg).setFont(sysFont).setBold().setFontSize(12); doc.add(paragraph); catalogMap.put(pg, doc.getPdfDocument().getNumberOfPages()); } /** * 添加段落 * @param * */ public void addParagraph(Document doc,String pg) throws Exception{ Paragraph paragraph = new Paragraph(); paragraph.add(pg).setFont(sysFont).setFirstLineIndent(20);//中文字体,首行缩进 doc.add(paragraph); } /** * 添加图片 * @param * */ public void addImg(Document doc,String url) throws Exception{ String realUrl=ServletActionContext.getServletContext().getRealPath("/img")+"/"+url; ImageData imgData=ImageDataFactory.create(realUrl); Image pic=new Image(imgData); doc.add(pic); } /** * 添加表格 * @param * @param Element e * */ public void addTable(Document doc,Element e) throws Exception{ Elements eTag=e.children();//获得thead和tobody Table table=new Table(e.children().last().children().last().childNodeSize());//tobody最后一列的tr获得行数,这里可能会有些出入 for(Element tBady: eTag){ Elements tCellList=tBady.children();//获得tr行 for(Element tCell: tCellList){ Elements tDList=tCell.children();//获得td格子 for(Element td:tDList){ String oldRowspan=td.attr("rowspan");//获得跨行 String oldColspan=td.attr("colspan");//获得跨列 int rowspan=0; int colspan=0; if(oldRowspan!="" && oldRowspan.length()>0){ rowspan=Integer.valueOf(td.attr("rowspan")); } if(oldColspan!="" && oldColspan.length()>0){ colspan=Integer.valueOf(td.attr("colspan")); } Cell cell=new Cell(rowspan,colspan).add(td.text()); table.addCell(cell); } } } doc.add(table.setFont(sysFont).setAutoLayout()); } public Map<String, Integer> getCatalogMap() { return catalogMap; } public void setCatalogMap(Map<String, Integer> catalogMap) { this.catalogMap = catalogMap; }}
阅读全文
0 0
- iText7根据html表格(table)代码生成表格、解决跨行跨列问题
- html生成表格table
- Proc_BuildTableHtml(根据SQL生成表格形式的html)
- html 表格table
- html table表格标签
- html 不规则table表格
- html --table表格
- HTML 表格table
- 解决table表格溢出错乱的问题
- 后台生成table表格
- 解决 HTML 转到 XHTML 中,表格(Table)不能居中的问题。align="center" 过时问题
- html表格(table)样式说明
- html的笔记(table表格)
- js通过后台配置动态生成html table表格
- 轻松解决HTML + CSS各种表格问题
- table 表格宽度问题
- python生成html表格
- python生成html表格
- Java 7 新的 try-with-resources 语句,自动资源释放
- SQLAlchemy中ResultProxy的正确使用
- Windows+tomcat+Nginx集群
- C++代码风格养成之路(一)
- intellij 对 Spring MVC + tomcat 7简单配置
- iText7根据html表格(table)代码生成表格、解决跨行跨列问题
- Redis学习——Redis持久化之RDB备份方式保存数据
- 学习Linux命令(32)
- Turing Tree(线段树区间不同数字)
- LeetCode 100. Same Tree
- 损失函数减肥用,神经网络调权重(深度学习入门系列之六)
- acm_step1.1.6 1.1.7 1.1.8
- h264编码速度质量参数设置
- 使用maven构建多模块项目(二)