iText7根据html表格(table)代码生成表格、解决跨行跨列问题

由于要解析html代码,所以我还用了一个第三方的插件jsoup,可以自己百度一下,或者点击这里下载API,免费的。。
html表格的代码是没有格式的,我就不贴了,下面直接给代码

1.效果图

iText7根据html表格(table)代码生成表格、解决跨行跨列问题_第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 catalogMap = new LinkedHashMap();//<标题,页数>

    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 getCatalogMap() {
        return catalogMap;
    }

    public void setCatalogMap(Map catalogMap) {
        this.catalogMap = catalogMap;
    }




}

你可能感兴趣的:(个人,iText)