当前位置: 代码网 > it编程>编程语言>Java > Java使用itextpdf实现表单导出为pdf

Java使用itextpdf实现表单导出为pdf

2025年06月26日 Java 我要评论
下文将简述如何通过itextpdf 导出form表单,涉及的内容有字体设置、创建表格、表格样式设置、安全性设置、表头设置、增加一行包括内容、增加多行、合并列、增加超链接1.引入依赖 <

下文将简述如何通过itextpdf 导出form表单,涉及的内容有字体设置、创建表格、表格样式设置、安全性设置、表头设置、增加一行包括内容、增加多行、合并列、增加超链接

1.引入依赖

       <dependency>
            <groupid>com.itextpdf</groupid>
            <artifactid>itextpdf</artifactid>
            <version>5.5.13.2</version>
        </dependency>

2.字体设置

对于导出的内容包含中文需要设置字体,windows系统自带的中文字体文件一般在c:/windows/fonts/下,不使用中文字体将显示空白

//获取基础字体
    public static basefont getbasefont(string path){
        if(basefont!=null){
            return basefont;
        }
        basefont basefont=null;
        try {
            basefont= basefont.createfont(path,
                    basefont.identity_h,
                    basefont.embedded);

        }catch (exception e){
            e.printstacktrace();
        }
        return basefont;
    }
    /**
     * 设置中文字体大小
     * @param path 字体路径
     * @param fontsize 字体大小
     * @param style 样式
     * @return
     */
    public static font getchinesefont(string path,int fontsize,int style){
        font chinesefont = null;
        try {
            basefont basefont = getbasefont(path);
            chinesefont = new font(basefont, fontsize);
            chinesefont.setstyle(style);
        }catch (exception e){
            e.printstacktrace();
        }
        return chinesefont;
    }

3.创建表格

  /**
     * 获取一个table
     * @param tablecolnum 列数
     * @param widthpercentage 宽度显示百分比
     * @param spacingbefore 设置前间距
     * @param spacingafter 设置后间距
     * @param columnwidths 设置表格列宽,格式{2f,2f},注意集合数量与列数相同,按顺序定义列宽
     * @return
     */
    public static pdfptable gettable(integer tablecolnum,float widthpercentage,float spacingbefore,float spacingafter, float[] columnwidths){
        if(tablecolnum==null||tablecolnum==0){
            return null;
        }
        pdfptable table = new pdfptable(tablecolnum); //创建一个tablecolnum的表格
        table.setwidthpercentage(widthpercentage);//设置百分比
        table.setspacingbefore(spacingbefore);//设置前间距
        table.setspacingafter(spacingafter);//设置后间距
        // 设置表格列宽
        try {
            table.setwidths(columnwidths);
        } catch (documentexception e) {
            throw new runtimeexception(e);
        }
        return table;
    }

4.设置表格无边框

