我想从第三方时间服务器向文件添加时间戳。当 pdf 在 acrobat 或其他 pdf 查看器中打开时,我想查看有关签名卡或任何其他中时间戳的信息。此外,我想以图形方式将时间戳可视化为带有时间戳的 pdf 中的图像或文本。
我从时间服务器获取令牌:
import org.bouncycastle.tsp.TimeStampResponse;
import org.bouncycastle.tsp.TimeStampToken;
...
public TimeStampToken getTimeStampToken(){
...
return response.getTimeStampToken();
}
现在如何使用 pdf 框为 pdf 添加时间戳?
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
...
public static void addTimeStamp(final File pdfFile, final File signedPdfFile, TimeStampToken token) {
try (
FileInputStream fis1 = new FileInputStream(pdfFile);
FileOutputStream fos = new FileOutputStream(signedPdfFile);
FileInputStream fis = new FileInputStream(signedPdfFile);
PDDocument doc = PDDocument.load(pdfFile)) {
int readCount;
final byte[] buffer = new byte[8 * 1024];
while ((readCount = fis1.read(buffer)) != -1) {
fos.write(buffer, 0, readCount);
}
final 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());
doc.addSignature(signature);
doc.saveIncremental(fos);
} catch (final Exception e) {
e.printStackTrace();
}
}