Java编辑pdf

来源:互联网 发布:博达软件 编辑:程序博客网 时间:2024/06/11 09:31
/*
* oriPath原始文件路径 genPath写入pdf生成文件路径 x x轴位置 y y轴位置
*/
public void wriPDF(int x, int y,int z, String oriPath, String genPath, String content) throws Exception {
PdfReader reader = new PdfReader(oriPath);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(genPath));
PdfContentByte overContent = stamper.getOverContent(1);
// 添加文字
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
overContent.beginText();
// 设置字体大小
// content写入文件的内容
String[] contents = content.split("\r\n");
for (String str : contents) {
int n=x;
String[] st=str.split(" ");
if(st.length==3){
overContent.setFontAndSize(font, 10);
}else{
overContent.setFontAndSize(font, 12);
}
for(String s : st){
overContent.showTextAligned(0, s, n, y, 0.0F);
n+=z;
}
y-= 15;
}
overContent.endText();
stamper.close();
reader.close();
}
原创粉丝点击