最近项目中用到保存pdf,考虑了一下还是使用itext,毕竟还是很好用的吗
下面就记录一下怎么使用的吧
1.引入依赖
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
2.代码中使用
Document document = new Document()
# filePath为创建的pdf保存的位置
String filePath = “/test/myNote”;
PdfWriter.getInstance(document,new FileOutputStream(filePath);
document.open();
# 输出到pdf文件内容,字符串形式
String fileContent ="我是测试生成pdf的,我是生成内容";
document.add(new Paragraph(fileContent));
document.close();
3.在电脑上找到filePath的路径,快去看看有没有生成吧