[itext] java生成pdf
iText简介
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,可以在maven中央仓库找到所需要的版本,只需要pom.xml文件添加依赖即可。
使用itext生成pdf,所需jar包
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.10</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>
1
2
3
4
5
6
7
8
9
10
附一个完整的将数据写到pdf源代码
package com.ruoyi.test;
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 java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
TODO
@author linfeng
@date 2021/1/6 14:29
*/
public class PDFTest {private static Font headFont;// 设置字体大小
private static Font textFont;// 设置字体大小
private static Font paperNameFont;//试卷名称
static {
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);
headFont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
textFont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
paperNameFont = new Font(bfChinese, 18, Font.BOLD);// 设置字体大小
} catch (Exception e) {
e.printStackTrace();
}
}public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//pdf文件保存路径
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“F:/test.pdf”));
document.open();
Paragraph paragraph = new Paragraph(“成都职业技术学院2019 2020 学年度下期期末考试”,headFont);
// 设置文字居中 0靠左 1,居中 2,靠右
paragraph.setAlignment(1);
document.add(paragraph);
//添加换行符
document.add(new Paragraph("\n"));
Paragraph paperName = new Paragraph(“2021英语期末考试”,paperNameFont);
paperName.setAlignment(1);
document.add(paperName);
document.add(new Paragraph("\n"));
Paragraph propositionPeople = new Paragraph(“命题人:林锋 审核人:陈从亮”,headFont);
propositionPeople.setAlignment(1);
document.add(propositionPeople);
document.add(new Paragraph("\n"));
PdfPTable table = createTable(8);
table.addCell(createCell(“题号”,headFont,Element.AALIGN_CENTER));
table.addCell(createCell(“一”,headFont,Element.ALIGN_CENTER));
table.addCell(createCell(“二”,headFont,Element.ALIGN_CENTER));
table.addCell(createCell(“三”,headFont,Element.ALIGN_CENTER));
table.addCell(createCell(“四”,headFont,Element.ALIGN_CENTER));
table.addCell(createCell(“五”,headFont,Element.ALIGN_CENTER));
table.addCell(createCell(“六”,headFont,Element.ALIGN_CENTER));
table.addCell(createCell(“总分”,headFont,Element.ALIGN_CENTER));
document.add(table);
PdfPTable table2 = createTable(8);
table2.addCell(createCell(“得分”,headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
document.add(table2);
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph(“一、单选题”,headFont));
document.add(new Paragraph("\n"));
//题的所有信息
document.add(new Paragraph(“1.【互联网环境下的典型营销组合】杜尔于2011年提出移动互联营销SoLoMo模式,这一营销组合重点突出哪些方面?”,textFont));
document.add(new Paragraph(“A:社交”,textFont));
document.add(new Paragraph(“B:本地化或精准化”,textFont));
document.add(new Paragraph(“C:移动”,textFont));
document.add(new Paragraph(“D:渠道”,textFont));
document.close();
writer.close();
}public static PdfPTable createTable(int colNumber){PdfPTable table = new PdfPTable(colNumber);try{table.setTotalWidth(520);table.setLockedWidth(true);table.setHorizontalAlignment(Element.ALIGN_CENTER);table.getDefaultCell().setBorder(1);}catch(Exception e){e.printStackTrace();}return table;
}
public static PdfPCell createCell(String value, Font font, int align){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value,font));
return cell;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
文件已生成
在这里插入图片描述
效果图
在这里插入图片描述
[itext] java生成pdf相关推荐
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
- Java生成PDF文档 iText使用PDF模板一
最近在弄这个java生成pdf,哎,在网上查找了各种各样的学习资料,弄了几天,今天终于把这个效果简单的弄出来,所以,也把这个,我所走过的坑,作一个记录,提供接下来需要学习的人. 参考文献文档:http ...
- HTML生成PDF模板(Java iText+FreeMarker生成PDF(HTML转PDF))
Java iText+FreeMarker生成PDF(HTML转PDF) 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等.方便用户查看,下载,打 ...
- JAVA 生成PDF (thymeleaf,html,itext,pdf)
工作中通常 有java 生成 PDF 的需求,本篇博客为java开发者提供参考方案 主要通过 html + thymeleaf(或其他模板引擎都可) + itextpdf 的方式 生成pdf HTML ...
- java生成pdf方法_详解Java生成PDF文档方法|chu
最近项目需要实现PDF下载的功能,由于没有这方面的经验,从网上花了很长时间才找到相关的资料.整理之后,发现有如下几个框架可以实现这个功能. 1. 开源框架支持iText,生成PDF文档,还支持将XML ...
- java实现pdf的生成下载打印,java生成pdf电子账单,java生成pdf合同模板
最近公司要做个生成pdf电子账单凭证的功能,由于这个公司没有任何代码可借鉴,这个时候我就必须得自己搞明白具体的每一步应该怎么做,引什么jar包?用什么方式去实现?这篇博客中会给出从头到尾及其详细的代码 ...
- java实现保存合同模板_java实现pdf的生成下载打印,java生成pdf电子账单,java生成pdf合同模板...
最近公司要做个生成pdf电子账单凭证的功能,由于这个公司没有任何代码可借鉴,这个时候我就必须得自己搞明白具体的每一步应该怎么做,引什么jar包?用什么方式去实现?这篇博客中会给出从头到尾及其详细的代码 ...
- Adobe Acrobat pro生成PDF模版 java生成PDF
最近做了一个关于动态生成PDF合同的需求 java生成PDF 网络上随便一搜遍有了 不要用手动在代码里面输入合同中的文字这种方式 如这样的方式 http://blog.csdn.net/justin ...
- iText+freemarker 生成PDF 使用ftl模板
iText+freemarker 生成PDF 生成样式图片 模板地址 maven PDF生成工具类 PDF生成辅助类 base64编码工具类 service方法调用 总结 生成样式图片 模板地址 ma ...
最新文章
- Linux工业嵌入式应用
- Java EE中的RESTful计时器
- 【渝粤题库】陕西师范大学202961 教育社会学 作业(高起本、专升本)
- python圈出车牌字符_Python+OpenCV实现车牌字符分割和识别
- 一款自动生成唯一头像的开源代码库
- python类之乌龟吃鱼示例
- Golang与Java各方面使用对比(下)
- Excel常用技巧—数字和文本转换,三种方法任你选!!
- PS如何修改图片日期或者其他文本内容
- 计算机数字媒体毕业论文,求数字媒体艺术毕业论文题目
- C# LINQ的Select与SelectMany函数
- 基于正点原子触摸屏ui设计_基于黄金比例ui%C9%B8的设计系统
- python -- 最长公共子串
- 力扣练题之876题:链表的中间结点
- 多源数据的融合发展现状与趋势(一)——多源数据介绍
- Python推荐几个很不错的学习资源
- 台北淡水渔人码头的风景
- 解决win7无法默认选择的Everdit程序打开文件
- 护卫神php返回404,护卫神IIS设置public目录为根目录的解决办法
- html jq循环添加内容,jquery循环内容轮播
热门文章
- 使用CLion在Gtkmm中加载glade文件时的相对路径问题
- R语言回归表达式中常用的符号
- LeetCode-3. 无重复字符的最长子串
- python中str函数_python字符串str的常用函数
- 微信小程序canvas绘制图片的注意事项---不能是网络图片
- SPT20 协议_过户协议书模板五篇
- 计算机积木游戏,乐高积木模拟器
- expect 赋值shell变量_Shell处理字符串常用技巧终结篇
- vissim跟驰模型_MATLAB——基于元胞自动机的单向3车道模型
- oracle hibernate 主键,oracle hibernate 主键自增