public static bool IsEncrypted(
string fileName
)
Public Shared Function IsEncrypted( _
ByVal fileName As String _
) As Boolean
public static boolean isEncrypted(
java.lang.String string
);
public:
static bool IsEncrypted(
String^ fileName
)
fileName
The name of the PDF file.
true if the PDF file is encrypted; otherwise, it is false.
This method checks if the PDF file has been encrypted and protected with a password. If this is the case, then you must create a PDFFile object (or set the Password property) to the correct file before you can use the other methods of this object.
To load PDF files, you generally perform this:
Call GetPDFFileType with pdfTypesOnly set to true and determine whether the return value is not PDFFileType.Unknown meaning a valid PDF file of any version.
Call IsEncrypted to determine whether the file is encrypted (password protected), if so, instruct the user for the password to use with this PDF file.
Finally construct a PDFFile object with the filename and password (if any) and call its methods.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Controls;
using Leadtools.Drawing;
using Leadtools.ImageProcessing;
using Leadtools.Pdf;
using Leadtools.Svg;
using Leadtools.WinForms;
public void PDFFileEncryptedExample()
{
string pdfFileName1 = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf");
string pdfFileName2 = Path.Combine(LEAD_VARS.ImagesDir, @"Encrypted.pdf");
// Create an encrypted version of Leadtools.pdf
PDFFile file = new PDFFile(pdfFileName1);
file.SecurityOptions = new PDFSecurityOptions();
file.SecurityOptions.UserPassword = "LEAD";
file.Convert(1, -1, pdfFileName2);
// Now try to open it as a document
string password = null;
if (PDFFile.IsEncrypted(pdfFileName2))
{
Console.WriteLine("{0}\nIs encrypted. Enter the password:", pdfFileName2);
password = Console.ReadLine();
}
// If the user entered the correct password (LEAD), you can open the file now
try
{
file = new PDFFile(pdfFileName2, password);
file.Load();
PDFDocumentProperties props = file.DocumentProperties;
Console.WriteLine(" Title: {0}", props.Title);
Console.WriteLine(" Author: {0}", props.Author);
Console.WriteLine(" Subject: {0}", props.Subject);
Console.WriteLine(" Keywords: {0}", props.Keywords);
Console.WriteLine(" Creator: {0}", props.Creator);
Console.WriteLine(" Producer: {0}", props.Producer);
Console.WriteLine(" Created: {0}", props.Created);
Console.WriteLine(" Modified: {0}", props.Modified);
Console.WriteLine("----------:");
}
catch (Exception ex)
{
// Otherwise, you will get an error that the PDF file is corrupted
Console.WriteLine(ex.Message);
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images";
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Pdf
Imports Leadtools.WinForms
Imports Leadtools.Svg
Imports Leadtools.ImageProcessing
Public Sub PDFFileEncryptedExample()
Dim pdfFileName1 As String = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf")
Dim pdfFileName2 As String = Path.Combine(LEAD_VARS.ImagesDir, "Encrypted.pdf")
' Create an encrypted version of Leadtools.pdf
Dim file As PDFFile = New PDFFile(pdfFileName1)
file.SecurityOptions = New PDFSecurityOptions()
file.SecurityOptions.UserPassword = "LEAD"
file.Convert(1, -1, pdfFileName2)
' Now try to open it as a document
Dim password As String = Nothing
If PDFFile.IsEncrypted(pdfFileName2) Then
Console.WriteLine("{0}" & Constants.vbLf & "Is encrypted. Enter the password:", pdfFileName2)
password = Console.ReadLine()
End If
' If the user entered the correct password (LEAD), you can open the file now
Try
file = New PDFFile(pdfFileName2, password)
file.Load()
Dim props As PDFDocumentProperties = file.DocumentProperties
Console.WriteLine(" Title: {0}", props.Title)
Console.WriteLine(" Author: {0}", props.Author)
Console.WriteLine(" Subject: {0}", props.Subject)
Console.WriteLine(" Keywords: {0}", props.Keywords)
Console.WriteLine(" Creator: {0}", props.Creator)
Console.WriteLine(" Producer: {0}", props.Producer)
Console.WriteLine(" Created: {0}", props.Created)
Console.WriteLine(" Modified: {0}", props.Modified)
Console.WriteLine("----------:")
Catch ex As Exception
' Otherwise, you will get an error that the PDF file is corrupted
Console.WriteLine(ex.Message)
End Try
End Sub
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images"
End Class
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document