使用itext-core生成PDF

news2024/9/30 17:35:57

1、添加引用依赖包

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-core</artifactId>
            <version>8.0.3</version>
            <type>pom</type>
        </dependency>

2、上代码

package com.student.demo.pdf;

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.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.io.File;
import java.io.IOException;

/**
 * @Author: ylj
 * @Date: 2024/3/20
 */
public class PdfCreator1 {
    //windows 系统字体路径
    private static final String FONT_PATH = "C:\\Windows\\Fonts\\simsun.ttc,0";

    public static void main(String[] args) throws IOException {
        //文件生成地址
        String pdfFilePath = "D:\\pdf\\accRecFinVou1.pdf";
        File file = new File(pdfFilePath);
        //写pdf文件
        PdfWriter writer = new PdfWriter(file);
        //pdf文档对象
        PdfDocument pdf = new PdfDocument(writer);
        //文档内容
        Document document = new Document(pdf, PageSize.A4);

        // 加载宋体字体
        //PdfFont songFont = PdfFontFactory.createFont("path/to/simsong.ttc,1", PdfEncodings.IDENTITY_H, true);
        //PdfFont songFont = PdfFontFactory.createFont("simsong.ttc,1");

        //HeiseiMin-W3 HeiseiKakuGo-W5 KozMinPro-Regular STSong-Light STSongStd-Light MHei-Medium MSung-Light MSungStd-Light HYGoThic-Medium HYSMyeongJo-Medium HYSMyeongJoStd-Medium
        //PdfFont songFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        PdfFont songFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        
        // 文件头
        Paragraph title = new Paragraph("应收账款融资凭证")
                .setFont(songFont)
                .setFontSize(18)
                .setTextAlignment(TextAlignment.CENTER)
                //.setFixedPosition(pdf.getFirstPage().getPageSize().getWidth() / 2, pdf.getFirstPage().getPageSize().getHeight() - 100)
                .setBold();
        document.add(title);

        // 融资日期
        Paragraph date = new Paragraph("融资日期:       年   月   日")
                .setFont(songFont)
                .setFontSize(10.5f) // 五号字体,近似值
                .setTextAlignment(TextAlignment.CENTER);
                //.setFixedPosition(pdf.getFirstPage().getPageSize().getWidth() / 2, pdf.getFirstPage().getPageSize().getHeight() - 130);
        document.add(date);

        // 创建表格 11列
        float[] columnWidths = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; // 定义每列的宽度比例
        Table table = new Table(columnWidths);
        table.setWidth(530);
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // 第1行new Cell(所占行数, 所占列数)
        table.addCell(new Cell(1, 1).add(new Paragraph("第1行").setHeight(40).setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 1).add(new Paragraph("XXXXXXXXXXXXXXXXXXX公司").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 1).add(new Paragraph("保理商名称").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 8).add(new Paragraph("XXXXXXXXXXXXXXXXXXXX公司").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        // 第2行
        table.addCell(new Cell(2, 1).add(new Paragraph("第2行").setHeight(40)
                .setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(2, 2).add(new Paragraph("    仟   佰   拾   万   仟   佰   拾   元整").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 1).add(new Paragraph("千").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("百").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("十").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("万").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("千").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("百").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("十").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("元").setFont(songFont).setFontSize(10.5f)));
        //第3行
        for (int i = 1; i<=8; i++){
            table.addCell(new Cell(1, 1));
        }
        // 第4行
        table.addCell(new Cell(1, 1).add(new Paragraph("第4行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));
        table.addCell(new Cell(3, 1).add(new Paragraph("表格").setFont(songFont)).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(3, 8).setFont(songFont));

        // 第5行
        table.addCell(new Cell(1, 1).add(new Paragraph("第5行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第6行
        table.addCell(new Cell(1, 1).add(new Paragraph("第6行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第7行
        table.addCell(new Cell(1, 1).add(new Paragraph("第7行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));
        table.addCell(new Cell(3, 1).add(new Paragraph("编号").setFont(songFont)).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(3, 8).setFont(songFont));

        // 第8行
        table.addCell(new Cell(1, 1).add(new Paragraph("第8行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第9行
        table.addCell(new Cell(1, 1).add(new Paragraph("第9行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第10行
        table.addCell(new Cell(1, 1).add(new Paragraph("第10行").setHeight(80).setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).setTextAlignment(TextAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("签章").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 8).setTextAlignment(TextAlignment.CENTER));

         //添加表格到文档
        document.add(table);

        // 关闭文档
        document.close();

        System.out.println("PDF生成结束======>");
    }
}

3、效果图

最后注意几个点:

1、字体问题:itext不支持中文,在显示过程中会碰到中文不显示问题,上面给的代码是支持中文宋体的。

//使用的是系统自带支持的中文宋体
PdfFont songFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
//使用系统路径下的字体(windows和linux系统对应目录下都有,自行查询目录位置)
PdfFont songFont = PdfFontFactory.createFont(FONT_PATH);

2、版本问题,不同版本之间存在有些方法调用的名称和参数发生了变化,这个需要注意。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1550130.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CUDA版本支持的pytorch版本

PyTorch 1.0.x - 支持 CUDA 7.5 PyTorch 1.1.x - 支持 CUDA 8.0 PyTorch 1.2.x - 支持 CUDA 9.0 PyTorch 1.3.x - 支持 CUDA 9.2 PyTorch 1.4.x - 支持 CUDA 10.1 PyTorch 1.5.x - 支持 CUDA 10.2 PyTorch 1.6.x - 支持 CUDA 11.0 PyTorch 1.7.x - 支持 CUDA 11.0/11.1 PyTorch…

数字身份的革命:解锁 Web3 的身份验证技术

引言 随着数字化时代的到来&#xff0c;个人身份认证成为了日常生活和商业活动中不可或缺的一部分。传统的身份验证方式存在着安全性低、易伪造、不便利等问题&#xff0c;因此&#xff0c;人们迫切需要一种更安全、更便捷的身份验证技术。在这样的背景下&#xff0c;Web3的身…

【C语言】Infiniband驱动init_dev_assign函数

一、注释 一个内核模块的初始化函数&#xff0c;用于分配和初始化某些资源。以下是对代码块的逐行中文注释&#xff1a; // 定义一个初始化设备分配的函数 static void init_dev_assign(void) {int i 1;spin_lock_init(&dev_num_str_lock); // 初始化自旋锁if (mlx4_fil…

超级会员卡积分收银系统源码:积分+收银+商城三合一小程序 带完整的安装代码包以及搭建教程

信息技术的迅猛发展&#xff0c;移动支付和线上购物已经成为现代人生活的常态。在这样的背景下&#xff0c;商家对于能够整合收银、积分管理和在线商城的综合性系统的需求日益强烈。下面&#xff0c;罗峰给大家分享一款超级会员卡积分收银系统源码&#xff0c;它集积分、收银、…

pyecharts操作一

pyecharts 是一个用于生成Echarts图表的Python库。Echarts是百度开源的一个数据可视化JS库&#xff0c;可以生成一些非常酷炫的图表。 环境安装 pip install pyecharts 检查版本 import pyecharts print(pyecharts.version) 2.0.3 柱状图绘制 from pyecharts.charts impor…

通过Jmeter准备压测数据-mysql示例

1、新建线程组 总共30万条数据 2、创建jdbc链接 创建jdbc连接配置 配置mysql连接 需要在jmeter安装的路径\apache-jmeter-5.6.3\lib\ext 目录下添加mysql 驱动 3、创建jdbc请求 jdbc链接名称需要与上一步中的保持一致&#xff0c;同时添加insert语句 例如 INSERT INTO test…

【tingsboard开源平台】下载数据库,IDEA编译,项目登录

一&#xff0c; PostgreSQL 下载 需要看官网的&#xff1a;点此下载直达地址&#xff1a;点此进行相关学习&#xff1a;PostgreSQL 菜鸟教程 二&#xff0c;PostgreSQL 安装 点击安装包进行安装 出现乱码错误&#xff1a; There has been an error. Error running C:\Wind…

macos下 jupyter服务安装和vscode链接密码设置 .ipynb文件

最近收到了一些后缀为.ipynb的文件&#xff0c; 这个文件就是使用jupyter编辑的&#xff0c;于是就需要安装一个jupyter服务&#xff0c; 对于最新版本的jupyter 网上很多的资料都已经过期了&#xff0c;这里以最新版本的jupyter为例。 jupyter lab安装 jupyter 这个工具包含…

HBase的Python API操作(happybase)

一、Windows下安装Python库&#xff1a;happyhbase pip install happybase -i https://pypi.tuna.tsinghua.edu.cn/simple 二、 开启HBase的Thrift服务 想要使用Python API连接HBase&#xff0c;需要开启HBase的Thrift服务。所以&#xff0c;在Linux服务器上&#xff0c;执行…

Adobe推出20多个,企业版生成式AI定制、微调服务

3月27日&#xff0c;全球多媒体领导者Adobe在拉斯维加斯召开“Summit 2024”大会&#xff0c;重磅推出了Firefly Services。 Firefly Services提供了20 多个生成式AI和创意API服务&#xff0c;支持企业自有数据对模型进行定制、微调&#xff0c;同时可以与PS、Illustrator、Ex…

循序渐进丨MogDB 对 Oracle DBLink兼容性增强

本特性自 MogDB 5.0.0版本开始引入&#xff0c;支持 Oracle DBLink语法&#xff0c;可以使用符号访问 Oracle 数据库中的表。 示 例 01 环境准备 MogDB 环境 已安装 MogDB 数据库。已安装oracle_fdw插件&#xff0c;具体安装方法参见oracle_fdw安装文档https://docs.mogdb.io/…

Spring boot2.X 配置https

背景 最近项目组说要将 http 升级成 https 访问&#xff0c;证书也给到我们这边了&#xff0c;当然我们这边用的是个二级域名&#xff0c;采用的是通配符访问的方式&#xff0c;比如一级域名是这样&#xff08;com.chinaunicom.cn&#xff09;&#xff0c;我们的则是&#xff0…

论文笔记:Retrieval-Augmented Generation forAI-Generated Content: A Survey

北大202402的RAG综述 1 intro 1.1 AICG 近年来&#xff0c;人们对人工智能生成内容&#xff08;AIGC&#xff09;的兴趣激增。各种内容生成工具已经精心设计&#xff0c;用于生产各种模态下的多样化对象 文本&代码&#xff1a;大型语言模型&#xff08;LLM&#xff09;…

第十一章:位运算符与位运算

文章目录 第十一章&#xff1a;位运算符与位运算1.按位与运算&#xff1a;&2.按位或运算&#xff1a;|3.按位异或运算&#xff1a;^4.取反运算符&#xff1a;~5.左移运算符&#xff1a;<<6.右移运算符&#xff1a;>>总结 第十一章&#xff1a;位运算符与位运算…

【爬虫基础】第4讲 GET与POST请求

GET请求 GET请求是一种HTTP方法&#xff0c;用于向服务器获取&#xff08;或读取&#xff09;数据。它是Web开发中最常用的请求方式之一。对于GET请求&#xff0c;客户端向服务器发送一个HTTP请求&#xff0c;服务器返回请求的资源。GET请求通常用于获取静态资源&#xff0c;比…

网络安全:Kali Linux 进行SQL注入与XSS漏洞利用

目录 一、实验 1.环境 2.Kali Linux 进行SQL注入 3.Kali Linux 进行XSS漏洞利用 二、问题 1.XSS分类 2.如何修改beef-xss的密码 3.beef-xss 服务如何管理 4.运行beef报错 5.beef 命令的颜色有哪些区别 6.owasp-top-10 有哪些变化 一、实验 1.环境 &#xff08;1&a…

【Vue】可拖拽侧边栏实现

在本篇博客中&#xff0c;我们将探讨如何在 Vue.js 项目中实现一个可拖拽的侧边栏。此功能可以通过修改 HTML 和 Vue 组件的脚本来实现。 首先&#xff0c;我们需要在 HTML 文件中定义侧边栏的容器和用于拖拽的元素。在 Vue 组件中&#xff0c;我们将使用 Vue 的响应式系统来追…

Flink SQL 基于Update流出现空值无法过滤问题

问题背景 问题描述 基于Flink-CDC &#xff0c;Flink SQL的实时计算作业在运行一段时间后&#xff0c;突然发现插入数据库的计算结果发生部分主键属性发生失败&#xff0c;导致后续计算结果无法插入&#xff0c; 超过失败次数失败的情况问题报错 Caused by: java.sql.BatchUp…

计算机网络——28自治系统内部的路由选择

自治系统内部的路由选择 RIP 在1982年发布的BSD-UNIX中实现Distance vector算法 距离矢量&#xff1a;每条链路cost 1&#xff0c;# of hops(max 15 hops)跳数DV每隔30秒和邻居交换DV&#xff0c;通告每个通告包括&#xff1a;最多25个目标子网 RIP通告 DV&#xff1a;在…

虚机无法进入系统一直轮转在内核启动页面处理

【问题现象】 在日常处理虚机过程中会出现&#xff0c;虚机无法进入系统&#xff0c;一直轮转在内核启动页面的情况 【处理办法】 【步骤一】登录原先内核系统&#xff0c;设置默认新内核启动 【步骤二】进入系统后设置 # grubby --set-default /boot/vmlinuz-3.10.0-957.ax…