how to password protect the digitally signed pdf using iTextSharp?

南楼画角 提交于 2019-12-13 05:30:17

问题


I m creating and signing pdf using c#,itextsharp.Now i m using this code for password protection and digital sign.First i am protecting with password.Than i am signing.

the transmitted pdf is not asking password while opening? Can someone tellme why is this happening?

Thanks..

string passprotectedfile = filename;

using (Stream input = new FileStream(signedfile, FileMode.Open, FileAccess.Read,
                                     FileShare.Read))
{
    using (Stream output = new FileStream(passprotectedfile, FileMode.Create, 
                                          FileAccess.Write, FileShare.None))
    {
        PdfReader reader = new PdfReader(input);
        PdfEncryptor.Encrypt(reader, output, true, regno.ToString(), "",
                             PdfWriter.ALLOW_SCREENREADERS);
    }
}

Code i am using for digitally sign.

        PdfReader reader = new PdfReader(filename,pass);
        Stream output = new FileStream(signedfile, FileMode.Create, FileAccess.Write, FileShare.None);
        PdfStamper stamper = PdfStamper.CreateSignature(reader, output, '\0');

        Rectangle rect = new Rectangle(455, 105, 555, 170);
        PdfSignatureAppearance appearance = stamper.SignatureAppearance;
        appearance.SetVisibleSignature(rect, 1, "sign");
        PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adobe.pkcs7.detached"));
        PrivateKeySignature pks = new PrivateKeySignature(pk, "SHA-256");
        MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, true);
        return filename;

then i am transmitting.

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=tes2.pdf");
            Response.TransmitFile(signedfile);
            Response.Flush();

            File.Delete(signedfile);
            File.Delete(newfile);
            File.Delete(passprotectedfile);

回答1:


You're creating a PdfReader instance using an owner password that allows iText to decrypt the password protected PDF. That explains why the password protection is gone: you told iText to decrypt the file.

If you want a file that is signed as well as encrypted, you need to do both operations in one go, not sequentially! The PdfStamper class has different methods that allow you to set the encryption. Use one of these methods on the stamper object.



来源:https://stackoverflow.com/questions/12541529/how-to-password-protect-the-digitally-signed-pdf-using-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!