我需要使用C#代码(.NET 2.0)确定指定PDF文件中的页数.PDF文件将从文件系统中读取,而不是从URL读取.有没有人对如何做到这一点有任何指示?注意:将在执行此检查的PC上安装Adobe Acrobat Reader.
dar*_*dog 69
你需要一个C#的PDF API.iTextSharp是一种可能的API,但可能存在更好的API.
iTextSharp示例
您必须安装iTextSharp.dll作为参考.从SourceForge.net下载iTextsharp这是一个使用控制台应用程序的完整工作程序.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
namespace GetPages_PDF
{
class Program
{
static void Main(string[] args)
{
// Right side of equation is location of YOUR pdf file
string ppath = "C:\\aworking\\Hawkins.pdf";
PdfReader pdfReader = new PdfReader(ppath);
int numberOfPages = pdfReader.NumberOfPages;
Console.WriteLine(numberOfPages);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Bar*_*ett 38
这应该做的伎俩:
public int getNumberOfPdfPages(string fileName)
{
using (StreamReader sr = new StreamReader(File.OpenRead(fileName)))
{
Regex regex = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());
return matches.Count;
}
}
Run Code Online (Sandbox Code Playgroud)