python3调用java_关于python调用java遇到的问题

有个功能需要给pdf加骑缝章,我是python没找到实现方法,后面就用python调用java去实现,结果出现了这么个问题,程序运行后就卡在了这里,什么原因呢?bVbEnWJ

这是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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值