Java利用iTextPDF库实现制作PDF表格模板并填充数据

 更新时间:2023年12月05日 10:18:24   作者:一休哥助手  
这篇文章主要为大家详细介绍了如何通过Java的iTextPDF库制作一个PDF表格模板并填充数据,文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编一起学习一下吧

要使用Java的iTextPDF库制作一个PDF表格模板并填充数据,你需要遵循以下步骤:

添加依赖:首先,确保你的项目中包含了iTextPDF库的依赖。如果你使用Maven,可以在你的pom.xml文件中添加以下依赖:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.11</version>
</dependency>

创建PDF模板:你可以使用iTextPDF创建一个简单的PDF模板,或者使用其他工具(比如Adobe Acrobat)创建PDF模板,并在模板中添加表格。

填充表格数据:使用iTextPDF API向PDF模板中的表格填充数据。

下面是一个简单的例子,演示如何使用PDFBox创建一个包含表格的PDF文档,并向表格中填充数据:

import com.google.common.collect.Lists;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class ITextPDFExample {

    public static void main(String[] args) {
        // 保存的pdf全路径
        String outPdfPath = "/path/out.pdf";
        // pdf中表格需要填充的数据
        List<List<String>> data = Lists.newArrayList();
        data.add(Lists.newArrayList("列1值", "列2值", "列3值", "列4值", "列5值"));
        //创建文件
        Document document = new Document(PageSize.A4);
        File file = new File(outPdfPath);
        try {
            PdfWriter.getInstance(document, new FileOutputStream(file));
            //打开文件
            document.open();
            //BaseFont-确认支持中文
            String fontPath = "/path/to/your/chinese/font.ttf";
            // 创建BaseFont对象,指定字体路径和编码
            BaseFont bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            Font fontChineseTitle = new Font(bfChinese, 12, Font.NORMAL);
            String contentTitle = "标题";
            Paragraph paragraphTitle = new Paragraph(contentTitle, fontChineseTitle);
            paragraphTitle.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraphTitle);

            Paragraph blankRow1 = new Paragraph(18f, " ", fontChineseTitle);
            document.add(blankRow1);

            Font fontChineseParagraph = new Font(bfChinese, 12, Font.NORMAL);

            String contentParagraph2 = "文本内容1";
            Paragraph paragraph2 = new Paragraph(contentParagraph2, fontChineseParagraph);
            paragraph2.setFirstLineIndent(28);
            document.add(paragraph2);

            paragraphTitle.setSpacingAfter(100);
            paragraphTitle.setSpacingBefore(100);


            Paragraph blankRow2 = new Paragraph(18f, " ", fontChineseParagraph);
            document.add(blankRow2);

            Font fontChineseTable = new Font(bfChinese, 12, Font.NORMAL);

            PdfPTable table = new PdfPTable(5);

            List<PdfPRow> listRow = table.getRows();
            //设置列宽
            float[] columnWidths = {10, 27, 24, 16, 23};
            table.setWidths(columnWidths);

            //行1
            PdfPCell[] cells1 = new PdfPCell[5];
            PdfPRow row1 = new PdfPRow(cells1);

            //单元格
            cells1[0] = new PdfPCell(new Paragraph("列1", fontChineseTable));
            cells1[0].setBorderColor(BaseColor.BLACK);
            cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

            cells1[1] = new PdfPCell(new Paragraph("列2", fontChineseTable));
            cells1[1].setBorderColor(BaseColor.BLACK);
            cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);

            cells1[2] = new PdfPCell(new Paragraph("列3", fontChineseTable));
            cells1[2].setBorderColor(BaseColor.BLACK);
            cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);

            cells1[3] = new PdfPCell(new Paragraph("列4", fontChineseTable));
            cells1[3].setBorderColor(BaseColor.BLACK);
            cells1[3].setVerticalAlignment(Element.ALIGN_MIDDLE);

            cells1[4] = new PdfPCell(new Paragraph("列5", fontChineseTable));
            cells1[4].setBorderColor(BaseColor.BLACK);
            cells1[4].setVerticalAlignment(Element.ALIGN_MIDDLE);

            //把第一行添加到集合
            listRow.add(row1);

            for (int i = 0; i < data.size(); i++) {
                PdfPCell[] cellsi = new PdfPCell[5];
                PdfPRow rowi = new PdfPRow(cellsi);
                cellsi[0] = new PdfPCell(new Paragraph(data.get(i).get(0), fontChineseParagraph));
                cellsi[0].setBorderColor(BaseColor.BLACK);
                cellsi[0].setVerticalAlignment(Element.ALIGN_MIDDLE);

                cellsi[1] = new PdfPCell(new Paragraph(data.get(i).get(1), fontChineseParagraph));
                cellsi[1].setBorderColor(BaseColor.BLACK);
                cellsi[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
                cellsi[2] = new PdfPCell(new Paragraph(data.get(i).get(2), fontChineseParagraph));
                cells1[2].setBorderColor(BaseColor.BLACK);
                cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);

                cellsi[3] = new PdfPCell(new Paragraph(data.get(i).get(3), fontChineseParagraph));
                cellsi[3].setBorderColor(BaseColor.BLACK);
                cellsi[3].setVerticalAlignment(Element.ALIGN_MIDDLE);

                cellsi[4] = new PdfPCell(new Paragraph(data.get(i).get(4), fontChineseParagraph));
                cellsi[4].setBorderColor(BaseColor.BLACK);
                cellsi[4].setVerticalAlignment(Element.ALIGN_MIDDLE);

                listRow.add(rowi);
            }

            //把表格添加到文件中
            document.add(table);


            Paragraph blankRow3 = new Paragraph(18f, " ", fontChineseParagraph);
            document.add(blankRow3);

            String contentParagraph4 = "文本内容2";
            Paragraph paragraph4 = new Paragraph(contentParagraph4, fontChineseParagraph);
            paragraph4.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph4);

            String contentParagraph5 = "文本内容3";
            ;
            Paragraph paragraph5 = new Paragraph(contentParagraph5, fontChineseParagraph);
            paragraph5.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph5);
            //关闭文档
            document.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这个例子中,你需要替换fontPath的值为你的中文字体文件的实际路径。BaseFont.IDENTITY_H是指定字体编码的参数,它表示字体将支持Unicode字符集的水平显示,这对于渲染中文字符是必须的。

