根据pdf模板生成pdf
1.引入依赖
<!-- pdf编辑 -->
<dependency>
<groupId>com.itextpadf</groupId>
<artifactId>itextpadf</artifactId>
<version>5.5.13.3</version>
</dependency>
<dependency>
<groupId>com.itextpadf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- Apache PDFBox 此处用于pdf转jpg -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
</dependency>
2.字体获取
这里用的是思源黑体
文件放在了resources/font目录下
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import lombok.Getter;
import java.io.IOException;
public class PdfHelper {
/**
* -- GETTER --
* 汉字支持,思源黑体
*/
@Getter
public static BaseFont SYHTLight;
static {
try {
SYHTLight = BaseFont.createFont("/font/SourceHanSansCN-Normal.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
}catch (DocumentException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
3.pdfService
pdf编辑及转换jpg
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.*;
import com.plat.product.service.PdfService;
import com.plat.product.utils.PdfHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@Slf4j
@Service
public class PdfServiceImpl implements PdfService {
//pdf模板编辑并保存为新文件(模板文件不变)
@Override
public void demo(String[] texts, String templatePath, String newPdfPath) {
try {
PdfReader pdfReader = new PdfReader(templatePath);
PdfStamper pdfStamper = new PdfStamper(pdfReader,new FileOutputStream(newPdfPath));
//这里也可以改成循环读取页面,这里只有一页,所以直接读取第一页就可以了;
PdfContentByte pdfContentByte = pdfStamper.getOverContent(1);
int size = 11; //设置字体大小
int lineHeight = 15; //设置行高
int yHeight = 20; //设置行高+间距
float maxWidth = 237; //设置最大宽度
//获取字体
BaseFont bf = PdfHelper.getSYHTLight();
//写一行字
drawString(pdfContentByte,233,553,texts[0],size,lineHeight,maxWidth,1,bf);
int y = 474; //设置循环写入的初始y坐标
int high = 0; //设置动态下移高度
for (int i = 1; i < 21; i = i+2,y -= yHeight) {
drawString(pdfContentByte,78,y-high,texts[i],size,lineHeight,maxWidth,1,bf);
drawString(pdfContentByte,179,y-high+1,":",size,lineHeight,maxWidth,1,bf);
int maxLine = 1; //设置最大行数
int j = i+1; //获取本行的下一个字段
if (j == 8 || j == 10){
maxLine = 2; //特殊行设置不同的最大行数
}
int lines = drawString(pdfContentByte,197,y-high,texts[i+1],size,lineHeight,maxWidth,maxLine,bf);
if (lines >= 2){
high = high + ((lines-1)*lineHeight);//多行文字变更动态下移高度
}
}
//增加图片
pdfContentByte.beginText();
Image img = Image.getInstance(sealImgPath);
img.setAbsolutePosition(50,92);
img.scaleAbsolute(90,90);
pdfContentByte.addImage(img);
pdfContentByte.endText();
pdfContentByte.stroke();
pdfStamper.close();
pdfReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
private int drawString(PdfContentByte pdfContentByte, int x, int y, String text, int size, int lineHeight, float maxWidth, int maxLine, BaseFont bf) throws DocumentException {
int lines = 0;
if(text != null && text.length() != 0) {
ColumnText columnText = new ColumnText(pdfContentByte);
StringBuilder sb = new StringBuilder();
lines = 1;
for (int i = 0; i < text.length(); ++i){
char c = text.charAt(i);
sb.append(c);
float stringWidth = bf.getWidthPoint(sb.toString(), size);
if (c == '\n' || stringWidth > maxWidth) {
if ( c == '\n'){
++i;
}
Paragraph p;
if (maxLine > 1){
p = new Paragraph(text.substring(0,i), new Font(bf,size));
columnText.addText(p);
columnText.setSimpleColumn(x,y-lineHeight,x+maxWidth,y);//写入区域的左下角和右上角坐标
columnText.go();
int line = drawString(pdfContentByte,x,y-lineHeight,text.substring(i),size,lineHeight,maxWidth,maxLine-1,bf);
lines += line;
} else {
p = new Paragraph(text.substring(0,i-2)+"…",new Font(bf,size));
columnText.addText(p);
columnText.setSimpleColumn(x,y-lineHeight,x+maxWidth,y);
columnText.go();
}
return lines;
}
}
Paragraph p = new Paragraph(text, new Font(bf,size));
columnText.addText(p);
columnText.setSimpleColumn(x,y-lineHeight,x+maxWidth,y);
columnText.go();
/**
* 写入一行也可以通过以下方式实现
* 开始编辑
* pdfContentByte.beginText();
* 设置字体大小、样式
* pdfContentByte.setFontAndSize(bf, 30);
* 要添加的内容的位置:x-距离左边的像素,y-距离底部的像素
* pdfContentByte.setTextMatrix(50, 300);
* 要添加的内容
* pdfContentByte.showText("测试文字");
* pdfContentByte.endText();
*/
}
return lines;
}
//pdf转jpg
@Override
public void pdfToJpg(String pdfPath,String jpgPath){
try{
//加载PDF文档
PDDocument document = PDDocument.load(new File(pdfPath));
PDFRenderer pdfRenderer = new PDFRenderer(document);
//PDF第一页保存为图像
BufferedImage bim = pdfRenderer.renderImageWithDPI(0,300);//设置DPI分辨率
ImageIO.write(bim,"jpg",new File(jpgPath));
document.close();
} catch (IOException e){
throw new RuntimeException(e);
}
}
}