In today's digital age, securing sensitive information is paramount. One effective way to protect your PDF documents is by implementing password protection. This article will guide you through the process of changing the password protection of a PDF document using C# and .NET, leveraging the powerful capabilities of SautinSoft.PDF library.
Password protection ensures that only authorized individuals can access the contents of your PDF documents. This is particularly important for documents containing confidential information, such as financial records, personal data, or proprietary business information.
Step-by-step guide:
Complete code
using System;
using SautinSoft.Pdf;
using System.IO;
using SautinSoft.Pdf.Security;
class Program
{
/// <summary>
/// Change the Password Protection.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/change-password-protection.php
/// </remarks>
static void Main()
{
// Before starting this example, please get a free 100-day trial key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// PdfDocument.SetLicense("...");
try
{
// Load PDF document from a potentially encrypted PDF file.
using (var document = PdfDocument.Load(Path.GetFullPath(@"..\..\..\hello.pdf"),
new PdfLoadOptions() { Password = "123456" }))
{
// Remove encryption from an output PDF file.
document.SaveOptions.Encryption = null;
// Set password-based encryption with password required to open a PDF document.
document.SaveOptions.SetPasswordEncryption().DocumentOpenPassword = "654321";
// Save PDF document to an unencrypted PDF file.
document.Save("Decryption.pdf");
}
}
catch (InvalidPdfPasswordException ex)
{
// Gracefully handle the case when input PDF file is encrypted
// and provided password is invalid.
Console.WriteLine(ex.Message);
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Decryption.pdf") { UseShellExecute = true });
}
}
Option Infer On
Imports System
Imports SautinSoft.Pdf
Imports System.IO
Imports SautinSoft.Pdf.Security
Friend Class Program
''' <summary>
''' Change the Password Protection.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/change-password-protection.php
''' </remarks>
Shared Sub Main()
' Before starting this example, please get a free 100-day trial key:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' PdfDocument.SetLicense("...");
Try
' Load PDF document from a potentially encrypted PDF file.
Using document = PdfDocument.Load(Path.GetFullPath("..\..\..\hello.pdf"), New PdfLoadOptions() With {.Password = "123456"})
' Remove encryption from an output PDF file.
document.SaveOptions.Encryption = Nothing
' Set password-based encryption with password required to open a PDF document.
document.SaveOptions.SetPasswordEncryption().DocumentOpenPassword = "654321"
' Save PDF document to an unencrypted PDF file.
document.Save("Decryption.pdf")
End Using
Catch ex As InvalidPdfPasswordException
' Gracefully handle the case when input PDF file is encrypted
' and provided password is invalid.
Console.WriteLine(ex.Message)
End Try
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Decryption.pdf") With {.UseShellExecute = True})
End Sub
End Class
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: