确定PDF文件中的页数 [英] Determine number of pages in a PDF file
问题描述
我需要使用C#代码(.NET 2.0)确定指定PDF文件中的页数.将从文件系统而不是从URL中读取PDF文件.有没有人对如何做到这一点有任何指示?注意:Adobe Acrobat Reader安装在将执行此检查的PC上.
I need to determine the number of pages in a specified PDF file using C# code (.NET 2.0). The PDF file will be read from the file system, and not from a URL. Does anyone have any pointers on how this could be done? Note: Adobe Acrobat Reader is installed on the PC where this check will be carried out.
推荐答案
您将需要用于C#的PDF API.尽管可能存在更好的API,但iTextSharp是一种可能的API.
You'll need a PDF API for C#. iTextSharp is one possible API, though better ones might exist.
iTextSharp示例
您必须安装iTextSharp.dll作为参考.从SourceForge.net下载iTextsharp.这是使用控制台应用程序的完整工作程序.
You must install iTextSharp.dll as a reference. Download iTextsharp from SourceForge.net This is a complete working program using a console application.
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();
}
}
}
这篇关于确定PDF文件中的页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!