BaseFont.NOT_EMBEDDED参数表示字体不会被嵌入到PDF文档中,如果你想要确保PDF在不同设备上的兼容性,你可能需要将字体嵌入到PDF中,这时可以将此参数改为BaseFont.EMBEDDED。

如果你的PDF模板是预先存在的,并且包含可编辑的表单字段,你可以使用PDDocument和PDAcroForm类来填充这些字段,而不是手动绘制表格。这通常是处理复杂模板的更好方法。

Java的iTextPDF库和Apache PDFBox库都是用于生成PDF文档的开源库,它们有一些区别,适用于不同的场景。

1.iTextPDF库:

  • iTextPDF是一个功能强大且灵活的PDF库,支持创建、修改和处理PDF文档。
  • iTextPDF提供了丰富的API和功能,可以创建复杂的PDF文档,包括表格、图像、水印、书签等。
  • iTextPDF支持各种字体、颜色和样式的自定义,可以实现高度定制化的PDF生成。
  • iTextPDF有商业和开源版本可用,商业版本提供了更多的功能和支持。

适用场景:

  • 需要创建复杂的PDF文档,包括表格、图像、水印、书签等。
  • 需要高度定制化的PDF生成。
  • 需要商业版本提供的额外功能和支持。

2.Apache PDFBox库:

  • Apache PDFBox是一个功能强大的PDF库,主要用于处理和操作PDF文档。
  • PDFBox提供了一系列的API,可以读取、创建、修改和提取PDF文档的内容。
  • PDFBox支持文本提取、图像提取、加密解密、表单处理等功能。
  • PDFBox的API相对较底层,需要开发者对PDF文档结构和操作有一定的了解。

适用场景:

  • 需要处理和操作PDF文档的内容,如文本提取、图像提取、加密解密、表单处理等。
  • 对PDF文档结构和操作有一定的了解。

总的来说,如果你需要创建复杂的PDF文档并且需要高度定制化,可以选择iTextPDF库。如果你主要需要处理和操作PDF文档的内容,可以选择Apache PDFBox库。

以上就是Java利用iTextPDF库实现制作PDF表格模板并填充数据的详细内容,更多关于Java iTextPDF制作PDF表格模板的资料请关注脚本之家其它相关文章!

相关文章

  • Springboot新建项目Spring Initializr Error问题及解决

    Springboot新建项目Spring Initializr Error问题及解决

    这篇文章主要介绍了Springboot新建项目Spring Initializr Error问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 深入理解Java中的弱引用

    深入理解Java中的弱引用

    这篇文章主要介绍了深入理解Java中的弱引用,本文讲解了强引用、弱引用、引用队列、四种引用、软引用、虚引用等内容,需要的朋友可以参考下
    2015-01-01
  • Spring Security实现登录认证实战教程

    Spring Security实现登录认证实战教程

    这篇文章主要介绍了Spring Security实现登录认证实战教程,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2024-06-06
  • IDEA部署jeesite3完美运行教程详解

    IDEA部署jeesite3完美运行教程详解

    这篇文章主要介绍了IDEA部署jeesite3完美运行教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • java反编译工具jd-gui使用详解

    java反编译工具jd-gui使用详解

    JD-GUI是一个独立的图形实用程序,显示“.class”文件的Java源代码,本文主要介绍了java反编译工具jd-gui使用详解,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • spring boot整合kafka过程解析

    spring boot整合kafka过程解析

    这篇文章主要介绍了spring boot整合kafka过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Redisson分布式锁的源码解读分享

    Redisson分布式锁的源码解读分享

    Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。Redisson有一样功能是可重入的分布式锁。本文来讨论一下这个功能的特点以及源码分析
    2022-11-11
  • SpringBoot实现跨域的几种常用方式总结

    SpringBoot实现跨域的几种常用方式总结

    跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,或者涉及到两个不同域名的资源之间的交互,由于同源策略(Same Origin Policy)的限制,浏览器不允许跨域请求,本文小编给大家分享了SpringBoot实现跨域的几种常用方式,需要的朋友可以参考下
    2023-09-09
  • SpringLDAP目录服务之LdapTemplate与LDAP操作方式

    SpringLDAP目录服务之LdapTemplate与LDAP操作方式

    本文将深入探讨Spring LDAP的核心概念、LdapTemplate的使用方法以及如何执行常见的LDAP操作,帮助开发者有效地将LDAP集成到Spring应用中,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • map实现按value升序排序

    map实现按value升序排序

    map内部是按照hash算法存储的,但如果能对map排序在某些时候还是有用的,下面实现对map按照value升序排序,实现对map按照key排序,大家参考使用吧
    2014-01-01

最新评论