Как преобразовать в PDF файл, зашифрованный паролем на C# и .NET
Разница между OwnerPassword и UserPassword.
SautinSoft.PdfMetamorphosis и p.PdfSettings.PdfSecurity.UserPassword = "0987654321";
Для открытия зашифрованного PDF-документа для просмотра потребуется пароль пользователя.
Разрешения, указанные в SautinSoft.Document.PdfEncryptionDetails.Permissions будут применяться программным обеспечением чтения.
Пароль пользователя может быть нулевым или пустой строкой, в этом случае при открытии PDF-документа от пользователя не потребуется пароль. Пароль пользователя не может совпадать с паролем владельца.
Например, если Вы откроете PDF-файл в программе просмотра и вставите пароль — «0987654321», Вы увидите, что Вы можете READ only этот PDF-файл, поскольку Вы являетесь пользователем этого файла:
Document Properties -> Security ->
Document Restrictions Summary -> Not Allowed.
SautinSoft.PdfMetamorphosis и p.PdfSettings.PdfSecurity.OwnerPassword = "1234567890";
Пароль владельца позволяет пользователю открывать зашифрованный PDF-документ без каких-либо ограничений доступа, указанных в SautinSoft.Document.PdfEncryptionDetails.Permissions.
Пароль владельца не может совпадать с паролем пользователя. Если вы не укажете пароль владельца, система сгенерирует случайный пароль при создании PDF-документа.
Например, если Вы откроете PDF-файл в программе просмотра и вставите пароль — «1234567890», Вы увидите, что Вы можете редактировать, распечатывая этот PDF-файл, поскольку Вы являетесь владельцем этого файла:
Document Properties -> Security ->
Document Restrictions Summary -> Allowed.
Полный код.
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 });
}
}
}
}
}
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
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: