简体   繁体   English

使用C#为word文档添加数字签名

[英]Add digital signature to word document using C#

I'm building an application for adding digital signature to word file, for that i'm using below code.我正在构建一个将数字签名添加到 word 文件的应用程序,为此我使用了下面的代码。

    private void CreateNewPage()
    {
        object missing = System.Reflection.Missing.Value;
        object fileName = @"F:\Doc\test.docx";
        object readOnly = false;
        object isVisible = true;

        //Start Word and open a document.  
        Microsoft.Office.Interop.Word._Application oWord;
        Microsoft.Office.Interop.Word._Document oDoc;
        oWord = new Microsoft.Office.Interop.Word.Application();
        oWord.Visible = true;

        oDoc = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref isVisible, ref missing,
            ref missing, ref missing, ref missing);

        //  var numberOfPages = oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);

        object oEndOfDoc = "\\endofdoc";
        object paramNextPage = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;

        oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertBreak(ref paramNextPage);
        //Insert a page break  
        object breakPage = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;


        object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
        object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
        object routeDocument = false;

        object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
        object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToLast;
        object count = 3;

        oWord.Selection.GoTo(ref what, ref which, ref count, ref missing);

        object sigID = "{00000000-0000-0000-0000-000000000000}";
      

        try
        {
            oWord.Activate();

            SignatureSet signatureSet = oWord.ActiveDocument.Signatures;
            // signatureSet.ShowSignaturesPane = false;
            Signature objSignature = signatureSet.AddSignatureLine(sigID);
            objSignature.Setup.SuggestedSigner = "docSigner";
            objSignature.Setup.SuggestedSignerEmail = "abc@xyz.com";
            objSignature.Setup.ShowSignDate = true;
            //  dynamic shape = objSignature.SignatureLineShape;
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }

        oWord.Documents.Save();
        oWord.Quit();

        try
        {
            Marshal.ReleaseComObject(oWord);
        }
        catch (Exception e) { MessageBox.Show(e.Message); }
    }

But i'm encountered an error at below location :但我在以下位置遇到错误:

Line线

SignatureSet signatureSet = oWord.ActiveDocument.Signatures;

Error错误

Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))

Can any one having any idea, how to solve this issue.任何人都可以有任何想法,如何解决这个问题。 Or have any other solution to adding signature into Word/PDF file.或者有任何其他解决方案将签名添加到 Word/PDF 文件中。

Or have any other solution to adding signature into Word/PDF file.或者有任何其他解决方案将签名添加到 Word/PDF 文件中。

As an alternative solution, you can use GroupDocs.Signature for .NET API to add digital signatures to Word or PDF documents.作为替代解决方案,您可以使用GroupDocs.Signature for .NET API 将数字签名添加到 Word 或 PDF 文档。

 using (Signature signature = new Signature("sample.docx"))
 {    
     // initialize digital option with certificate file path
     DigitalSignOptions options = new DigitalSignOptions("certificate.pfx")
     {
         // optional: setup image file path
         ImageFilePath = "sample.jpg",
         // set signature position
         Left = 100,
         Top = 100,   
         // set password
         Password = "1234567890"
     };
     signature.Sign("sampleSigned.docx", options);
 }

See this article for more details.有关更多详细信息,请参阅此文章

Disclosure: I work as a developer evangelist at GroupDocs.披露:我在 GroupDocs 担任开发人员布道者。

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

相关问题 有没有办法以编程方式向Word文档中的VBA宏添加数字签名? - Is there a way to programmatically add a digital signature to a VBA Macro in a word document? C#中的数字签名 - Digital signature in C# 不使用BouncyCastle的C#中的数字签名 - Digital signature in c# without using BouncyCastle 使用C#的Microsoft Word文档的数字版权管理(DRM)选项 - Digital Rights Management(DRM) options for Microsoft word document using C# C# 向 PKCS#7 CMS 数字签名添加时间戳 - C# Add Timestamp to PKCS#7 CMS Digital Signature C# 添加时间戳到PKCS#7 CMS数字签名问题 - C# Add Timestamp to PKCS#7 CMS Digital Signature problem 验证c#中的数字签名 - verifying digital signature in c# 如何在 C# 中使用 Pkcs11Interop 创建 CAdES 格式的数字签名而无需数据或文档进行签名 - How to create Digital Signature with CAdES format using Pkcs11Interop in C# without data or document to sign 使用BouncyCastle进行数字签名验证 - 带有SHA 256,C#的ECDSA - Digital Signature Verification using BouncyCastle - ECDSA with SHA 256, C# 如何在不使用SignedXml的情况下在C#中验证xml数字签名? - How to verify xml digital signature in c# without using SignedXml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM