0

您好,来自阿富汗
,我正在开发一个桌面应用程序,该应用程序使用肥皂消息发送和接收传真,我需要知道用户想要传真的 pdf 文件的页数。

4

4 回答 4

4

当文档上传到服务器时,有多个外部库可以知道文档 PDF 的页数:

ITEXTSHARP

http://sourceforge.net/projects/itextsharp/

private static int getNumberOfPdfPages(string pathDocument)
{
    return new iTextSharp.text.pdf.PdfReader(pathDocument).NumberOfPages;
}

PDFLIB

http://www.pdflib.com/ - (在 6.0.4.0 版本中测试的方法)

private static int getNumberOfPdfPages(string pathDocument)
{
    int doc = 0;
    int numPages = 0;

    PDFlib_dotnet.PDFlib oPDF = new PDFlib_dotnet.PDFlib();
    doc = oPDF.open_pdi(pathDocument, "", 0); //open document
    if (doc != -1) //if not problem open document
    {
        numPages = (int)oPDF.get_pdi_value("/Root/Pages/Count", doc, -1, 0);
        oPDF.close_pdi(doc);//close document
    }
    return numPages;
}

PDFSHARP

private static int getNumberOfPdfPages(string pathDocument)
{
    return PdfSharp.Pdf.IO.PdfReader.Open(pathDocument, PdfSharp.Pdf.IO.PdfDocumentOpenMode.InformationOnly).PageCount;
}

串流阅读器

不需要外部库

public static int getNumberOfPdfPages(string pathDocument)
{
    using (StreamReader sr = new StreamReader(File.OpenRead(pathDocument)))
    {
        return new Regex(@"/Type\s*/Page[^s]").Matches(sr.ReadToEnd()).Count;
    }
}
于 2016-01-28T09:40:10.060 回答
0

You may be able to find the /Count value in the file and just parse that out without using an external library. It'll look like /Count 5 if the PDF has 5 pages.

I don't know how robust that solution is, but it's definitely worth trying before adding/learning another library.

于 2014-03-28T03:54:54.207 回答
0

您需要一个 PDF 库来解析 PDF 并确定它包含的页数。.NET有几个免费的

于 2014-03-28T03:41:02.273 回答
0

在客户端上传之前,我使用 PDFjs 来评估 PDF。非常适合获取页数、页面大小....尽管加载 PDFjs 的负载非常重

http://www.pdfcharts.com/lab/analyze.html

于 2016-03-18T14:14:43.437 回答