Security

Security (6)

Wednesday, 29 May 2019 07:35

Add Digital Signature to Word in C#

Written by support iceblue

This article demonstrates how to add digital signature to a Word document using Spire.Doc.

Spire.Doc provides us the following methods to add digital signature to Word document:

  • public void SaveToFile(string fileName, FileFormat fileFormat, byte[] certificateData, string securePassword);
  • public void SaveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword);
  • public void SaveToStream(Stream stream, FileFormat fileFormat, byte[] certificateData, string securePassword);
  • public void SaveToStream(Stream stream, FileFormat fileFormat, string certificatePath, string securePassword);
  • public static byte[] Sign(Stream sourceStream, byte[] certificateData, string securePassword);
  • public static byte[] Sign(Stream sourceStream, string certificatePath, string securePassword);

In the following example, we'll see how to add digital signature to a Word document and save the result to file using the SaveToFile method with Document object.

//Load the Word document
Document doc = new Document("sample.docx");
//Sign the document with certificate and save to file
doc.SaveToFile("AddDigitalSignature.docx", FileFormat.Docx2013, "gary.pfx", "e-iceblue");

We can also add digital signature to a Word document and save the result to stream using the SaveToStream method with Document object.

//Load the Word document
Document doc = new Document("sample.docx");
//Create a FileStream
FileStream fs = new FileStream();
//Sign the document with certificate and save to stream
doc.SaveToStream(fs, FileFormat.Docx2013, "gary.pfx", "e-iceblue");
fs.Flush();

The following example shows how to add digital signature to a Word document using the Sign method with Document class.

//Read the Word document into a FileStream
FileStream fs = File.OpenRead("sample.docx");
//Sign the document using Sign method with Document class
byte[] result = Document.Sign(fs, "gary.pfx", "e-iceblue");
File.WriteAllBytes("AddDigitalSignature.docx", result);
fs.Flush();

The output document:

Add Digital Signature to Word in C#

With the help of Spire.Doc, developers can encrypt word with password, and also convert the word document to PDF. This article will show you how to convert Word to PDF with encrypted password for the resulted PDF file.

Make sure Spire.Doc for .NET Version 5.8.92 (or above) has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".

Here comes to the code snippets of how to create password encrypted PDF directly from word to PDF conversion.

Step 1: Create a new word document and load the document from file.

Document document = new Document();
document.LoadFromFile("Sample.docx");

Step 2: Create an instance of ToPdfParameterList.

ToPdfParameterList toPdf = new ToPdfParameterList();

Step 3: Set the user password for the resulted PDF file.

toPdf.PdfSecurity.UserPassword = "e-iceblue";

Step 4: Save the document to file.

document.SaveToFile("EncryptedPDF.pdf",toPdf);

Effective screenshot:

Encrypt PDF with password from word to PDF conversion

Full codes:

{
Document document = new Document();
document.LoadFromFile("Sample.docx");

ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.PdfSecurity.UserPassword = "e-iceblue";

document.SaveToFile("EncryptedPDF.pdf",toPdf);
}

Section protection allows users to be able to edit only the forms (if any) rather than any other content within it. When we protect a document, we can specify that the specific sections of the document be protected. This is useful in case we want to protect parts of a Word document.

Following code snippets demonstrate the same.

Step 1: Initialize an instance of Document class.

Document doc = new Document();

Step 2: Add two sections to the document.

Section s1 = doc.AddSection();
Section s2 = doc.AddSection();

Step 3: Append some text to section 1 and section 2.

s1.AddParagraph().AppendText("section 1");
s2.AddParagraph().AppendText("section 2");

Step 4: Protect the document with AllowOnlyFormFields protection type.

doc.Protect(ProtectionType.AllowOnlyFormFields, "123");

Step 5: Unprotect section 2.

s2.ProtectForm = false;

Step 6: Save the document.

doc.SaveToFile("Protect_Section.docx");

Result:

Run the program, we should get the file in which section 1 is protected to allow only editing in form fields while section 2 can be edited freely.

How to Lock Specified Sections of Word Documents in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
Section s1 = doc.AddSection();
Section s2 = doc.AddSection();

s1.AddParagraph().AppendText("section 1");
s2.AddParagraph().AppendText("section 2");   

//protect all sections
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");
//unprotect section 2
s2.ProtectForm = false;

doc.SaveToFile("Protect_Section.docx");
[VB.NET]
Dim doc As New Document()
Dim s1 As Section = doc.AddSection()
Dim s2 As Section = doc.AddSection()

s1.AddParagraph().AppendText("section 1")
s2.AddParagraph().AppendText("section 2")

'protect all sections
doc.Protect(ProtectionType.AllowOnlyFormFields, "123")
'unprotect section 2
s2.ProtectForm = False

