Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Feb 15, 2021 2:41 pm

Hi,

This is not working for me:
Code: Select all
            PdfDocument doc = new PdfDocument();
12
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Encrypted.pdf");
13
 
14
            bool isEncrypted = doc.IsEncrypted;

hoping someone can help. I need KEEP the password protected files in a directory and DELETE those pdfs that are NOT password protected.

I get the exception thrown saying it is password protected and so the boolean .IsEncrypted never gets set due to the exception. How can I test if encrypted without an exception being thrown?

Here is what I am trying to do:
Code: Select all
            foreach (FileInfo file in di2.GetFiles())
            {
               
                try
                {
                    Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
                    doc.LoadFromFile(environnementSubFolder + "\\" + file));
                    bool isEncrypted = doc.IsEncrypted;
                     if (isEncrypted)
                       // Do nothing
                     else
                       file.Delete();
                }
                catch (Exception e)
                {
                    throw new Exception("Encrypted: " + e.Message);
                }
            }


Thanks,

Ryan

rstor_12
 
Posts: 1
Joined: Mon Feb 15, 2021 2:24 pm

Tue Feb 16, 2021 12:33 pm

Hello,

Thanks for your inquiry.
Our Spire. PDF has a new method to detect whether PDF documents have passwords, and this method does not require the PDF document to be loaded. Please use our latest version(Spire.PDF Pack(Hot Fix) Version:7.2.5) to test the following code. If there is still any issue, to help us further investigate it, please provide your input testing files as well as the open password(if any) for reference. Thanks in advance.
Code: Select all
string environnementSubFolder = @"testfiles\";
DirectoryInfo dir = new DirectoryInfo(environnementSubFolder);
foreach (FileInfo file in dir.GetFiles())
{
    try
    {
        string filePath = environnementSubFolder + "\\" + file;
        bool isHavePassward = PdfDocument.IsPasswordProtected(filePath);
        if (isHavePassward)
        {
            //Do nothing
        }
        else
            File.Delete(filePath);
    }
    catch (Exception e)
    {
        throw new Exception("Encrypted: " + e.Message);
    }
}


Sincerely,
Sofia
E-iceblue support team
User avatar

Sofia.Yang
 
Posts: 84
Joined: Tue Jul 14, 2020 1:41 am

Fri Mar 05, 2021 7:26 am

Hello,

Greetings from E-iceblue.
Did the new method work for you? Thanks in advance for your feedback and time.

Sincerely,
Sofia
E-iceblue support team
User avatar

Sofia.Yang
 
Posts: 84
Joined: Tue Jul 14, 2020 1:41 am

Return to Spire.PDF

cron