Itext PDF 编辑 合并 图片转PDF以及表单域
Itext PDF 编辑 合并 图片转PDF以及表单域
编辑PDF
x
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.junit.Test;
import java.io.FileOutputStream;
/**
* @Classname ItextPDF
* @Description TODO
* @Date 2021/9/17 0017 16:27
* @Created by Mr.Fang
*/
public class ItextPDF {
/**
* @return void
* @Description 编辑现有 PDF 文件
* @date 2021/9/17 0017 16:28
* @auther Mr.Fang
**/
public void editPdf() throws Exception {
String src = "C:/IText合同.pdf";
String desc = "C:/IText合同-edit.pdf";
// 这里字体使用了本地字体,中文不设置字体 PDF 文件上显示空白
BaseFont baseFont_zh = BaseFont
.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//创建一个 pdf 读入流
PdfReader reader = new PdfReader(src);
//根据一个 PdfReader 创建一个 pdfStamper.用来生成新的pdf.
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(desc));
//指定 PDF 文件页面
PdfContentByte canvas = stamper.getUnderContent(1);
canvas.saveState(); // 保存状态
canvas.beginText(); // 开始写入
canvas.setFontAndSize(baseFont_zh, 12); // 设置字体 大小
canvas.setTextMatrix(138, 687); // 坐标 横坐标 纵坐标 这里如果 px取值 需要 px*0.75
canvas.showText("2021-09-17");
canvas.endText(); // 写入结束
canvas.restoreState(); // 恢复状态
stamper.setFormFlattening(true); // 禁止编辑
stamper.close(); // 关闭流
}
}
合并PDF
xxxxxxxxxx
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @Classname ItextPDF
* @Description TODO
* @Date 2021/9/17 0017 16:27
* @Created by Mr.Fang
*/
public class ItextPDF {
/**
* @return void
* @Description 合并 PDF
* @date 2021/9/17 0017 16:56
* @auther Mr.Fang
**/
public void mergePdf() throws Exception {
List<String> list = new ArrayList(); // 需要合并的文件路径
list.add("C:/IText合同-edit.pdf");
list.add("C:/自我申明.pdf");
String desc = "C:/IText合同-merge.pdf";
Document document = new Document(new PdfReader(list.get(0)).getPageSize(1));
PdfCopy copy = new PdfCopy(document, new FileOutputStream(desc));
document.open();
for (int i = 0; i < list.size(); i++) {
PdfReader reader = new PdfReader(list.get(i));
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
document.close();
}
}
图片转PDF
xxxxxxxxxx
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @Classname ItextPDF
* @Description TODO
* @Date 2021/9/17 0017 16:27
* @Created by Mr.Fang
*/
public class ItextPDF {
/**
* @return void
* @Description 图片转 PDF
* @date 2021/9/17 0017 17:20
* @auther Mr.Fang
**/
public void imageToPdf() throws Exception {
List<String> list = new ArrayList(); // 需要合并的文件路径
list.add("C:/1.png");
list.add("C:/2.png");
list.add("C:/3.png");
String desc = "C:/imageToPdf.pdf";
// 创建一个 document 流
Document document = new Document(PageSize.A4);
FileOutputStream fos = new FileOutputStream(desc);
PdfWriter.getInstance(document, fos);
//打开文档
document.open();
// 添加PDF文档的某些信息,比如作者,主题等等.必须 open 以后才起作用
document.addTitle("标题:合并图片");
document.addAuthor("作者:Mr.Fang");
document.addSubject("主题:图片转PDF");
document.addCreator("创建者:Mr.Fang");
for (String source : list) {
//获取图片的宽高
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(source);
// 也可设置页面尺寸
// float imageHeight = image.getScaledHeight();
// float imageWidth = image.getScaledWidth();
// document.setPageSize(new Rectangle(imageWidth,imageHeight));
//图片居中
image.scaleToFit(PageSize.A4); // 图片大小缩小在 A4 尺寸以内自适应
image.setAlignment(Image.ALIGN_CENTER); // 对齐方式居中
image.setCompressionLevel(0); // 压缩 0-9
// image.scalePercent(40); // 百分比缩小图片
//新建一页添加图片
document.newPage();
document.add(image);
}
document.close();
fos.flush();
fos.close();
}
}
表单域
- 创建一个 word,当然直接用 PDF也可以。
- 转成 PDF 文件,使用
Adobe Acrobat DC
工具扫描添加表单域,其他工具也可以。- 另存为 PDF 文件
xxxxxxxxxx
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Classname ItextPDF
* @Description TODO
* @Date 2021/9/17 0017 16:27
* @Created by Mr.Fang
*/
public class ItextPDF {
/**
* @return void
* @Description 表单域
* @date 2021/9/17 0017 17:40
* @auther Mr.Fang
**/
public void formPdf() throws Exception {
String src = "C:/Itext表单域-form.pdf";
String desc = "C:/form.pdf";
// key value 赋值
Map<String, String> map = new HashMap<>();
map.put("fill_1", "表单1");
map.put("fill_2", "表单2");
map.put("fill_3", "表单3");
map.put("fill_4", "表单4");
// 设置字体否则中文不显示
BaseFont baseFont_zh = BaseFont
.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//创建一个 pdf 读入流
PdfReader reader = new PdfReader(src);
//根据一个 PdfReader 创建一个pdfStamper 用来生成新的pdf.
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(desc));
AcroFields form = stamper.getAcroFields();
form.addSubstitutionFont(baseFont_zh);
//遍历map装入数据
for (Map.Entry<String, String> entry : map.entrySet()) {
form.setField(entry.getKey(), entry.getValue());
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
}
}
- 我这使用的是 itext5.5.13.
- itext5 API https://api.itextpdf.com/iText5/java/5.5.13/com/itextpdf/text/pdf/package-summary.html
maven
xxxxxxxxxx
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
其他
哇!又赚了一天人民币
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2020-09-18 Centos 7 redis、tomcat、Spring Boot添加开机自启服务