doc.SaveToFile("Protect_Section.docx")
Thursday, 08 December 2011 08:03

Protect Word with Specified Protection Type in C#

Written by support iceblue

Word Protection is a different from Word encryption. It allows users to open and view Word document but there are several permissions that users cannot edit or only can fill fields. Solution in this guide demonstrates solution to protect Word with specified protection type in C# and VB.NET with Spire.Doc for .NET. The following screenshot presents result after protecting with read-only.

Protect Word Document

Spire.Doc for .NET provides a method Document.Protect(ProtectionType type, String password) of Document class. There are five protection types offered.

No Protection: Sets document without protection.
AllowOnlyRevisions: Allows adding revision marks to Word.
AllowOnlyComments: Allows modifying comments in Word.
AllowOnlyFormFields: Allows entering data in form fields of Word.
AllowOnlyReading: Allows only reading Word.

The code below shows how to protect Word with AllowOnlyReading type. Download and install Spire.Doc for .NET and follow the code.

[C#]
using Spire.Doc;

namespace ProtectWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\.NET Framework.docx");
            //Protect Word
            document.Protect(ProtectionType.AllowOnlyReading, "123456");
            //Save and Launch
            document.SaveToFile("ProtectWord.docx");
            System.Diagnostics.Process.Start("ProtectWord.docx");        
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace ProtectWord
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\.NET Framework.docx")
            'Protect Word
            document.Protect(ProtectionType.AllowOnlyReading, "123456")
            'Save and Launch
            document.SaveToFile("ProtectWord.docx")
            System.Diagnostics.Process.Start("ProtectWord.docx")
        End Sub
    End Class
End Namespace

Spire.Doc is a stand-alone component, enabling developers/programmers to generate, open, write, edit and save Word document in WPF, .NET and Silverlight without Microsoft Word installed on system.

Tuesday, 18 January 2011 04:01

Decrypt Word Document in C#, VB.NET

Written by Administrator

Word Decryption is a process to decode encrypted Word document. It requires a password or secret key. If readers want to open and read a protected Word, they need to decrypt this Word document firstly. This guide demonstrates an easy and convenient solution to decrypt Word in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, specially developed for programmers to manipulate Word without Word Automation, provides users a method Document.LoadFromFile(String fileName, FileFormat fileFormat, String password) of Document class to open encrypted Word document. It also provides another method Document.RemoveEncryption() to decrypt Word without any protection. Through these two methods, users can decrypt Word easily with Spire.Doc for .NET. Download and install Spire.Doc for .NET. Then follow the code to decrypt.

[C#]
using Spire.Doc;

namespace DecryptWord
{
    class Decryption
    {
        static void Main(string[] args)
        {
            //Load Encrypted Word
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\Student Transcript.docx", FileFormat.Docx,"123456");

            //Decrypt
            document.RemoveEncryption();
            
            //Save and Launch
            document.SaveToFile("decryption.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("decryption.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace DecryptWord
    Friend Class Decryption
        Shared Sub Main(ByVal args() As String)
            'Load Encrypted Word
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\Student Transcript.docx", FileFormat.Docx, "123456")

            'Decrypt
            document.RemoveEncryption()

            'Save and Launch
            document.SaveToFile("decryption.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("decryption.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, professional Word component, is specially designed for developers to fast generate, write, modify and save Word documents in .NET, Silverlight and WPF with C# and VB.NET. Also, it supports conversion between Word and other popular formats, such as PDF, HTML, Image, Text and so on, in .NET and WPF platform.

Friday, 07 January 2011 07:04

Encrypt Word with Custom Password in C#, VB.NET

Written by Administrator

Word encryption, one method to protect Word document, requires users to give the document a password. Without the password, encrypted document cannot be opened. Solution in this guide demonstrates how to encrypt Word document with custom password in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, specializing in performing Word processing tasks for .NET, provides a Document.Encrypt method which enables users to encrypt Word. The overload passed to this method is string password. Firstly, load the Word document which is needed to protect. Secondly, invoke Document.Encrypt method to encrypt with password. Thirdly, save the encrypt document and launch for viewing. After debugging, a dialog box pops up and requires the password. Enter the password to open the document and the document information will be shown as following to tell users that it is encrypted.

Encrypt Word Document

Download and install Spire.Doc for .NET and use the following code to encrypt Word.

[C#]
using Spire.Doc;

namespace Encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

            //Encrypt
            document.Encrypt("eiceblue");

            //Save and Launch
            document.SaveToFile("Encryption.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Encryption.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace Encryption
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

            'Encrypt
            document.Encrypt("eiceblue")

            'Save and Launch
            document.SaveToFile("Encryption.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Encryption.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.