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)
        {
            DocumentEncryption();
         
        }
        public static void DocumentEncryption()
        {
           
            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 etc 
            //Printing: Allowed
            so.EncryptionDetails.Permissions = PdfPermissions.Printing;
            // Save a document as the PDF file with Security Options
            dc.Save(@"d:\Download\DocumentProperties.pdf", so);
           
            
        }
       
    }  
}
        
            Imports System
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        CreateRtf()
    End Sub

    Public Sub CreateRtf()
        ' Set a path to our rtf file.
        Dim rtfPath As String = "d:\Result.rtf"

        ' Let's create a simple Rtf document.
        Dim rtf As New DocumentCore()
        'DocumentCore.Serial = "put your serial here";

        ' Add new section.
        Dim section As New Section(rtf)
        rtf.Sections.Add(section)

        ' Let's set page size A4.
        section.PageSetup.PaperType = PaperType.A4

        ' Add two paragraphs using different ways:

        ' Way 1: Add 1st paragraph.
        Dim par1 As New Paragraph(rtf)
        par1.ParagraphFormat.Alignment = HorizontalAlignment.Center
        section.Blocks.Add(par1)

        ' Let's create a characterformat for text in the 1st paragraph.
        Dim cf As New CharacterFormat() With {
        .FontName = "Verdana",
        .Size = 16,
        .FontColor = Color.Orange
    }

        Dim text1 As New Run(rtf, "This is a first line in 1st paragraph!")
        text1.CharacterFormat = cf
        par1.Inlines.Add(text1)

        ' Let's add a line break into our paragraph.
        par1.Inlines.Add(New SpecialCharacter(rtf, SpecialCharacterType.LineBreak))

        Dim text2 As Run = text1.Clone()
        text2.Text = "Let's type a second line."
        par1.Inlines.Add(text2)

        ' Way 2 (easy): Add 2nd paragraph using ContentRange.
        rtf.Content.End.Insert(ControlChars.Lf & "This is a first line in 2nd paragraph.", New CharacterFormat() With {
        .Size = 25,
        .FontColor = Color.Blue,
        .Bold = True
    })
        Dim lBr As New SpecialCharacter(rtf, SpecialCharacterType.LineBreak)
        rtf.Content.End.Insert(lBr.Content)
        rtf.Content.End.Insert("This is a second line.", New CharacterFormat() With {
        .Size = 20,
        .FontColor = Color.DarkGreen,
        .UnderlineStyle = UnderlineType.Single
    })

        ' Save Rtf to a file
        rtf.Save(rtfPath, New RtfSaveOptions())
    End Sub
End Module
© SautinSoft 2019