zoukankan      html  css  js  c++  java
  • 创建pdf 表格

    创建pdf 表格
        pom.xml添加Jar依赖
            <!-- pdf处理jar依赖 start -->
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itextpdf</artifactId>
                <version>5.5.12</version>
            </dependency>
            <!-- pdf处理jar依赖 end -->

            <!-- pdf处理jar包依赖 start -->
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-asian</artifactId>
                <version>5.2.0</version>
            </dependency>
            <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext</artifactId>
                <version>4.2.1</version>
            </dependency>
            <!-- pdf处理jar包依赖 end -->        
            
            
    package com.bjpowernode.p2p.admin.pdf;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.List;
    import org.apache.log4j.Logger;
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    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;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfWriter;

    /**
     * 创建pdf表格
     *
     * @author ldd
     *
     */
    public class CreatePdfTable {
        
        private static final Logger logger = Logger.getLogger(CreatePdfTable.class);
        
        /**
         * 创建PDF表格文件
         *
         * @param outPdfTableFile 输出表头文件路径
         * @param tableHeaderList 头部标题
         * @param tableDataList 数据List
         * @throws DocumentException
         * @throws IOException
         * @throws Exception
         */
        public static void createPdfTable (String outPdfTableFile, List<String> tableHeaderList, List<String> tableDataList) {
            
            //创建一个文档对象纸张大小为A4
            Document doc = new Document();
            PdfWriter writer = null;
            try {
                //设置要输出到磁盘上的文件名称
                writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(outPdfTableFile)));
                //设置作者信息
                doc.addAuthor("");
                //设置文档创建日期
                doc.addCreationDate();
                //设置标题
                doc.addTitle("");
                //设置值主题
                doc.addSubject("");
                //打开文档开始写内容
                doc.open();
                //使用资源字体(ClassPath)simkai.ttf 字体:可以从win 中字体中cp
                String simkaiPath = CreatePdfTable.class.getResource("simkai.ttf").getPath().replaceAll("%20", " ");
                BaseFont baseFont = BaseFont.createFont(simkaiPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font chineseFont = new Font(baseFont, 12, Font.NORMAL, BaseColor.BLACK);
                
                //创建一个四列的表格
                PdfPTable table = new PdfPTable (tableHeaderList.size());
                //设置表格具体宽度
                table.setTotalWidth(100f);
                //设置每一列所占的长度
                table.setWidths(new float[] {28f, 18f, 20f, 17f});
                //设置行头
                Paragraph titleP = new Paragraph("甲方(出借人):", chineseFont);
                //设置居中
                titleP.setAlignment(Paragraph.ALIGN_CENTER);
                //设置字体 +12
                titleP.setLeading(chineseFont.getSize() + 12);
                //设置单元格
                PdfPCell titleCell = new PdfPCell(titleP);
                titleCell.setBorder(0);//设置边框
                titleCell.setColspan(4);//标段名称 占一行 (甲方(出借人):)扩展4列
                table.addCell(titleCell);

                //创建表头
                for (int i=0; i<tableHeaderList.size(); i++) {
                    PdfPCell cell = new PdfPCell();
                    Paragraph paragraph = new Paragraph(tableHeaderList.get(i), chineseFont);
                    paragraph.setAlignment(Element.ALIGN_CENTER);//设置该段落为居中显示
                    cell.setPhrase(paragraph);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    table.addCell(cell);
                    //添加此代码后每页都会显示表头
                    //table.endHeaders();
                }
                //创建表格数据
                for (int i=0; i<tableDataList.size(); i++) {
                    //设置编号单元格
                    PdfPCell cell = new PdfPCell();
                    Paragraph para = new Paragraph(tableDataList.get(i), chineseFont);
                    para.setAlignment(Element.ALIGN_CENTER);//设置该段落为居中显示
                    cell.setPhrase(para);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    table.addCell(cell);
                }
                //将表格添加到新的文档
                doc.add(table);
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (doc.isOpen()) {
                    doc.close();
                }
                if (null != writer) {
                    writer.close();
                }
                logger.info("创建数据量:" + tableDataList.size());
            }
        }
    }        
           

  • 相关阅读:
    C#逻辑运算符
    C#:采用TuesPechkin生成Pdf
    C# 发布时出现:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
    C# winform在窗体中动态添加按钮
    C# winform滚动字幕效果
    C# Winform操作注册表实现窗体的定位记忆功能
    C#提取类型的所有方法和参数
    .NET FCL(框架类库)名称空间说明
    .NET框架通用语言运行时(CLR)的执行模型
    坚持学习,高效率才能更有水平
  • 原文地址:https://www.cnblogs.com/liudongdong666666/p/7811581.html
Copyright © 2011-2022 走看看