如何找出用户在C#中的OpenFileDialog中选择的pdf文件的页数?

jaf*_*aqi 0 c# openfiledialog filestream dialogresult

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

Joa*_*tos 8

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

伊文夏普

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

private static int getNumberOfPdfPages(string pathDocument)
{
    return new iTextSharp.text.pdf.PdfReader(pathDocument).NumberOfPages;
}
Run Code Online (Sandbox Code Playgroud)

PDF库

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;
}
Run Code Online (Sandbox Code Playgroud)

PDF夏普

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

流媒体阅读器

不需要外部库

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;
    }
}
Run Code Online (Sandbox Code Playgroud)