确定 PDF 文件中的页数 [英] Determine number of pages in a PDF file
问题描述
我需要使用 C# 代码 (.NET 2.0) 确定指定 PDF 文件中的页数.PDF 文件将从文件系统中读取,而不是从 URL 中读取.有没有人知道如何做到这一点?注意:进行此项检查的 PC 上已安装 Adobe Acrobat Reader.
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 an URL. Does anyone have any idea 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.iTextSharp 是一种可能的 API,但可能存在更好的 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屋!