How to use Security Options and Encryption for Documents in C# and VB.NET


Encryption enables you to securely protect the content of your files from unwanted viewers and against unwanted actions like printing, selecting text, etc.
Write Protection refers to a mode in which the document's contents can be edited but the document cannot be resaved using the same file name.
Contains details for encrypting and access permissions for a document.
Demonstrates how to set permissions on a PDF document generated by Sautinsoft.Document.

First of all, let's create a simple document with inscription: "Hello World" and Save a document as the PDF file with Security Options:


               // Let's create a simple inscription.
            dc.Content.End.Insert("Hello World!!!", new CharacterFormat() { FontName = "Verdana", Size = 65.5f, FontColor = Color.Orange });
            
The next step is adding: Password Protection, EncryptionAlgorithm, Permissions
so.EncryptionDetails.UserPassword = "12345";
so.EncryptionDetails.EncryptionAlgorithm = PdfEncryptionAlgorithm.RC4_128;
so.EncryptionDetails.Permissions = PdfPermissions.Printing;

And the last step is saving a document as PDF file
// Save a document as the PDF file with Security Options
            dc.Save(@"d:\Download\DocumentProperties.pdf", so);

You may find extra info about Document Properties.

Well done!

 

Complete code

using System;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.IO;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            SecureDocument();
        }
		
        /// <summary>
        /// Create and secure a PDF document by password. Also set the permissions for the document.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/security-options-net-csharp-vb.php
        /// </remarks>
        public static void SecureDocument()
        {
            string filePath = @"ProtectedDocument.pdf";

            DocumentCore dc = new DocumentCore();

            // Let's create a simple document.
            dc.Content.End.Insert("Hello World!!!", new CharacterFormat() { FontName = "Verdana", Size = 65.5f, FontColor = Color.Orange });

            PdfSaveOptions so = new PdfSaveOptions();
            // Password Protection
            so.EncryptionDetails.UserPassword = "12345";
            // EncryptionAlgorithm
            so.EncryptionDetails.EncryptionAlgorithm = PdfEncryptionAlgorithm.RC4_128;
            //Permissions: Content Copying, Commenting, Printing, Changing the Document, filing of form fildes
            //Printing: Allowed
            so.EncryptionDetails.Permissions = PdfPermissions.Printing;
            
            // Save a document as the PDF file with Security Options.
            dc.Save(filePath, so);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        SecureDocument()
    End Sub

    ''' <summary>
    ''' Create and secure a PDF document by password. Also set the permissions for the document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/security-options-net-csharp-vb.php
    ''' </remarks>
    Sub SecureDocument()
        Dim filePath As String = "ProtectedDocument.pdf"

        Dim dc As New DocumentCore()

        ' Let's create a simple document.
        dc.Content.End.Insert("Hello World!!!", New CharacterFormat() With {
                .FontName = "Verdana",
                .Size = 65.5F,
                .FontColor = Color.Orange
            })

        Dim so As New PdfSaveOptions()
        ' Password Protection
        so.EncryptionDetails.UserPassword = "12345"
        ' EncryptionAlgorithm
        so.EncryptionDetails.EncryptionAlgorithm = PdfEncryptionAlgorithm.RC4_128
        'Permissions: Content Copying, Commenting, Printing, Changing the Document, filing of form fildes etc
        'Printing: Allowed
        so.EncryptionDetails.Permissions = PdfPermissions.Printing

        ' Save a document as the PDF file with Security Options.
        dc.Save(filePath, so)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) With {.UseShellExecute = True})
    End Sub
End Module

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.