• itextpdf7自写算法的表格展示 制表符


    注意:1.itextpdf的版本为7.0.5(7.1.4的版本过高,用本示例会报错(比如7.0.5是这个包是com.itextpdf.kernel.color.Color,在7.1.4会升级为com.itextpdf.kernel.colors.Color);  7.0.0的版本低,  也会报错。)

    2.jdk要在1.7及以上

    3.maven版本也不要太低了(推荐3.3)

    4.本文根据官方例子(https://itextpdf.com/en/resources/examples/itext-7/page-events-watermarking)改编,支持文字中文

    自写算法达到表格的效果代码

    package com.test;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import com.itextpdf.kernel.font.PdfFont;
    import com.itextpdf.kernel.font.PdfFontFactory;
    import com.itextpdf.kernel.geom.PageSize;
    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.itextpdf.layout.Document;
    import com.itextpdf.layout.Style;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.layout.element.Tab;
    import com.itextpdf.layout.element.TabStop;
    import com.itextpdf.layout.property.TabAlignment;
    
    /**
     * pdf 段落添加制表符
     * @author 鲲鹏展翅
     * @date 2019年1月22日 下午3:08:14
     * @desc 
     */
    public class Pdf{
        public static final String DEST = "E:\bbb\段落添加制表符.pdf";
        public static PdfFont watermark = null;
        
        public static void main(String[] args) throws Exception {
            watermark = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);//解决中文不显示
            // itext7需要jdk7及以上 jdk6就用itext5
            File dest = new File(DEST);
            dest.getParentFile().mkdirs();
            createPdf(DEST);
        }
        
        
        public static OutputStream createFile(String fileName) {
            File file = new File(fileName);
            // 创建一个文件之前判断他的父路径的文件夹是否存在,不存在需要创建
            if (!new File(file.getParent()).exists())
                new File(file.getParent()).mkdirs();
            try {
                // 创建该文件夹
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            // 获取所要创建pdf的文件的输出流
            OutputStream os = null;
            try {
                os = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return os;
        }
        
        public static void createPdf(String dest) throws Exception {
            // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
            PdfWriter pdfWriter = new PdfWriter(createFile(DEST));
            // 创建pdfDocument的对象与Writer的对象关联
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            // 设定创建pdf的纸张大小
            PageSize pageSize = PageSize.A4;
            // 创建Document的对象
            Document document = new Document(pdfDocument, pageSize);
            float countWidth = pageSize.getWidth();
            float l = document.getLeftMargin();
            float r = document.getRightMargin();
            float w = pageSize.getWidth() - document.getLeftMargin() - document.getRightMargin();
            
            java.util.List<TabStop> tabstops = new ArrayList();
            tabstops.add(new TabStop(countWidth - l, TabAlignment.LEFT));
            tabstops.add(new TabStop(w/3+l, TabAlignment.LEFT));
            tabstops.add(new TabStop(countWidth - r, TabAlignment.RIGHT));
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops).addStyle(new Style().setBold().setFont(watermark).setFontSize(10.5f));
            p.add("报告编号: 20190122").add(new Tab());
            p.add("查询时间: 2019.01.22 09:38:46").add(new Tab());
            p.add("报告时间: 2019.01.22 13:09:26");
            p.setMarginBottom(0);
            document.add(p);
            
            java.util.List<TabStop> tabstops2 = new ArrayList();
            tabstops2.add(new TabStop(countWidth - l, TabAlignment.LEFT));
            tabstops2.add(new TabStop(w/4+l, TabAlignment.LEFT));
            tabstops2.add(new TabStop(w/4*2+l, TabAlignment.LEFT));
            tabstops2.add(new TabStop(countWidth - r, TabAlignment.RIGHT));
            
            Paragraph p2 = new Paragraph();
            p2.addTabStops(tabstops2).addStyle(new Style().setBold().setFont(watermark).setFontSize(10.5f));
            p2.add("姓名: 鲲鹏展翅").add(new Tab());
            p2.add("证件类型: 身份证" ).add(new Tab());
            p2.add("证件号码: **************1234").add(new Tab());
            p2.add("xx").add(new Tab());
            p2.setMarginBottom(0);
            document.add(p2);
    
            document.flush();
            // 关闭document
            document.close();
        }
    }

    效果

    第一行是分为三份 ,其最后一个是居右对齐的

    第一行是分为四份 ,其最后一个是居右对齐的

  • 相关阅读:
    060821流水账
    060721流水账
    060421流水账
    [Tips] 更新oh my zsh
    [Tips] updraftplus备份wordpress
    [Tips] SSH免密登陆
    [Notes] 基于阿里云的SSL在容器化wordpress中部署https服务
    [Tips] wordpress添加文章计数
    [Notes] 容器化部署wordpress
    [Notes] pandas 保存hdf5时numpy array遇到的性能warning
  • 原文地址:https://www.cnblogs.com/zjk1/p/10305245.html
Copyright © 2020-2023  润新知