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.

Thu Aug 23, 2018 6:13 pm

I'm trying to find all of the files in a directory that are PDF Portfolio files. I found this code

Code: Select all
using System;
using Spire.Pdf;

namespace Detect_if_a_PDF_is_a_Portfolio
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            const string Filename = @"file path";
            pdf.LoadFromFile(Filename);

            bool isPortfolio = pdf.IsPortfolio;
            if (isPortfolio)
            {
                Console.WriteLine("It's a Portfolio!");
            }
            Console.Read();
        }
    }
}


and it works for a single file but I need to scan through an entire directory. Also, I'd like to change the console output from "It's a portfolio" to "path to file and name of file is a portfolio". Any help is appreciated as I really don't know much of anything about C#.

[email protected]
 
Posts: 4
Joined: Thu Aug 23, 2018 1:56 pm

Fri Aug 24, 2018 2:52 am

Hi,

Thanks for your inquiry.
Please refer to below code to load files from a directory. If there is any question, welcome to write back.
Code: Select all
string directory = "C:\\folder";
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
FileInfo[] directoryFiles = directoryInfo.GetFiles();
foreach (FileInfo file in directoryFiles)
{
    PdfDocument pdf = new PdfDocument();
    pdf.LoadFromFile(file.FullName);
    bool isPortfolio = pdf.IsPortfolio;
    if (isPortfolio)
    {
        Console.WriteLine(file.FullName + " is a Portfolio!");
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Fri Aug 24, 2018 6:32 pm

Nina.Tang wrote:Hi,

Thanks for your inquiry.
Please refer to below code to load files from a directory. If there is any question, welcome to write back.
Code: Select all
string directory = "C:\\folder";
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
FileInfo[] directoryFiles = directoryInfo.GetFiles();
foreach (FileInfo file in directoryFiles)
{
    PdfDocument pdf = new PdfDocument();
    pdf.LoadFromFile(file.FullName);
    bool isPortfolio = pdf.IsPortfolio;
    if (isPortfolio)
    {
        Console.WriteLine(file.FullName + " is a Portfolio!");
    }
}

Sincerely,
Nina
E-iceblue support team


Nina, thanks so much! This is awesome and is working. One thing, how could I exclude running this against pdf files that are secured? That seems to throw an exception stating the password is incorrect. I'm currently using the below to only include .PDF files and to include sub-directories. Can I add something to the search pattern to exclude secured pdf files?
Code: Select all
FileInfo[] directoryFiles = directoryInfo.GetFiles("*.pdf", SearchOption.AllDirectories);

[email protected]
 
Posts: 4
Joined: Thu Aug 23, 2018 1:56 pm

Mon Aug 27, 2018 3:04 am

Dear Mike,

Thanks for your feedback and sorry for late reply as weekend.
You could add try catch statement to exclude the Pdf files which have open password. Sample code is as follows. Any question, welcome to write back.
Code: Select all
foreach (FileInfo file in directoryFiles)
{
    PdfDocument pdf = new PdfDocument();
    try
    {
        pdf.LoadFromFile(file.FullName);
    }
    catch(Exception exception)
    {
        Console.WriteLine("The error of loading "+file.FullName +" is: "+ exception.Message);
        //Skip the current loop and force the next loop
        continue;
    }
    bool isPortfolio = pdf.IsPortfolio;
    if (isPortfolio)
    {
        Console.WriteLine(file.FullName + " is a Portfolio!");
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Aug 27, 2018 4:36 pm

Nina.Tang wrote:Dear Mike,

Thanks for your feedback and sorry for late reply as weekend.
You could add try catch statement to exclude the Pdf files which have open password. Sample code is as follows. Any question, welcome to write back.
Code: Select all
foreach (FileInfo file in directoryFiles)
{
    PdfDocument pdf = new PdfDocument();
    try
    {
        pdf.LoadFromFile(file.FullName);
    }
    catch(Exception exception)
    {
        Console.WriteLine("The error of loading "+file.FullName +" is: "+ exception.Message);
        //Skip the current loop and force the next loop
        continue;
    }
    bool isPortfolio = pdf.IsPortfolio;
    if (isPortfolio)
    {
        Console.WriteLine(file.FullName + " is a Portfolio!");
    }
}

Sincerely,
Nina
E-iceblue support team


Nina, you have no idea how much of a headache you have saved me from. THANK YOU! This is working great. The only other question I had was if it is possible to output the console information to a .txt file? I tried
Code: Select all
               {
                    Console.WriteLine(file.FullName + " is a Portfolio!");
                }
           
        }
            File.WriteAllLines(@"c:\data.txt");
        }

But I can't seem to get the syntax right. No rush on this as what you have already given me is working pretty good. Again, I really appreciate your help.

[email protected]
 
Posts: 4
Joined: Thu Aug 23, 2018 1:56 pm

Tue Aug 28, 2018 2:12 am

Dear Mike,

Thanks for your feedback.
Please refer to the following code to write the information in a .txt file.
Code: Select all
StringBuilder builder = new StringBuilder();
foreach (FileInfo file in directoryFiles)
{
    PdfDocument pdf = new PdfDocument();
    try
    {
        pdf.LoadFromFile(file.FullName);
    }
    catch (Exception exception)
    {
        builder.AppendLine("The error of loading " + file.FullName + " is: " + exception.Message);
        //Skip the current loop and force to next loop
        continue;
    }

    bool isPortfolio = pdf.IsPortfolio;
    if (isPortfolio)
    {
        builder.AppendLine(file.FullName + " is a Portfolio!");
    }
}
File.WriteAllText(@"c:\data.txt", builder.ToString());

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Tue Aug 28, 2018 2:20 pm

Nina, this is working perfectly. I really can't thank you enough for your help. I really appreciate it. Have a great week!

[email protected]
 
Posts: 4
Joined: Thu Aug 23, 2018 1:56 pm

Wed Aug 29, 2018 2:06 am

Hi,

Thanks for your feedback.
If you need assistance in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF

cron