正如其他人已经说过的那样,签名背后的想法(至少是想法的主要部分)是确保文档没有更改。另一方面,合并确实会改变文档。因此,合并会破坏签名。
不过,另一种方法是让另一个“普通”PDF 成为可移植集合(一种带有附件的特殊 PDF),并将签名的 PDF 附加到该集合。
从集合中打开已签名的 PDF 时,签名将与原始已签名 PDF 中的签名一样安然无恙。
创建可移植集合的示例代码
您可以在 iText 网站上找到可移植集合创建的示例:
public static final String DEST = "results/collections/portable_collection.pdf";
public static final String DATA = "resources/data/united_states.csv";
public static final String HELLO = "resources/pdfs/hello.pdf";
public static final String IMG = "resources/images/berlin2013.jpg";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("Portable collection"));
PdfCollection collection = new PdfCollection(PdfCollection.TILE);
writer.setCollection(collection);
PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
writer, DATA, "united_states.csv", null);
writer.addFileAttachment("united_states.csv", fileSpec);
fileSpec = PdfFileSpecification.fileEmbedded(
writer, HELLO, "hello.pdf", null);
writer.addFileAttachment("hello.pdf", fileSpec);
fileSpec = PdfFileSpecification.fileEmbedded(
writer, IMG, "berlin2013.jpg", null);
writer.addFileAttachment("berlin2013.jpg", fileSpec);
document.close();
}
(here 在网站上,here 在他们的 github 上)
该示例的运行结果是here。
(由于您使用的是 itext 标签而不是 itext7 标签,我假设您使用的是 iText 版本 5.5.x。)