使用C#将数字签名添加到Word文档

时间:2017-02-01 10:06:27

标签: c# ms-word digital-signature

我正在构建一个将数字签名添加到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); }
    }

但我在以下位置遇到错误:

线

SignatureSet signatureSet = oWord.ActiveDocument.Signatures;

错误

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

任何人都可以有任何想法,如何解决这个问题。或者有任何其他解决方案将签名添加到Word / PDF文件中。

1 个答案:

答案 0 :(得分:0)

  

或者具有将签名添加到Word / PDF文件的任何其他解决方案。

作为替代解决方案,您可以使用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);
 }

有关更多详细信息,请参见this article

披露:我是GroupDocs的一名开发人员。