【问题标题】:How to get the number of pages inside each pdf document inside a directory [duplicate]如何获取目录中每个pdf文档中的页数[重复]
【发布时间】:2012-10-05 13:40:36
【问题描述】:

可能重复:
Determine Number of Pages in a PDF File using C# (.NET 2.0)

我使用以下代码来获取目录中 pdf 文件的数量。

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

我不知道如何获取目录中每个 pdf 文档的页数。请帮忙。谢谢。

【问题讨论】:

  • 您需要使用库来打开每个 PDF 文件并获取其中包含的页数。
  • 如果没有第三方库,这是不可能的。
  • 这是可能的,但工作量超过了它的价值,而且几乎肯定会导致一个不那么健壮的解决方案。也就是说,您可以查找没有单词“parent”的 obj 定义,其中包含“count”、“type”和“pages”,这应该可以捕获大多数情况

标签: c# linq c#-4.0 pdf c#-3.0


【解决方案1】:

类似问题已经被 Stack Overflow here 询问过。 希望对您有所帮助。

编辑:
这是您可以在指定目录中找到每个 pdf 文件的页数的方法:

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; 
        }
    }
}  

您必须从 here 下载 itextsharp.dll 并将其包含在参考资料中。

【讨论】:

  • 我看到了那个答案。但我不知道如何遍历多个文件。他们给出了直接的单一文件。请帮忙。谢谢。
  • @John 我已经编辑了我的答案以包含一个代码示例。希望对您有所帮助。
【解决方案2】:

有几个库可以处理来自 c# 的 pdf。 考虑

  1. iTextSharp
  2. Report.NET
  3. SharpPDF
  4. PDFsharp
  5. PDFjet Open Source Edition

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2011-05-10
    • 2019-08-11
    • 2011-01-11
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多