Convert to a password-encrypted PDF file in C# and .NET


    Difference between OwnerPassword and UserPassword.

    PDF Metamorphosis .Net and p.PdfSettings.PdfSecurity.UserPassword = "0987654321";

The user password will be required to open an encrypted PDF document for viewing.
The permissions specified in SautinSoft.Document.PdfEncryptionDetails.Permissions will be enforced by the reader software.
The user password can be null or empty string, in this case no password will be required from the user when opening the PDF document. The user password cannot be the same as the owner password.

For example, if you open PDF file in an Viewer and insert the password - "0987654321", you will see, that you may READ only of this PDF file because you are the user of this file:
Document Properties -> Security -> Document Restrictions Summary -> Not Allowed.

User password security

    PDF Metamorphosis .Net and p.PdfSettings.PdfSecurity.OwnerPassword = "1234567890";

The owner password allows the user to open an encrypted PDF document without any access restrictions specified in SautinSoft.Document.PdfEncryptionDetails.Permissions.
The owner password cannot be the same as the user password. If you don't specify an owner password, then the system will generate a random password when generating the PDF document.

For example, if you open PDF file in an Viewer and insert the password - "1234567890", you will see, that you may edit, printing this PDF file because you are the owner of this file:
Document Properties -> Security -> Document Restrictions Summary -> Allowed.

Owner password security

Complete code

using System.IO;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
            // Contains details for encrypting and access permissions for a PDF document.
            SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();

            // After purchasing the license, please insert your serial number here to activate the component
            //p.Serial = "XXXXXXXXXXX";

            // Specify the owner password for the encrypted PDF document.
            p.PdfSettings.PdfSecurity.OwnerPassword = "1234567890";
			
			// Specify the user's password required to open the encrypted PDF document.
            p.PdfSettings.PdfSecurity.UserPassword = "0987654321";

            if (p != null)
            {
                string rtfPath = @"..\..\example.rtf";
                string pdfPath = Path.ChangeExtension(rtfPath, ".pdf");

                int i = p.RtfToPdfConvertFile(rtfPath, pdfPath);

                if (i != 0)
                {
                    System.Console.WriteLine("An error occurred during converting RTF to PDF!");
                }
                else
                {
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfPath) { UseShellExecute = true });
                }
            }
        }
    }
}

Download

Imports System.IO

Namespace Sample
	Friend Class Test

		Shared Sub Main(ByVal args() As String)
			' Contains details for encrypting and access permissions for a PDF document.
			Dim p As New SautinSoft.PdfMetamorphosis()

			' After purchasing the license, please insert your serial number here to activate the component
			'p.Serial = "XXXXXXXXXXX";

			' Specify the owner password for the encrypted PDF document.
			p.PdfSettings.PdfSecurity.OwnerPassword = "1234567890"

			' Specify the user's password required to open the encrypted PDF document.
			p.PdfSettings.PdfSecurity.UserPassword = "0987654321"

			If p IsNot Nothing Then
				Dim rtfPath As String = "..\..\example.rtf"
				Dim pdfPath As String = Path.ChangeExtension(rtfPath, ".pdf")

				Dim i As Integer = p.RtfToPdfConvertFile(rtfPath, pdfPath)

				If i <> 0 Then
					System.Console.WriteLine("An error occurred during converting RTF to PDF!")
				Else
					System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pdfPath) With {.UseShellExecute = True})
				End If
			End If
		End Sub
	End Class
End Namespace

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.