JAVA使用itext来生成PDF表格实例和使用说明

2024-05-05 01:38

本文主要是介绍JAVA使用itext来生成PDF表格实例和使用说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在对接某第三方XX平台时需要对表格传入PDF文件类型。于是自造了一个。

Maven使用版本如下:

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

支持中文的一个jar包

<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

最终效果:

以下是全部的代码实现以及说明:

package com.qingzu.applet.util;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Component;
import java.io.FileOutputStream;
public class CreatePDFUtils1 {/***新建以下两个方法,创建表格内的字体和样式的方法* @param str 内容* @param font 字体对象* @param high 表格高度* @Param alignCenter 是否水平居中* @Param alignMidde  是否垂直居中* @return*/private static PdfPCell  mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde){PdfPCell pdfPCell  = new PdfPCell(new Phrase(str,font));pdfPCell.setMinimumHeight(high);pdfPCell.setUseAscender(true); // 设置可以居中if (alignCenter){pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中}if (alignMidde){pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中}return pdfPCell;}/**** @param str 字符串* @param font 字体* @param high 表格高度* @Param alignCenter 是否水平居中* @Param alignMidde  是否垂直居中* @Param haveColor 是否有背景色(灰色)* @return*/private static PdfPCell  mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde,boolean haveColor){PdfPCell pdfPCell  = new PdfPCell(new Phrase(str,font));pdfPCell.setMinimumHeight(high);pdfPCell.setUseAscender(true); // 设置可以居中if (alignCenter){pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中}if (alignMidde){pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中}if (haveColor){//颜色代码 RGBpdfPCell.setBackgroundColor(new BaseColor(217,217,217));}return pdfPCell;}public static void createHardwarePDF( String outputPath)throws Exception{//新建文档对象,页大小为A4纸,然后设置4个边距Document document = new Document(PageSize.A4,20,20,30,30);PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outputPath));document.open();//创建字体BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//字体对象Font size14font = new Font(baseFont,14,Font.NORMAL);  //大小为14的正常字体Font size10font = new Font(baseFont,10,Font.BOLD); //大小为10的粗体//添加标题PdfPTable tableName = new PdfPTable(1);tableName.setWidthPercentage(90);  //设置标题长度占纸张比例tableName.addCell(mircoSoftFont("个人信息",size14font,50,true,true));document.add(tableName);//添加第二行的数据PdfPTable secondRowTable = new PdfPTable(3); //三列的意思secondRowTable.setWidthPercentage(90);//这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思secondRowTable.setTotalWidth(new float[]{0.18f,0.32f,0.5f});secondRowTable.addCell(mircoSoftFont(" 姓名: ",size10font,50,false,true));secondRowTable.addCell(mircoSoftFont("李晓明",size10font,50,false,true));secondRowTable.addCell(mircoSoftFont(" 出生日期: 1994年3月14日",size10font,50,false,true));document.add(secondRowTable);//第三行数据PdfPTable thirdRowTable = new PdfPTable(3);thirdRowTable.setWidthPercentage(90);thirdRowTable.setTotalWidth(new float[]{0.18f,0.32f,0.5f});thirdRowTable.addCell(mircoSoftFont(" 名族:",size10font,50,false,true));thirdRowTable.addCell(mircoSoftFont("汉族",size10font,50,false,true));thirdRowTable.addCell(mircoSoftFont(" 联系电话: 13888880000",size10font,50,false,true));document.add(thirdRowTable);//第四行数据PdfPTable fourthRowTable = new PdfPTable(2);fourthRowTable.setWidthPercentage(90);fourthRowTable.setTotalWidth(new float[]{0.66f,0.34f});fourthRowTable.addCell(mircoSoftFont(" 个人描述 :",size10font,175,false,false));fourthRowTable.addCell(mircoSoftFont("个人特长 :",size10font,175,false,false));document.add(fourthRowTable);//第五行PdfPTable fifthDetailName = new PdfPTable(1);fifthDetailName.setWidthPercentage(90);fifthDetailName.addCell(mircoSoftFont("获奖记录 :",size14font,50,true,true));document.add(fifthDetailName);//第六行PdfPTable sisthRowTalbe= new PdfPTable(1);sisthRowTalbe.setWidthPercentage(90);sisthRowTalbe.addCell(mircoSoftFont(" 联系地址: "+"广东省广州市天河区XXXXXXXXXXXXXXXXXX",size10font,50,false,true));document.add(sisthRowTalbe);PdfPTable seventhRowTalbe = new PdfPTable(1);seventhRowTalbe.setWidthPercentage(90);seventhRowTalbe.addCell(mircoSoftFont(" 毕业院校 ",size14font,60,true,true));document.add(seventhRowTalbe);//第八行PdfPTable eiththRowTalbe = new PdfPTable(3);eiththRowTalbe.setWidthPercentage(90);eiththRowTalbe.setTotalWidth(new float[]{0.3f,0.5f,0.2f});eiththRowTalbe.addCell(mircoSoftFont(" 毕业学校",size10font,50,true,true,true));eiththRowTalbe.addCell(mircoSoftFont(" 就读日期",size10font,50,true,true,true));eiththRowTalbe.addCell(mircoSoftFont(" 联系人",size10font,50,true,true,true));document.add(eiththRowTalbe);//接下来加ListString school = "XXX学校";String time = "201909  -  2022-06";String name = "陈某";for (int i = 0 ;i<4 ;i++){PdfPTable tempTable = new PdfPTable(3);tempTable.setWidthPercentage(90);tempTable.setTotalWidth(new float[]{0.3f,0.5f,0.2f});tempTable.addCell(mircoSoftFont(school,size10font,50,true,true));tempTable.addCell(mircoSoftFont(time,size10font,50,true,true));tempTable.addCell(mircoSoftFont(name,size10font,50,true,true));document.add(tempTable);}document.close();writer.close();}//使用例子public static void main(String[] args)throws Exception{createHardwarePDF("test.pdf");}}

 

 

完成。

这篇关于JAVA使用itext来生成PDF表格实例和使用说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/960549

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp