C#使用iTextSharp獲取PDF文件書簽信息的操作方法
一、新建項(xiàng)目,引用iTextSharp.dll
新建Winform項(xiàng)目,并且下載iTextSharp.dll,并在項(xiàng)目中引用。
二、獲取PDF的書簽
using iTextSharp.text.pdf; using System; using System.Collections.Generic; // 遞歸函數(shù),用于獲取指定書簽下的所有子書簽并保持結(jié)構(gòu) 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); // 獲取第一個(gè)書簽下的所有子書簽并保持結(jié)構(gòu) 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"]); } } } // 關(guān)閉PDF閱讀器 reader.Close();
定義遞歸函數(shù) GetAllSubBookmarks :
- 這個(gè)函數(shù)通過遞歸方式獲取指定書簽下的所有子書簽并保持結(jié)構(gòu)。它接受兩個(gè)參數(shù):書簽列表和父書簽的標(biāo)題。
- 函數(shù)首先創(chuàng)建一個(gè)空的結(jié)果列表 result 用于存儲(chǔ)子書簽信息。
- 然后遍歷書簽列表中的每個(gè)書簽,如果書簽的標(biāo)題與指定的父標(biāo)題匹配,則繼續(xù)處理該書簽。
- 如果該書簽包含子書簽(即有 “Kids” 鍵),則遞歸調(diào)用 GetAllSubBookmarks 函數(shù)來獲取子書簽,并將子書簽信息添加到當(dāng)前書簽的子書簽列表中。
- 最后,將當(dāng)前書簽及其子書簽信息添加到結(jié)果列表中,并最終返回結(jié)果列表。
加載PDF文件和獲取目錄信息:
- 使用 PdfReader 類加載指定的PDF文件。
- 使用 SimpleBookmark.GetBookmark(reader) 方法獲取PDF文件的目錄信息,并將其存儲(chǔ)在 bookmarks 列表中。
獲取第一個(gè)書簽下的所有子書簽:
- 從目錄信息中獲取第一個(gè)書簽的標(biāo)題,然后調(diào)用 GetAllSubBookmarks 函數(shù)來獲取該書簽下的所有子書簽,并將結(jié)果存儲(chǔ)在 allSubBookmarks 列表中。
輸出所有子書簽:
- 遍歷 allSubBookmarks 列表,輸出每個(gè)子書簽的標(biāo)題和頁碼信息。
- 如果子書簽包含子書簽(即有 “Kids” 鍵),則繼續(xù)遍歷并輸出每個(gè)子書簽的標(biāo)題和頁碼信息。
關(guān)閉PDF閱讀器:
- 使用 reader.Close() 方法關(guān)閉PDF文件閱讀器。
三、拓展:
C#使用iTextSharp合并多個(gè)PDF
多個(gè)PDF的頁大小要一致
/// <summary> /// 合并多個(gè)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與實(shí)際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#項(xiàng)目中引用。
二、新建PDF文件代碼
在當(dāng)前路徑下新建output.pdf文件并寫入一些內(nèi)容
using iTextSharp.text; using iTextSharp.text.pdf; // 創(chuàng)建一個(gè)Document對象 Document doc = new Document(); // 創(chuàng)建PdfWriter對象,將文檔內(nèi)容寫入輸出流中 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("output.pdf", FileMode.Create)); // 打開文檔進(jìn)行寫入操作 doc.Open(); // 設(shè)置字體和字號(hào) BaseFont bfChinese = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED); Font bfChineseFont = new Font(bfChinese, 14); // 創(chuàng)建要添加的段落文本 string rowInfo = "這是一個(gè)測試段落"; Paragraph paragInfo = new Paragraph(rowInfo, bfChineseFont); doc.Add(paragInfo ); // 將寫入信息加入到文檔中 // 獲取段落所占的寬度 // float columnWidth = ColumnText.GetColumnWidth(doc.PageSize.Width, doc.Top, doc.Bottom, paragInfo, Element.ALIGN_LEFT); // 計(jì)算左右頁邊距之間的距離 // float marginDistance = columnWidth / 2; // 假設(shè)左右頁邊距相等,所以取寬度的一半作為距離 // Console.WriteLine("左右頁邊距之間的距離: " + marginDistance + "像素"); // 關(guān)閉文檔流和釋放資源 doc.Close();
以上就是C#使用iTextSharp獲取PDF文件書簽信息的操作方法的詳細(xì)內(nèi)容,更多關(guān)于C# iTextSharp獲取PDF書簽信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#利用deskew算法實(shí)現(xiàn)圖像文本傾斜校正
這篇文章主要為大家詳細(xì)介紹了C#如何利用deskew算法實(shí)現(xiàn)圖像文本傾斜校正,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01WPF實(shí)現(xiàn)繪制餅狀統(tǒng)計(jì)圖的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用WPF實(shí)現(xiàn)繪制簡單的餅狀統(tǒng)計(jì)圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10基于私鑰加密公鑰解密的RSA算法C#實(shí)現(xiàn)方法
這篇文章主要介紹了基于私鑰加密公鑰解密的RSA算法C#實(shí)現(xiàn)方法,是應(yīng)用非常廣泛,需要的朋友可以參考下2014-08-08