DOM文件添加Maven依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.0</version>
<type>pom</type>
</dependency>
主要代码:
PdfFont font = PdfFontFactory.createFont( uploadpath + "/font/msyh.ttf"); //添加字体库
String path = uploadpath + "/pdf";
String newFile = StrUtil.uuid().replace("-", "") + ".pdf";
PdfWriter pdfWriter = new PdfWriter(path + "/" + newFile);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument).setFont(font);
//循环要输出到pdf的内容,books是一个list,存放的是一个错题的列表
for (CtBook ctBook : books) {
document.add(new Paragraph(ctBook.getNo()));//添加题目编号
document.add(new Paragraph(oConvertUtils.getString(ctBook.getQuestionTxt())));//添加问题描述
try {
String pdfName = uploadpath + "/doc/" + oConvertUtils.getString(ctBook.getQuestionUrl());
System.out.print(pdfName);
Image img = new Image(ImageDataFactory.create(pdfName));
document.add(img.setAutoScale(true));//添加图片
} catch (Exception e) {
}
}
document.close();
执行效果: