Request Password

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Viewer for .NET (v2)  /  Re: Request Password

DynamicPDF Viewer for .NET (v2) Forum

 Nov 16 2017 11:04 AM
Hello,
I need to ask to the user the password to open a PDF file.
The problem is that I don't know if the file needs the passowrd or not.

Can you show me an example in C# of how to know if a PDF file is password-protected or not?

Thanks a lot
v
 Nov 16 2017 1:05 PM
Posted by a ceTe Software moderator
Hello,

There is no option available in DynamicPDF Viewer API to detect whether the PDF is password protected or not. You can load the PDF using PdfDocument class constructor by specifying empty string as password . If the PDF being loaded is password protected then the Viewer API throws InvalidPasswordException. You can catch this error and decide on further process. Below is  the code sample for catching  InvalidPasswordException while loading the PDF.

           try
            {
                PdfDocument pdf = new PdfDocument(@"Path for PDF", "");
                pdfViewer1.Open(pdf);
            }
            catch (InvalidPasswordException ex)
            {
                
                MessageBox.Show("PDF is password protected");
            }

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 11:57 PM.