需要创建完所有行后,再设置为无边框

    /**
     * 设置表格为无边框
     * @param table
     */
    public static void setnonebordertable(pdfptable table){
        for (pdfprow row : table.getrows()) {
            for (pdfpcell cell : row.getcells()) {
                if (cell != null) {
                    cell.setborder(rectangle.no_border);
                }
            }
       

5.安全性设置

    /**
     * 设置文档的安全性
     * @param document
     * @param pdfpath
     * @return
     */
    public static pdfwriter  getsecuritywriter( document document,string pdfpath,string ownerpass)  {
        pdfwriter instance = null;
        try {
            instance = pdfwriter.getinstance(document, new fileoutputstream(pdfpath));
            if(stringutils.isempty(ownerpass)){
                ownerpass="ssc-logistic";
            }
            instance.setencryption(
                    null,               // 用户密码(空表示无需密码)
                    ownerpass.getbytes(), // 所有者密码,修改时需要该密码
                    pdfwriter.allow_printing |//允许打印
                            pdfwriter.allow_copy |//允许复制
                            pdfwriter.allow_screenreaders,//允许屏幕阅读器访问
                    pdfwriter.encryption_aes_256//aes 256加密
            );
        } catch (exception e) {
            throw new runtimeexception(e);
        }
        return instance;

    }

6.设置表头

 /**
     * 设置表头
     * @param table
     * @param headermap 按照put顺序
     * @param fontpath 从配置文件帐获取
     * @param language 语言
     */
    public  static void addtableheader(pdfptable table, linkedhashmap<string,string> headermap, string language,string fontpath){
        font chinesefont=getchinesefont(fontpath);
        if(headermap!=null){
            for(string key:headermap.keyset()){
                pdfpcell headercell = new pdfpcell(new phrase(headermap.get(key), chinesefont));
                headercell.setbackgroundcolor(basecolor.light_gray);//背景颜色
                headercell.sethorizontalalignment(element.align_center);//水平对齐方式
                headercell.setverticalalignment(element.align_middle);//垂直对齐方式
                headercell.setfixedheight(40f);//行高
                table.addcell(headercell);
            }
        }
    }

7.增加一行内容

  /**
     * 增加表格内容行
     * @param table
     * @param rowmap 格式为k-v如k:field1 v:test
     * @param language
     */
    public static void addtablerole(pdfptable table,linkedhashmap<string,string> rowmap,string fontpath){
        font chinesefont = getchinesefont(fontpath);
        font font = new font(font.fontfamily.courier, 10);
        if(rowmap!=null){
            for(string key:rowmap.keyset()){
                string fieldvalue=rowmap.get(key);
                pdfpcell cell = new pdfpcell(new phrase(fieldvalue,chinesefont));
                cell.setverticalalignment(element.align_middle);
                cell.sethorizontalalignment(element.align_left);
                cell.setfixedheight(30f);//行高
                table.addcell(cell);
            }
        }
    }

8.增加多行内容

  /**
     * 增肌多列内容
     * @param table
     * @param rowsmaplist 多行的list
     * @param language 语言
     * @param fontpath 字体路径
     */
    public static void addtableroles(pdfptable table,list<linkedhashmap<string,string>> rowsmaplist,string language,string fontpath){
        font chinesefont = getchinesefont(fontpath);
        font font = new font(font.fontfamily.courier, 10);
        if(!collectionutils.isempty(rowsmaplist)){
            for(linkedhashmap<string,string> rowmap:rowsmaplist){
                for(string key:rowmap.keyset()){
                    string fieldvalue=rowmap.get(key);
                    pdfpcell cell=null;
                    if("zh-cn".equals(language)){
                        cell = new pdfpcell(new phrase(fieldvalue,chinesefont));
                    }else{
                        cell = new pdfpcell(new phrase(fieldvalue,font));
                    }
                    cell.setverticalalignment(element.align_middle);
                    cell.sethorizontalalignment(element.align_left);
                    cell.setfixedheight(30f);//行高
                    table.addcell(cell);
                }
            }

        }
    }

9.合并列

   /**
     * 增加一个合并列
     * @param table
     * @param colsnum 合并几列
     * @param startkey 从valuemap 那个key开始
     * @param valuemap filed1:fieldvalue1,field2:fieldvalue2
     */
    public static void addcolspanrow(pdfptable table,int colsnum,string startkey,linkedhashmap<string,string> valuemap){
        if(!valuemap.isempty()){
            for(string key:valuemap.keyset()){
                string fieldvalue = valuemap.get(key);
                pdfpcell cell = new pdfpcell(new phrase(fieldvalue,chinesefont));
                cell.setverticalalignment(element.align_middle);
                cell.sethorizontalalignment(element.align_left);
                if(startkey.equals(key)){
                    cell.setcolspan(colsnum); // 设置跨列
                }
                cell.setfixedheight(30f);//行高
                table.addcell(cell);
            }
        }
    }

10.增加超链接

   /**
     * 增加一个超链接的单元格
     * @param table
     * @param fieldfixedvalue 固定单元格内容
     * @param linkedlist list<map<string,string>> 格式,包含linkname、url,分别表示显示的名称和超链接地址
     */
    public static void addlinkcell(pdfptable table, string fieldfixedvalue,int colspannum,string fontpath, list<map<string,string>> linkedlist){
        basefont basefont = getbasefont(fontpath);
        font chinesefont = getchinesefont(fontpath);
        font linkfont = new font(basefont, 10, font.underline, new basecolor(0,0,255));
        paragraph mixpara = new paragraph();
        mixpara.add(new chunk(fieldfixedvalue, chinesefont));  // 普通中文
        //设置超链接
        int size=0;
        if(!collectionutils.isempty(linkedlist)){
            for(map<string,string> linkedmap:linkedlist){
                string linkname = linkedmap.get("linkname");
                string url=linkedmap.get("url");
                if(!stringutils.isempty(linkname)&&!stringutils.isempty(url)){
                    if(size!=0&&size!=linkedlist.size()){//增加分割线
                        chunk sep = new chunk(" | ", new font(font.fontfamily.helvetica, 12));
                        mixpara.add(sep);
                    }
                    chunk linkchunk = new chunk(linkname, linkfont);
                    linkchunk.setaction(new pdfaction(url));
                    mixpara.add(linkchunk);
                }
                size++;
            }
        }
        pdfpcell cell = new pdfpcell(mixpara);
        cell.setborder(rectangle.no_border);//无边框
        cell.setcolspan(colspannum);//合并单元格
        cell.setpaddingtop(10f);//上间距
        cell.setfixedheight(30f);//行高
        table.addcell(cell);
    }

根据以上方法可以实现一个简单的form表单导出成pdf,并实现超链接可跳转或下载、文档密码设置、可编辑修改复制权限。具体代码以具体业务为主,仅供参考。

到此这篇关于java使用itextpdf实现表单导出为pdf的文章就介绍到这了,更多相关java itextpdf导出表单为pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com