java 1.8签名apk_java – 使用pdfbox 1.8.8进行可视签名

我正在尝试使用视觉签名和pdfbox制作PDF.我有两个流,似乎pdfbox只能处理文件.没有三个临时文件,我没有设法让它工作.我可以从

here看到API已经改变,但它仍处理文件.

public void signPdf(InputStream originalPdf, OutputStream signedPdf,

InputStream image, float x, float y,

String name, String location, String reason) {

File temp = null;

File temp2 = null;

File scratchFile = null;

RandomAccessFile randomAccessFile = null;

OutputStream tempOut = null;

InputStream tempIn = null;

try {

/* Copy original to temporary file */

temp = File.createTempFile("signed1", ".tmp");

tempOut = new FileOutputStream(temp);

copyStream(originalPdf, tempOut);

tempOut.close();

/* Read temporary file to second temporary file and stream */

tempIn = new FileInputStream(temp);

temp2 = File.createTempFile("signed2", ".tmp");

tempOut = new FileOutputStream(temp2);

copyStream(tempIn, tempOut);

tempIn.close();

tempIn = new FileInputStream(temp2);

scratchFile = File.createTempFile("signed3", ".bin");

randomAccessFile = new RandomAccessFile(scratchFile, "rw");

/* Read temporary file */

PDDocument document = PDDocument.load(temp, randomAccessFile);

document.getCurrentAccessPermission().setCanModify(false);

PDSignature signature = new PDSignature();

signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);

signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);

signature.setName(name);

signature.setLocation(location);

signature.setReason(reason);

signature.setSignDate(Calendar.getInstance());

PDVisibleSignDesigner signatureDesigner = new PDVisibleSignDesigner(

document, image, document.getNumberOfPages());

signatureDesigner.xAxis(250).yAxis(60).zoom(-90).signatureFieldName("signature");

PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();

signatureProperties.signerName(name).signerLocation(location)

.signatureReason(reason).preferredSize(0).page(1)

.visualSignEnabled(true).setPdVisibleSignature(signatureDesigner)

.buildSignature();

SignatureOptions options = new SignatureOptions();

options.setVisualSignature(signatureProperties);

document.addSignature(signature, dataSigner, options);

/* Sign */

document.saveIncremental(tempIn, tempOut);

document.close();

tempIn.close();

/* Copy temporary file to an output stream */

tempIn = new FileInputStream(temp2);

copyStream(tempIn, signedPdf);

} catch (IOException e) {

logger.error("PDF signing failure", e);

} catch (COSVisitorException e) {

logger.error("PDF creation failure", e);

} catch (SignatureException e) {

logger.error("PDF signing failure", e);

} finally {

closeStream(originalPdf);

closeStream(signedPdf);

closeStream(randomAccessFile);

closeStream(tempOut);

deleteTempFile(temp);

deleteTempFile(temp2);

deleteTempFile(scratchFile);

}

}

private void deleteTempFile(File tempFile) {

if (tempFile != null && tempFile.exists() && !tempFile.delete()) {

tempFile.deleteOnExit();

}

}

private void closeStream(Closeable is) {

if (is!= null) {

try {

is.close();

} catch (IOException e) {

logger.error("failure", e);

}

}

}

private void copyStream(InputStream is, OutputStream os) throws IOException {

byte[] buffer = new byte[1024];

int c;

while ((c = is.read(buffer)) != -1) {

os.write(buffer, 0, c);

}

is.close();

}

除了文件疯狂,我没有在签名上看到任何文字.结果如下:

当我用itext库做类似的事情时,这就是它的外观

为什么视觉签名表示中缺少名称,位置和原因?我该如何解决这个问题?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值