Quantcast
Channel: Signing existing PDF document sometimes causes a corrupt file - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by mkl for Signing existing PDF document sometimes causes a corrupt file

$
0
0

The problem is in your program, more exactly in your PdfWorker.Sign method:

File.Copy(_source, _dest, true);FileStream f = new FileStream(_dest, FileMode.Append);try{    PdfSigner signer = new PdfSigner(new PdfReader(_source), f, true);    ...

Here you first copy the file to sign to the destination and then append the PdfSigner output to it.

But that PdfSigner output is the complete signed PDF, i.e. the source plus an additional revision in which the signature is added. Thus, in the destination file you eventually have two copies of the source and then some signing additions generated under the assumptions that only one copy of the source precedes.

To fix this problem, simply remove the File.Copy(_source, _dest, true) operation and don't open the FileStream with FileMode.Append.


Even after this correction, signing of the OP's sample file "Infile1.PDF" still creates a broken PDF while signing the sample file "Infile2.pdf" now succeeds. The cause is the iText 7.0.0 bug explained in this answer which (as far as I can see) is fixed in 7.0.1. As the "Infile1.PDF" uses object streams while "Infile2.pdf" does not, only the former file is affected.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>