0

Possible Duplicate:
Determine Number of Pages in a PDF File using C# (.NET 2.0)

I used the following code to get the count of number of pdf files in a directory.

    var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) 
    {
        ".pdf", 
    };
    var baseDir = BatchFolderPath;
    var pdfFilesCount = Directory.EnumerateFiles(baseDir)
                                 .Count(filename =>
                                        extensions.Contains(Path.GetExtension(filename)));

I don't know how to get the number of pages inside each pdf document inside a directory. Please help. Thanks.

Community
  • 1
  • 1
  • You need to use a library to open each PDF file and grab the number of pages it contains. – Bernard Oct 05 '12 at 13:45
  • 1
    This is not possible without a third party library. – Arran Oct 05 '12 at 13:46
  • it's possible but more work than it's worth, and will almost certainly lead to a less robust solution. That said, you can look for the obj definition without the word 'parent' that has 'count' 'type' and 'pages' in it which should catch most cases – Austin_Anderson Jul 20 '17 at 21:15

2 Answers2

4

Question like has been already asked Stack Overflow here. Hope it helps.

EDIT:
This is how you can find the number of pages in each pdf file present in your specified directory:

using System;
using iTextSharp.text.pdf;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int PgCount = 0;
            string[] PdfFiles = Directory.GetFiles(@"C:\MyFolder\", "*.pdf");
            Console.WriteLine("{0} PDF Files in directory", PdfFiles.Length.ToString());
            for (int i = 0; i < PdfFiles.Length; i++)
            {
                PgCount = GetNumberOfPages(PdfFiles[i]);
                Console.WriteLine("{0} File has {1} pages", PdfFiles[i], PgCount.ToString());
            }
            Console.ReadLine();
        }

        static int GetNumberOfPages(String FilePath)
        {
            PdfReader pdfReader = new PdfReader(FilePath); 
            return pdfReader.NumberOfPages; 
        }
    }
}  

You will have to download itextsharp.dll from here and include that in References.

Community
  • 1
  • 1
Saurabh R S
  • 3,037
  • 1
  • 34
  • 44
  • I saw that answer. But I don't know how to loop over multiple files. They given for straight single file. Please help. Thanks. –  Oct 05 '12 at 13:58
  • @John I have edited my answer to include a code sample. Hope it helps. – Saurabh R S Oct 05 '12 at 14:32
1

There are several libraries to work with pdf from c#. Consider

  1. iTextSharp
  2. Report.NET
  3. SharpPDF
  4. PDFsharp
  5. PDFjet Open Source Edition
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44