C#代碼實(shí)現(xiàn)獲取PDF文件的頁(yè)數(shù)
計(jì)算 PDF 文件的頁(yè)數(shù)在許多場(chǎng)景中都十分重要,例如確定文檔長(zhǎng)度、整理內(nèi)容結(jié)構(gòu)以及評(píng)估打印需求。除了通過(guò) PDF 閱讀器查看頁(yè)數(shù)信息外,你還可以通過(guò)編程方式自動(dòng)完成這一任務(wù)。本文將介紹如何使用 C#,借助 Spire.PDF for .NET 獲取 PDF 文件的頁(yè)數(shù)。
安裝 Spire.PDF for .NET
首先,你需要在 .NET 項(xiàng)目中添加 Spire.PDF for .NET 軟件包中包含的 DLL 文件作為引用。這些 DLL 文件可以通過(guò)指定鏈接下載,或者通過(guò) NuGet 進(jìn)行安裝。
PM> Install-Package Spire.PDF
在 C# 中獲取 PDF 文件的頁(yè)數(shù)
Spire.PDF for .NET 提供了 PdfDocument.Pages.Count 屬性,無(wú)需打開(kāi) PDF 文件即可快速統(tǒng)計(jì)其頁(yè)數(shù)。具體步驟如下:
- 創(chuàng)建一個(gè) PdfDocument 對(duì)象。
- 使用 PdfDocument.LoadFromFile() 方法加載示例 PDF 文件。
- 通過(guò) PdfDocument.Pages.Count 屬性統(tǒng)計(jì) PDF 文件的頁(yè)數(shù)。
- 輸出結(jié)果并關(guān)閉 PDF 文檔。
示例代碼如下:
using Spire.Pdf;
namespace GetNumberOfPages
{
class Program
{
static void Main(string[] args)
{
// 創(chuàng)建 PdfDocument 對(duì)象
PdfDocument pdf = new PdfDocument();
// 加載示例 PDF 文件
pdf.LoadFromFile("Contract.pdf");
// 獲取 PDF 文件的頁(yè)數(shù)
int PageNumber = pdf.Pages.Count;
Console.WriteLine("該 PDF 文件共有 {0} 頁(yè)", PageNumber);
// 關(guān)閉 PDF 文檔
pdf.Close();
}
}
}知識(shí)擴(kuò)展
C#判斷pdf文檔的頁(yè)數(shù)
/// <summary>
/// 獲取pdf文檔的頁(yè)數(shù)
/// </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);
//從流的當(dāng)前位置到末尾讀取流
string pdfText = reader.ReadToEnd();
Regex rgx = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = rgx.Matches(pdfText);
count = matches.Count;
}
}
return count;
}C#獲取pdf文檔的頁(yè)數(shù)
/// <summary>
/// 獲取pdf文檔的頁(yè)數(shù)
/// </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);
//從流的當(dāng)前位置到末尾讀取流
string pdfText = reader.ReadToEnd();
Regex rgx = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = rgx.Matches(pdfText);
count = matches.Count;
}
}
return count;
}使用iTextSharp獲取PDF文件的總頁(yè)數(shù)
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);
}
} 到此這篇關(guān)于C#代碼實(shí)現(xiàn)獲取PDF文件的頁(yè)數(shù)的文章就介紹到這了,更多相關(guān)C#獲取PDF頁(yè)數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#反射調(diào)用dll文件中的方法操作泛型與屬性字段
這篇文章介紹了C#反射調(diào)用dll文件中的方法操作泛型與屬性字段,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器
這篇文章主要介紹了C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器的相關(guān)代碼和使用方法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-07-07
C#類(lèi)的創(chuàng)建與初始化實(shí)例解析
這篇文章主要介紹了C#類(lèi)的創(chuàng)建與初始化實(shí)例解析,有助于初學(xué)者較為直觀(guān)的理解C#的類(lèi),需要的朋友可以參考下2014-07-07
C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
WPF使用DrawingContext實(shí)現(xiàn)繪制刻度條
這篇文章主要為大家詳細(xì)介紹了如何利用WPF DrawingContext實(shí)現(xiàn)繪制刻度條,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09

