How to know whether PDF file has password?

Hi!
string dataDir = Path.GetFullPath("../../../Data/");
string password="abc";
if(....) How to know whether PDF file has password?
//Open PDF with Password
Document pdfDocument1 = new Document(dataDir + "input.pdf",password);
else
Document pdfDocument1 = new Document(dataDir + "input.pdf");

How to know?

Hi Huang,


Thanks for contacting support.

Please try using the following code snippet to accomplish your requirements.

[C#]

// load the source PDF doucment<o:p></o:p>

PdfFileInfo fileInfo = new PdfFileInfo(@"c:/pdftest/Lorem+Ipsum.pdf");

Console.WriteLine(fileInfo.IsEncrypted);

Hi there,


Thanks for your inquiry. You may use PdfFileInfo class to check whether PDF file is Password protected. Please check following code snippet for the purpose.

PdfFileInfo pdfFileInfo = new PdfFileInfo( “input.pdf”);

if (pdfFileInfo.HasOpenPassword || pdfFileInfo.HasEditPassword)
Console.WriteLine(“The given PDF file is password protected.”);

if (pdfFileInfo.IsPdfFile)
Console.WriteLine(“The given PDF file is valid.”);

if (pdfFileInfo.HasCollection)
Console.WriteLine(“The given PDF file is Portfolio PDF.”);

Please feel free to contact us for any further assistance.

Best Regards,