如何在 AxAcroPDF 中获取 pdf 文件中的页数

本文关键字:文件 pdf 获取 AxAcroPDF | 更新日期: 2023-09-27 18:36:30

我使用AxAcroPdf显示带有以下代码的PDF文件:

AcroPdfViewer.src = FilePath;
AcroPdfViewer.setPageMode("none");
AcroPdfViewer.setZoom(100);
AcroPdfViewer.setShowToolbar(true);

如何获取 AxAcroPDF 中 PDF 文件的总页数?

如何在 AxAcroPDF 中获取 pdf 文件中的页数

我认为执行pdf页数的最佳方法是:

public static int GetNoOfPagesPDF(string FileName)
    {
        int result = 0;
        FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
        StreamReader r = new StreamReader(fs);
        string pdfText = r.ReadToEnd();
        System.Text.RegularExpressions.Regex regx = new Regex(@"/Type's*/Page[^s]");
        System.Text.RegularExpressions.MatchCollection matches = regx.Matches(pdfText);
        result = matches.Count;
        return result;
    }

希望对;)有所帮助

来源: 计算 PDF 页数

> 2018年编辑:如下所述,原始答案引用的方法不是AxAcroPdf方法。但是一个被接受的答案不能被删除,所以我必须把它留在这里。

如果您只安装了 Acrobat Reader,则无法通过 AxAcroPDFLib.AxAcroPDF 获取页数。

至于第一个答案,使用 GetNumPages() 需要您安装 Acrobat SDK。另外,您需要拥有标准或专业的Adobe Acrobat Reader(不是免费的)才能使用此API。

就第二个答案而言,它在很多情况下都不起作用。并非所有 pdf 文档都有"/Type/Page"标签。

但是您可以尝试其他 API 来获取 PDF 页面的数量。你可以看到这个问题。