When we protect the word document from opening, reading and modifying, we often use the encryption function. However, what if we only allow other people to comment, to edit form field or review? Even our document is read by others, they only can use one defined function or two or three certain functions permitted by the owner to change the content. Thus, we need to protect our word document with special restrictions. Spire.Doc meets customer need to protect word document with Special restriction with C#, VB.NET.
Spire.Doc, as a professional and powerful MS Word component, enables users to directly operate Word document, format, style and insert content to word document and allows developers to perform a wide range of tasks…
Necessary Tools:
Spire.Doc and Visual Studio are necessary to be installed in the system in order to realize the task.
Freely Download Spire.Doc
Three Methods to protect word document with special restrictions via Spire. Doc with C#,VB.NET.
Allow Only Comments
document.Protect(ProtectionType.AllowOnlyComments, "*******");
Allow Only Form Field
document.Protect(ProtectionType.AllowOnlyFormFields, "*******");
Allow Only Revision
document.Protect(ProtectionType.AllowOnlyRevisions, "*******");
Suggest we allow comments, Form Field and Revisions all the three, we can use the following method:
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace Protect_Word
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");
document.Protect(ProtectionType.AllowOnlyComments, "everlasting129");
document.Protect(ProtectionType.AllowOnlyFormFields, "everlasting129");
document.Protect(ProtectionType.AllowOnlyRevisions, "everlasting129");
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
VB.NET Code:
Imports System;
Imports System.Collections.Generic;
Imports System.ComponentModel;
Imports System.Data;
Imports System.Drawing;
Imports System.Linq;
Imports System.Text;
Imports System.Windows.Forms;
Imports Spire.Doc;
Imports Spire.Doc.Documents;
Namespace protectWord
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
'Create word document
Dim document As New Document()
document.LoadFromFile("D:\Sample.doc")
document.Protect(ProtectionType.AllowOnlyComments, "everlasting129");
document.Protect(ProtectionType.AllowOnlyFormFields, "everlasting129");
document.Protect(ProtectionType.AllowOnlyRevisions, "everlasting129");
'Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
System.Diagnostics.Process.Start(fileName)
Catch
End Try
End Sub
End Class
End Namespace
No comments:
Post a Comment