C#代码实现获取PDF文件的页数
作者:2501_93070778
计算 PDF 文件的页数在许多场景中都十分重要,本文将介绍如何使用 C#,借助 Spire.PDF for .NET 获取 PDF 文件的页数,有需要的小伙伴可以了解下
计算 PDF 文件的页数在许多场景中都十分重要,例如确定文档长度、整理内容结构以及评估打印需求。除了通过 PDF 阅读器查看页数信息外,你还可以通过编程方式自动完成这一任务。本文将介绍如何使用 C#,借助 Spire.PDF for .NET 获取 PDF 文件的页数。
安装 Spire.PDF for .NET
首先,你需要在 .NET 项目中添加 Spire.PDF for .NET 软件包中包含的 DLL 文件作为引用。这些 DLL 文件可以通过指定链接下载,或者通过 NuGet 进行安装。
PM> Install-Package Spire.PDF
在 C# 中获取 PDF 文件的页数
Spire.PDF for .NET 提供了 PdfDocument.Pages.Count 属性,无需打开 PDF 文件即可快速统计其页数。具体步骤如下:
- 创建一个 PdfDocument 对象。
- 使用 PdfDocument.LoadFromFile() 方法加载示例 PDF 文件。
- 通过 PdfDocument.Pages.Count 属性统计 PDF 文件的页数。
- 输出结果并关闭 PDF 文档。
示例代码如下:
using Spire.Pdf;
namespace GetNumberOfPages
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument pdf = new PdfDocument();
// 加载示例 PDF 文件
pdf.LoadFromFile("Contract.pdf");
// 获取 PDF 文件的页数
int PageNumber = pdf.Pages.Count;
Console.WriteLine("该 PDF 文件共有 {0} 页", PageNumber);
// 关闭 PDF 文档
pdf.Close();
}
}
}知识扩展
C#判断pdf文档的页数
/// <summary>
/// 获取pdf文档的页数
/// </summary>
/// <param name="filePath"></param>
/// <returns>-1表示文件不存在</returns>
public static int GetPDFofPageCount(string filePath)
{
int count = -1;//-1表示文件不存在
if (File.Exists(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
StreamReader reader = new StreamReader(fs);
//从流的当前位置到末尾读取流
string pdfText = reader.ReadToEnd();
Regex rgx = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = rgx.Matches(pdfText);
count = matches.Count;
}
}
return count;
}C#获取pdf文档的页数
/// <summary>
/// 获取pdf文档的页数
/// </summary>
/// <param name="filePath"></param>
/// <returns>-1表示文件不存在</returns>
public static int GetPDFofPageCount(string filePath)
{
int count = -1;//-1表示文件不存在
if (File.Exists(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
StreamReader reader = new StreamReader(fs);
//从流的当前位置到末尾读取流
string pdfText = reader.ReadToEnd();
Regex rgx = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = rgx.Matches(pdfText);
count = matches.Count;
}
}
return count;
}使用iTextSharp获取PDF文件的总页数
public class pdfPage : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
iTextSharp.text.Rectangle page = doc.PageSize;
//PdfPTable EndTable = new PdfPTable(2);
PdfPTable EndTable = new PdfPTable(2);
EndTable.DefaultCell.Padding = 2f;
EndTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
iTextSharp.text.Font smallfont2 = FontFactory.GetFont(FontFactory.HELVETICA, "CP1250", 10);
PdfPCell stopka1 = new PdfPCell(new Phrase("Left column - not important", smallfont2));
stopka1.BorderWidthLeft = 0;
stopka1.BorderWidthBottom = 0;
stopka1.BorderWidthRight = 0;
stopka1.HorizontalAlignment = Element.ALIGN_LEFT;
stopka1.VerticalAlignment = Element.ALIGN_MIDDLE;
PdfPCell stopka2 = new PdfPCell(new Phrase("Page " + doc.PageNumber + "/", smallfont2));
stopka2.BorderWidthLeft = 0;
stopka2.BorderWidthBottom = 0;
stopka2.BorderWidthRight = 0;
stopka2.HorizontalAlignment = Element.ALIGN_RIGHT;
stopka2.VerticalAlignment = Element.ALIGN_MIDDLE;
EndTable.AddCell(stopka1);
EndTable.AddCell(stopka2);
EndTable.TotalWidth = page.Width - doc.LeftMargin - doc.RightMargin;
EndTable.WriteSelectedRows(0, -1, doc.LeftMargin, EndTable.TotalHeight + doc.BottomMargin - 45, writer.DirectContent);
}
} 到此这篇关于C#代码实现获取PDF文件的页数的文章就介绍到这了,更多相关C#获取PDF页数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
