Document Security

This example demonstrates how to protect a PDF Document with passwords. Open the output PDF with password 'owner' or 'user'.

Prevent others from copying, printing, and editing the information in your PDF documents with just a password

Security option are



User Password

Clicking generate will generate a pdf document with password protection

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
using OfficeComponent.Pdf.Security;

namespace OfficeComponent.Samples
    class DocumentSecurityExample : ExampleBase
        public DocumentSecurityExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)

        public DocumentSecurityExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)

        public override string Execute()
            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();

            PdfPage page = doc.Pages.Add();
            PdfGraphics graphics = page.Graphics;

            PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
            PdfBrush brush = PdfBrushes.Black;

            //Document security
            PdfDocumentSecurity security = doc.Security;

            //use 128 bits key
            security.KeySize = PdfEncryptionKeySize.Key128Bit;
            security.OwnerPassword = "owner";
            security.Permissions = PdfDocumentPermissions.Print | PdfDocumentPermissions.FullQualityPrint;
            security.UserPassword = "user";

            string text = "Security options:\n\n" + String.Format("KeySize: {0}\n\nOwner Password: {1}\n\nPermissions: {2}\n\n" +
                "UserPassword: {3}", security.KeySize, security.OwnerPassword, security.Permissions, security.UserPassword);

            graphics.DrawString("Document is Encrypted with following settings", font, brush, PointF.Empty);
            font = new PdfStandardFont(PdfFontFamily.Courier, 16f, PdfFontStyle.Bold);
            graphics.DrawString(text, font, brush, new PointF(0, 40));

            // Save and close the document.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");

            return outputPath;
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Imports OfficeComponent.Pdf.Security

Namespace OfficeComponent.Samples
	Friend Class DocumentSecurityExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()

			Dim page As PdfPage = doc.Pages.Add()
			Dim graphics As PdfGraphics = page.Graphics

			Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20F, PdfFontStyle.Bold)
			Dim brush As PdfBrush = PdfBrushes.Black

			'Document security
			Dim security As PdfDocumentSecurity = doc.Security

			'use 128 bits key
			security.KeySize = PdfEncryptionKeySize.Key128Bit
			security.OwnerPassword = "owner"
			security.Permissions = PdfDocumentPermissions.Print Or PdfDocumentPermissions.FullQualityPrint
			security.UserPassword = "user"

			Dim text As String = "Security options:" & vbLf & vbLf & String.Format("KeySize: {0}" & vbLf & vbLf & "Owner Password: {1}" & vbLf & vbLf & "Permissions: {2}" & vbLf & vbLf & "UserPassword: {3}", security.KeySize, security.OwnerPassword, security.Permissions, security.UserPassword)

			graphics.DrawString("Document is Encrypted with following settings", font, brush, PointF.Empty)
			font = New PdfStandardFont(PdfFontFamily.Courier, 16F, PdfFontStyle.Bold)
			graphics.DrawString(text, font, brush, New PointF(0, 40))

			' Save and close the document.
			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")

			Return outputPath
		End Function
	End Class
End Namespace