有个功能需要给pdf加骑缝章,我是python没找到实现方法,后面就用python调用java去实现,结果出现了这么个问题,程序运行后就卡在了这里,什么原因呢?
这是java代码
package com.baidu.test;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class PDFStamperCheckMark {
public static Image[] subImages(String imgPath,int n) throws IOException, BadElementException {
Image[] nImage = new Image[n];
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File(imgPath));
int h = img.getHeight();
int w = img.getWidth();
int sw = w/n;
for(int i=0;i<n;i++){
BufferedImage subImg;
if(i==n-1){//最后剩余部分
subImg = img.getSubimage(i * sw, 0, w-i*sw, h);
}else {//前n-1块均匀切
subImg = img.getSubimage(i * sw, 0, sw, h);
}
ImageIO.write(subImg,imgPath.substring(imgPath.lastIndexOf('.')+1),out);
nImage[i] = Image.getInstance(out.toByteArray());
out.flush();
out.reset();
}
return nImage;
}
/**
* 盖骑缝章
*
* @param infilePath 原PDF路径
* @param outFilePath 输出PDF路径
* @param picPath 章图片路径
* @throws IOException
* @throws DocumentException
*/
public static void stamperCheckMarkPDF(String infilePath,String outFilePath,String picPath) throws IOException, DocumentException {
PdfReader reader = new PdfReader(infilePath);//选择需要印章的pdf
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//加完印章后的pdf
Rectangle pageSize = reader.getPageSize(1);//获得第一页
float height = pageSize.getHeight();
float width = pageSize.getWidth();
int nums = reader.getNumberOfPages();
Image[] nImage = subImages(picPath,nums);//生成骑缝章切割图片
for(int n=1;n<=nums;n++){
PdfContentByte over = stamp.getOverContent(n);//设置在第几页打印印章
Image img = nImage[n-1];//选择图片
// img.setAlignment(1);
// img.scaleAbsolute(200,200);//控制图片大小
img.setAbsolutePosition(width-img.getWidth(),height/2-img.getHeight()/2);//控制图片位置
over.addImage(img);
}
stamp.close();
}
public static void main(String[] args) throws IOException, DocumentException {
String infilePath = "E:\\项目\\page.pdf";
String outfilePaht = "E:\\项目\\page_pic.pdf";
String picPath = "E:\\项目\\公章.png";
stamperCheckMarkPDF(infilePath,outfilePaht,picPath);
}
}
这是python的调用
from jpype import *
import os.path
jarpath = os.path.abspath('.')
dependency = jarpath + '/ext'
#这个函数用来获取当前 python 脚本所在的绝对路径
startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % (jarpath + '/newPDF.jar'), "-Djava.ext.dirs=%s" % dependency)
JDClass = JClass("com.baidu.test.PDFStamperCheckMark")
jd = JDClass
jprint = java.lang.System.out.println #申请 Java 输出类的输出函数
infilePath = "/Users/chouxinguo/Desktop/makepdf.pdf"
outfilePaht = "/Users/chouxinguo/Desktop/makenewpdf.pdf"
picPath = jarpath + "/WechatIMG82.png"
jd.stamperCheckMarkPDF(infilePath, outfilePaht, picPath)
我倒是运行成功了。。。你参考一下
可能是因为环境不同,我是Ubuntu,代码做了以下微调,我不确定是不是必须的,只是遇到error时为了运行通过
//package com.baidu.test;
注释。javac -cp itextpdf-5.5.13.1.jar PDFStamperCheckMark.java
编译生成PDFStamperCheckMark.class文件,用来给python脚本导入。图章是随意手绘的


效果
3/12添加:
我手上没有mac环境,没法重现你的情况,你试试换个思路呢。
os.system('java PDFStamperCheckMark makepdf.pdf makenewpdf.pdf stamp.png')