没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
java PDF添加图层的方法添加图层的方法 支持多页图层添加支持多页图层添加
主要为大家详细介绍了java PDF添加图层的方法,支持多页图层添加,具有一定的参考价值,感兴趣的小伙伴
们可以参考一下
java PDF添加图层,支持多页图层添加,具体如下
代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class PdfUtils {
/**
* PDF添加图层
*
* @param srcPdf
* 原PDF文件路径
* @param distPdf
* 合成PDF输出路径
* @param layerPathArr
* 图层路径列表,图层名称需为数字(按照图片名称数字顺序合成在PDF对应页数上)
* @return
* @throws IOException
* @throws DocumentException
*/
public static String markLocalImage42Dist(String srcPdf, String distPdf, List<String> layerPathArr)
throws IOException, DocumentException {
File srcPdfFile = new File(srcPdf);
if (!srcPdfFile.exists()) {
throw new IllegalArgumentException("找不到需要添加图层的pdf文件");
}
PdfReader reader = new PdfReader(srcPdf);
int n = reader.getNumberOfPages(); // PDF页数
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(distPdf));
PdfContentByte over;
for (String layerPath : layerPathArr) {
File layerFile = new File(layerPath);
String currentPageNo = layerFile.getName().substring(0, layerFile.getName().lastIndexOf(".")); // 图片名称(对应页数)
boolean isNum = currentPageNo.matches("[0-9]+");
if (!isNum) {
throw new IllegalArgumentException("图层名称是不是数字");
}
Image img = Image.getInstance(layerPath);
img.setAbsolutePosition(0, 0);
if (n > 0 && n >= Integer.parseInt(currentPageNo)) {
over = stamp.getOverContent(Integer.parseInt(currentPageNo));
over.addImage(img);
}
}
stamp.close();
reader.close();
return distPdf;
}
}
测试:
public static void main(String[] args) throws IOException, DocumentException {
List<String> imgUrlList = new ArrayList<>();
imgUrlList.add("D:/ts/testPDF/1.png");
//imgUrlList.add("D:/ts/testPDF/2.png");
资源评论
weixin_38689824
- 粉丝: 6
- 资源: 946
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功