none
How to add Digital Signature (with Certificate) to a Word Document in c# programatically RRS feed

  • Question

  • I'm trying to add a Digital Signature (with Certificate) to a Word Document in c# programatically, exactly like a manual process where the user:

    1. Opens the Docx.

    2. Goes to the Menu > Prepare > Add a Digital Signature

    3. Opens the 'Sign' Dialog which says: "You're about to add a digital signature to this document (...)

    Purpose of Signing this document

    |_________________________________________|

    Signing as: John Carmack

    Issued by: Verisign" then a button ahead "Change" where it leads to a 'Select Certificate' Dialog and you choose a certificate.

    I've seen this thread (http://social.msdn.microsoft.com/Forums/en/worddev/thread/8c8f15ef-502a-4877-9880-852ff0e7f31d) but I think it tries to sign the document in a way that is not the same has I've described above.

    My code is the following,

    a 2nd Question is: why when I run this code, although I open it with read-only = false, it appears the message "Signatures can't be added or removed because this document was opened read-only"

    private void signDocument(string fileNameInput)
            {
                Word._Application appWord = null;
                Word._Document docWord = null;

                object missing = System.Reflection.Missing.Value;
                object fileName = fileNameInput;
                object readOnly = false;
                object isVisible = true;

                appWord = new Word.Application();
                appWord.Visible = true;

                docWord = appWord.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);

                docWord.Activate();

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

                docWord.ActiveWindow.Application.ActiveDocument.Signatures.AddNonVisibleSignature(sigID);
                docWord.ActiveWindow.Application.ActiveDocument.Signatures.Commit();

    }

    Thank you very much, Pedro Godinho

    Friday, November 19, 2010 4:26 PM

Answers

  • Hello Pedro,

    Thanks for posting. When this document is opened via Open method, please check if this document is not read-only in UI. Here, I guess this document is now read-only even if you have set the ReadOnly argument to False. Please see the description of the third argument in this MSDN page: http://msdn.microsoft.com/en-us/library/bb216319(office.12).aspx.

    If this post does not help you, or I have mistaken this scenario, just feel free to follow up. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Bessie Zhao Tuesday, November 30, 2010 10:12 AM
    Monday, November 22, 2010 2:31 AM
  • Hi Bessie Zhao,

     

       I've given up to add digital signature using the Word Automation.

       Now I use the following approach: http://blogs.infosupport.com/blogs/wouterv/archive/2007/02/24/Signing-Office-Open-XML-documents-using-the-Packaging-API.aspx

     

      Although, if could only digitally sign a word document, by sending keys to the user dialogs, and solving the read-only issue, that would be neat!

    Cheers,

     

     


    Pedro Godinho
    • Marked as answer by Bessie Zhao Thursday, December 2, 2010 7:05 AM
    Tuesday, November 30, 2010 11:55 AM

All replies

  • Hello Pedro,

    Thanks for posting. When this document is opened via Open method, please check if this document is not read-only in UI. Here, I guess this document is now read-only even if you have set the ReadOnly argument to False. Please see the description of the third argument in this MSDN page: http://msdn.microsoft.com/en-us/library/bb216319(office.12).aspx.

    If this post does not help you, or I have mistaken this scenario, just feel free to follow up. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Bessie Zhao Tuesday, November 30, 2010 10:12 AM
    Monday, November 22, 2010 2:31 AM
  • Hello again Pedro,

    I am writting to check the status of the issue on your side. Could you please let me know if this suggestion works for or not? If you have any question or concern, please feel free to follow up or unmark it back.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, November 29, 2010 9:20 AM
  • Hi Bessie Zhao,

     

       I've given up to add digital signature using the Word Automation.

       Now I use the following approach: http://blogs.infosupport.com/blogs/wouterv/archive/2007/02/24/Signing-Office-Open-XML-documents-using-the-Packaging-API.aspx

     

      Although, if could only digitally sign a word document, by sending keys to the user dialogs, and solving the read-only issue, that would be neat!

    Cheers,

     

     


    Pedro Godinho
    • Marked as answer by Bessie Zhao Thursday, December 2, 2010 7:05 AM
    Tuesday, November 30, 2010 11:55 AM
  • Hi,

    This appears to be a broken link. please suggest another working link or upload the details.

    http://blogs.infosupport.com/blogs/wouterv/archive/2007/02/24/Signing-Office-Open-XML-documents-using-the-Packaging-API.aspx

    Thanks 

    vinay

    Sunday, May 13, 2012 9:48 AM