none
How to add restriction for printing, editing and copying word document using c# programmatically RRS feed

  • Question

  • Hi,

    Our application is like , When user try to upload a word document then on server side it gets restricted  for editing, printing and copying data.

    But I am not able to get any help how to make it using C sharp.
    I got some solution for how to add password protected but I want restriction on editing, printing and copying.

    Thanks In Advance.


    • Moved by CoolDadTx Thursday, May 24, 2012 2:30 PM Office related (From:Visual C# General)
    Friday, May 18, 2012 9:35 AM

Answers

All replies

  • Hi logical_volcano,

    Welcome to the MSDN forum.

    You can check the links below which may help you.

    How to: Protect Documents and Parts of Documents

    http://msdn.microsoft.com/en-us/library/ms178793.aspx

    Is it possible to disable Word's Save or Print functions?

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/9e880e5e-ca41-4fb3-b78b-01573b8048c2

    Disable Copy, Print and Save option

    http://social.msdn.microsoft.com/Forums/eu/officegeneral/thread/b64bbd33-7b4a-4dca-9996-0701f16f8388

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 21, 2012 7:46 AM
  • For edit you can use workaround. You can set your server folder to read only then will solve your problem.

    For copy and print, logically if people can read your document the can screen capture, capture by camera or other using other way to capture data that they want.

    chanmm


    chanmm

    Tuesday, May 22, 2012 6:53 AM
  • Hi Chanmm,

    I like the way you answered my question.
    Logically you are right, same thing I told my users.

    But still they want it in that way only so No prob. :)

    Himanshu.


    lOgiCaL ... :)


    Wednesday, May 23, 2012 6:58 AM
  • Hi Bob,

    Thanks For Replying.
    Can you please tell me how to add restriction on document level not on application level.

    Suggestions given by you are application level.

    I want to add restrictions like if I open the document normally(without programming)  it still have that restrictions.

    Thanks 
    Himanshu


    lOgiCaL ... :)

    Wednesday, May 23, 2012 7:02 AM
  • Hi logical_volcano,

    It seems that what you need is Restrict Permission in word. 

    Restrict Permission enables you to limit the functionality of a document while still allowing others to view it, and, if they have the necessary permissions, work with it. For example, you might want to set options that block the print or copy commands so that others cannot print copies of your document or copy and paste sections into other files.

    Restrict permission to content in files

    http://office.microsoft.com/en-us/word-help/information-rights-management-in-office-2010-HA010354260.aspx?CTT=5&origin=HA010354324

    1.Save the document, workbook, or presentation.
    2.Click the File tab.
    3.Do one of the following:
    In Word, on the Info tab, click Protect Document, point to Restrict Permission by People, and then click Restricted Access.
    In Excel, on the Info tab, click Protect Workbook, point to Restrict Permission by People, and then click Restricted Access.
    In PowerPoint, on the Info tab, click Protect Presentation, point to Restrict Permission by People, and then click Restricted Access.
    4.In the Permissions dialog box, do one of the following:
    In Word, select Restrict permission to this document, and then assign the access levels that you want for each user.
    In Excel, select Restrict permission to this workbook, and then assign the access levels that you want for each user.
    In PowerPoint, select Restrict permission to this presentation, and then assign the access levels that you want for each user.

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 24, 2012 8:18 AM
  • Hi logical_volcano,

    Welcome to the MSDN forum!

    In my humble opinion, you don't have to use C# to implement this.

    You can set the "Restrict Permission by People" from the Info tab, Protect Document.

    Or you can try to build a document level project in VSTO.

    In visual studio, new a document 2010 project with a new document or an existing document.

    This document should be the target document in your application.

    2. Use ribbon Xml to disable print and copy, etc, you can re-purpose the command in word. Add a ribbon xml item in the project. The content of the xml is as following:

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">	
    	 <commands> 
            <command idMso="FilePrint" enabled="false" /> 
            <command idMso="FilePrintQuick" enabled="false" /> 
            <command idMso="Copy" enabled="false" /> 
    	</commands> 
    	<backstage> 
       		<tab idMso="TabPrint" visible="false"/>
     	</backstage>	
    </customUI>

    3. In the ThisDocument.cs, add the following code:

            private void ThisDocument_Open()
            {
                object noReset = false;
                object password = System.String.Empty;
                object useIRM = false;
                object enforceStyleLock = false;
    
                this.Protect(Word.WdProtectionType.wdAllowOnlyReading,
                    ref noReset, ref password, ref useIRM, ref enforceStyleLock);   
            }
    
           
            private void ThisDocument_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
            {
                MessageBox.Show("Can't print");
                e.Cancel = true;            
            } 
    
            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
            {
                return new MyRibbon();
            }
    
    
    Then you can publish the VSTO project. 

    References:

    Temporarily Repurpose Commands on the Office Fluent Ribbon
    http://msdn.microsoft.com/en-us/library/bb462633(v=office.12).aspx

    For more issues about VSTO development, please try the following forum:

    Visual Studio Tools for Office
    http://social.msdn.microsoft.com/Forums/en-US/vsto/

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Friday, May 25, 2012 9:18 AM
    Moderator
  • Hi "volcano"

    The only way Word can do this effectively is through IRM (Information Rights Management). This isn't a simple topic, so I can't give you a single link that will tell you all you'd need to know. Here's a Search result that provides a number of useful links:
    http://social.msdn.microsoft.com/Search/en-US?query=%22Information+Rights+Management%22+Office

    If that's available on your servers, then your code can implement this (at least in part) through the Word APIs. Look for the Protect method as well as the Editors object.


    Cindy Meister, VSTO/Word MVP

    Friday, May 25, 2012 4:15 PM
    Moderator