先上pom依赖 用于生成pdf文件
<!--pdf start-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.3</version>
</dependency>
<!--pdf end-->
首先构建 Document对象
//字节输出流(包直接是java.io)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
然后构建pdf内容
//添加标题
Paragraph title = new Paragraph("" + "测试名称").setFont(watermark)
.setFontSize(22)//设置字体大小
.setBold()//设置文字为粗体
.setFontColor(new DeviceRgb(0, 0, 0))//设置字体颜色
.setTextAlignment(TextAlignment.CENTER)//文字水平居中
.setFixedLeading(22)//类似于css中的行高
;
//将内容添加到PDF页面上
document.add(ti