C#使用iTextSharp獲取PDF文件書簽信息的操作方法
更新時間:2024年04月09日 09:56:59 作者:JosieBook
C# iTextSharp是一個用于處理PDF文件的源庫,它提供了一系列的功能,包括創(chuàng)建PDF文件,以及提取和操作PDF文件中的內容,本文給大家介紹了C#使用iTextSharp獲取PDF文件書簽信息的操作方法,需要的朋友可以參考下
一、新建項目,引用iTextSharp.dll
新建Winform項目,并且下載iTextSharp.dll,并在項目中引用。
二、獲取PDF的書簽
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
// 遞歸函數(shù),用于獲取指定書簽下的所有子書簽并保持結構
List<Dictionary<string, object>> GetAllSubBookmarks(List<Dictionary<string, object>> bookmarks, string parentTitle)
{
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
foreach (var bookmark in bookmarks)
{
string title = (string)bookmark["Title"];
if (title == parentTitle)
{
if (bookmark.ContainsKey("Kids"))
{
List<Dictionary<string, object>> kids = (List<Dictionary<string, object>>)bookmark["Kids"];
foreach (var subBookmark in kids)
{
Dictionary<string, object> subBookmarkWithChildren = new Dictionary<string, object>();
subBookmarkWithChildren["Title"] = subBookmark["Title"];
subBookmarkWithChildren["Page"] = subBookmark["Page"];
subBookmarkWithChildren["Kids"] = GetAllSubBookmarks(kids, (string)subBookmark["Title"]);
result.Add(subBookmarkWithChildren);
}
}
}
}
return result;
}
// 加載PDF文件
PdfReader reader = new PdfReader("your_pdf_file_path.pdf");
// 獲取PDF的目錄信息
List<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
// 獲取第一個書簽下的所有子書簽并保持結構
string parentTitle = (string)bookmarks[0]["Title"];
List<Dictionary<string, object>> allSubBookmarks = GetAllSubBookmarks(bookmarks, parentTitle);
// 輸出所有子書簽
foreach (var subBookmark in allSubBookmarks)
{
Console.WriteLine("Sub-Title: " + subBookmark["Title"] + ", Page: " + subBookmark["Page"]);
if (subBookmark.ContainsKey("Kids"))
{
foreach (var childBookmark in (List<Dictionary<string, object>>)subBookmark["Kids"])
{
Console.WriteLine(" Child Title: " + childBookmark["Title"] + ", Page: " + childBookmark["Page"]);
}
}
}
// 關閉PDF閱讀器
reader.Close();
定義遞歸函數(shù) GetAllSubBookmarks :
- 這個函數(shù)通過遞歸方式獲取指定書簽下的所有子書簽并保持結構。它接受兩個參數(shù):書簽列表和父書簽的標題。
- 函數(shù)首先創(chuàng)建一個空的結果列表 result 用于存儲子書簽信息。
- 然后遍歷書簽列表中的每個書簽,如果書簽的標題與指定的父標題匹配,則繼續(xù)處理該書簽。
- 如果該書簽包含子書簽(即有 “Kids” 鍵),則遞歸調用 GetAllSubBookmarks 函數(shù)來獲取子書簽,并將子書簽信息添加到當前書簽的子書簽列表中。
- 最后,將當前書簽及其子書簽信息添加到結果列表中,并最終返回結果列表。
加載PDF文件和獲取目錄信息:
- 使用 PdfReader 類加載指定的PDF文件。
- 使用 SimpleBookmark.GetBookmark(reader) 方法獲取PDF文件的目錄信息,并將其存儲在 bookmarks 列表中。
獲取第一個書簽下的所有子書簽:
- 從目錄信息中獲取第一個書簽的標題,然后調用 GetAllSubBookmarks 函數(shù)來獲取該書簽下的所有子書簽,并將結果存儲在 allSubBookmarks 列表中。
輸出所有子書簽:
- 遍歷 allSubBookmarks 列表,輸出每個子書簽的標題和頁碼信息。
- 如果子書簽包含子書簽(即有 “Kids” 鍵),則繼續(xù)遍歷并輸出每個子書簽的標題和頁碼信息。
關閉PDF閱讀器:
- 使用 reader.Close() 方法關閉PDF文件閱讀器。
三、拓展:
C#使用iTextSharp合并多個PDF
多個PDF的頁大小要一致
/// <summary>
/// 合并多個pdf
/// </summary>
/// <param name="fileList">pdf文件路徑集合</param>
/// <param name="outPath">最終pdf的輸出目錄</param>
/// <param name="width">pdf頁寬,mm</param>
/// <param name="height">pdf頁高,mm</param>
public static void 合并PDF(List<string> fileList, string outPath, float width, float height) {
width = (float)(width * 2.83462677);//PDF的mm與實際mm有所區(qū)別
height = (float)(height * 2.83462677);
iTextSharp.text.pdf.PdfReader reader;
iTextSharp.text.Document document = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(0, 0, width, height), 0, 0, 0, 0);
using (FileStream fs = new FileStream(outPath, FileMode.Create)) {
using (iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fs)) {
document.Open();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage newPage;
for (int i = 0; i < fileList.Count; i++) {
using (reader = new iTextSharp.text.pdf.PdfReader(fileList[i])) {
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++) {
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, 0, 0);
}
}
}
document.Dispose();
}
}
}
C#使用itextsharp新建PDF文件
一、下載并引用itextsharp
itextsharp.dll在C#項目中引用。
二、新建PDF文件代碼
在當前路徑下新建output.pdf文件并寫入一些內容
using iTextSharp.text;
using iTextSharp.text.pdf;
// 創(chuàng)建一個Document對象
Document doc = new Document();
// 創(chuàng)建PdfWriter對象,將文檔內容寫入輸出流中
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("output.pdf", FileMode.Create));
// 打開文檔進行寫入操作
doc.Open();
// 設置字體和字號
BaseFont bfChinese = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);
Font bfChineseFont = new Font(bfChinese, 14);
// 創(chuàng)建要添加的段落文本
string rowInfo = "這是一個測試段落";
Paragraph paragInfo = new Paragraph(rowInfo, bfChineseFont);
doc.Add(paragInfo ); // 將寫入信息加入到文檔中
// 獲取段落所占的寬度
// float columnWidth = ColumnText.GetColumnWidth(doc.PageSize.Width, doc.Top, doc.Bottom, paragInfo, Element.ALIGN_LEFT);
// 計算左右頁邊距之間的距離
// float marginDistance = columnWidth / 2; // 假設左右頁邊距相等,所以取寬度的一半作為距離
// Console.WriteLine("左右頁邊距之間的距離: " + marginDistance + "像素");
// 關閉文檔流和釋放資源
doc.Close();
以上就是C#使用iTextSharp獲取PDF文件書簽信息的操作方法的詳細內容,更多關于C# iTextSharp獲取PDF書簽信息的資料請關注腳本之家其它相關文章!

