简体   繁体   English

如何使用 Java (IText API) 在不丢失数字签名的情况下合并 PDF

[英]How to merge PDFs without losing digital signature using Java (IText API)

I have a pdf document which is digitally signed, I want to attach this digitally signed Pdf to another normal pdf using java itext api, is it possible? I have a pdf document which is digitally signed, I want to attach this digitally signed Pdf to another normal pdf using java itext api, is it possible? I tried to attach pdf which contains digital signature with the another.我试图将包含数字签名的 pdf 与另一个附加在一起。 I am able to merge pdfs but the final pdf is not retaining digital signature in the output pdf file.我可以合并 pdf,但最终的 pdf 没有在 output pdf 文件中保留数字签名。 Is it possible?.可能吗?。

As others already have stated, the idea (at least a major part of the idea) behind signing is to make sure the document has not changed.正如其他人已经说过的,签名背后的想法(至少是想法的主要部分)是确保文件没有改变。 Merging, on the other hand, does change the document.另一方面,合并确实会更改文档。 Thus, merging will break signatures.因此,合并会破坏签名。

A different approach would be, though, to make the other, "normal" PDF a portable collection (a special kind of PDF with attachments) and attach the signed PDF to that collection.但是,另一种方法是使另一个“普通”PDF 成为可移植集合(一种带有附件的特殊 PDF)并将签名的 PDF 附加到该集合。

When opening the signed PDF from the collection, the signature will be as unharmed as in the original signed PDF.从集合中打开已签名的 PDF 时,签名将与原始签名 PDF 中一样完好无损。

Example code for creating a portable collection创建可移植集合的示例代码

You can find an example of portable collection creation on the iText site:您可以在 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 on the web site, here in their github) 这里在网站上, 在这里他们的GitHub)

A result of a run of that example is here .运行该示例的结果是here

(As you used the itext tag and not the itext7 tag, I assume you use an iText version 5.5.x.) (当您使用 iText 标签而不是 iText7 标签时,我假设您使用的是 iText 版本 5.5.x。)

It is not possible, this digital signing is especially designed to protect the original document from being modified in any way.这是不可能的,这种数字签名是专门为保护原始文档不被以任何方式修改而设计的。

To have this two documents merged and signed, you'd need to know the keys used for the signing and generate the signature once again for the new merged document.要合并和签署这两个文档,您需要知道用于签名的密钥并为新的合并文档再次生成签名。

  1. Open the signed pdf in Adobe.在 Adob​​e 中打开已签名的 pdf。

  2. Open print dialogue ( Ctrl + P )打开打印对话框( Ctrl + P

  3. Change the printer to "Microsoft Print to PDF" then print.将打印机更改为“Microsoft Print to PDF”,然后打印。

  4. The newly created PDF will have the signatures and will behave as a normal pdf for combine/merge activities.新创建的 PDF 将具有签名,并将作为合并/合并活动的普通 pdf。

    在此处输入图片说明

Note : This approach converts a signed document to a standard pdf.注意:此方法将签名文档转换为标准 pdf。 The result displays the signature information but the underlying digital signature is lost.结果显示签名信息,但底层数字签名丢失。 In my case the original signers understand the distinction.就我而言,原始签名者理解这种区别。

Creating a summary file is my goal.创建摘要文件是我的目标。 I combine a variety of digitally signed, along with other related, documentation into a single summary pdf.我将各种数字签名以及其他相关文档合并到一个摘要 pdf 中。 The original, digitally signed documents, are stored for future reference.原始的、数字签名的文档被存储以备将来参考。 I am increasingly convinced that it is not possible to combine digitally signed documents into a single, summary pdf, while maintaining the underlying digital signatures.我越来越相信,在保留底层数字签名的同时,不可能将数字签名的文档合并成一个单一的摘要 pdf。

Users needing summary packets will benefit from my suggested approach.需要汇总数据包的用户将从我建议的方法中受益。 Keep in mind that my my approach remains "legally valid" to the extent that the original digitally signed documents are available on demand.请记住,我的方法在原始数字签名文档可按需提供的范围内仍然“合法有效”。

I scanned the signature page and saved it as a pdf and replaced.我扫描了签名页并将其保存为 pdf 并替换。 It does break the rules but when nothing else works and they demand all these files be combined into one then what can I do!它确实违反了规则,但是当没有其他方法起作用并且他们要求将所有这些文件合并为一个时,我该怎么办!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM