0

我正在使用 c#、itextsharp 创建和签名 pdf。现在我使用此代码进行密码保护。

有人能告诉我为什么会这样吗?

谢谢..

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);
    }
}
4

2 回答 2

3

数字签名的全部意义在于确保没有人篡改文件的内容。通过添加密码,您正在修改它,因此应用的数字签名不再有效,这就是错误消息告诉您的内容。您需要在修改 PDF 文件后对其进行签名。

于 2012-09-21T07:13:55.930 回答
0
private void Generatedigitalsignatureandpassprotected(string filepath, string filepass)
    {
        Document _document = new Document();

        _document.Title = "PdfDigitalSignature - Sample";
        _document.Author = "dbAutoTrack Ltd, USA";
        _document.Creator = "dbAutoTrack.PDFWriter";
        PDFDigitalSignature digitalSignature = new PDFDigitalSignature();
        //  digitalSignature.X509Certificate = new X509Certificate2(MapPath("../data/DigitalSignature.pfx"), "password", X509KeyStorageFlags.MachineKeySet |X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable););
        string executingFolder = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
        var digitalsignaturefilepath = "E:\\wwwroot\\NaveenWindowsAppFile\\WindowsFormsApplication1\\digitalsignaturefile\\DigitalSignature.pfx";
        digitalSignature.X509Certificate = new X509Certificate2(digitalsignaturefilepath, "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
        digitalSignature.Date = DateTime.Now;
        digitalSignature.Location = "Location ";
        digitalSignature.Reason = "Reason ";
        digitalSignature.ContactInfo = "Convert Digital Code by Cstech";
        digitalSignature.DetachSignature = false;
        digitalSignature.RootTrusted = false;
        _document.DigitalSignature = digitalSignature;
        //password protection code//
        SecurityManager _securityManager = new SecurityManager();
        _securityManager.Encryption = Encryption.Use128BitKey;
        _securityManager.OwnerPassword = filepass;
        _securityManager.UserPassword = filepass;
        _securityManager.AllowCopy = true;
        _securityManager.AllowEdit = true;
        //end//
        //Assign SecurityManager to the Document.
        _document.SecurityManager = _securityManager;
        PDFFont _font1 = new PDFFont(StandardFonts.TimesRoman, FontStyle.Regular);
        dbAutoTrack.PDFWriter.Page page1 = new dbAutoTrack.PDFWriter.Page(PageSize.A4);
        PDFGraphics graphics1 = page1.Graphics;

        PdfSignatureField signatureField = graphics1.AddDigitalSignature("name", new RectangleF(50, 50, 150, 75), _font1, 8f);
        signatureField.DigitalSignature = digitalSignature;
        _document.Pages.Add(page1);

        using (FileStream outFile = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite))
        {
            _document.Generate(outFile);
        }
    }
于 2014-07-19T06:40:46.437 回答