1. Create a new project, quote
Create a new Winform project, download it, and reference it in the project.
2. Get PDF bookmarks
using ; using System; using ; // Recursive function, used to obtain all subbookmarks under the specified bookmark and maintain the structureList<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 (("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"]); (subBookmarkWithChildren); } } } } return result; } // Load PDF filePdfReader reader = new PdfReader("your_pdf_file_path.pdf"); // Get the directory information of the PDFList<Dictionary<string, object>> bookmarks = (reader); // Get all subbookmarks under the first bookmark and keep the structurestring parentTitle = (string)bookmarks[0]["Title"]; List<Dictionary<string, object>> allSubBookmarks = GetAllSubBookmarks(bookmarks, parentTitle); // Output all subbookmarksforeach (var subBookmark in allSubBookmarks) { ("Sub-Title: " + subBookmark["Title"] + ", Page: " + subBookmark["Page"]); if (("Kids")) { foreach (var childBookmark in (List<Dictionary<string, object>>)subBookmark["Kids"]) { (" Child Title: " + childBookmark["Title"] + ", Page: " + childBookmark["Page"]); } } } // Close PDF reader();
-
Define recursive function GetAllSubBookmarks:
- This function recursively obtains all subbookmarks under the specified bookmark and maintains the structure. It accepts two parameters: the bookmark list and the title of the parent bookmark.
- The function first creates an empty result list result for storing subbookmark information.
- Then iterate through each bookmark in the bookmark list and continue processing the bookmark if the title of the bookmark matches the specified parent title.
- If the bookmark contains a subbookmark (i.e., with the "Kids" key), the GetAllSubBookmarks function is called recursively to get the subbookmarks and add the subbookmark information to the subbookmark list of the current bookmark.
- Finally, add the current bookmark and its subbookmark information to the result list and finally return the result list.
-
Load PDF files and get directory information:
- Use the PdfReader class to load the specified PDF file.
- Use the (reader) method to get the directory information of the PDF file and store it in the bookmarks list.
-
Get all subbookmarks under the first bookmark:
- Get the title of the first bookmark from the directory information, and then call the GetAllSubBookmarks function to get all subbookmarks under the bookmark and store the result in the allSubBookmarks list.
-
Output all subbookmarks:
- Iterate through the allSubBookmarks list and output the title and page number information of each subbookmark.
- If the subbookmark contains a subbookmark (i.e., with the "Kids" key), continue to traverse and output the title and page information of each subbookmark.
-
Close PDF reader:
- Use the () method to close the PDF file reader.
3. Expansion:
C# Merge multiple PDFs using iTextSharp
The page sizes of multiple PDFs should be the same
/// <summary> /// Merge multiple pdfs/// </summary> /// <param name="fileList">pdf file path collection</param>/// <param name="outPath">The output directory of the final pdf</param>/// <param name="width">pdf page width, mm</param>/// <param name="height">pdf page height, mm</param>public static void mergePDF(List<string> fileList, string outPath, float width, float height) { width = (float)(width * 2.83462677);//There is a difference between the mm and the actual mm in PDF height = (float)(height * 2.83462677); reader; document = new (new (0, 0, width, height), 0, 0, 0, 0); using (FileStream fs = new FileStream(outPath, )) { using ( writer = (document, fs)) { (); cb = ; newPage; for (int i = 0; i < ; i++) { using (reader = new (fileList[i])) { int iPageNum = ; for (int j = 1; j <= iPageNum; j++) { (); newPage = (reader, j); (newPage, 0, 0); } } } (); } } }
C# Create a new PDF file using itextsharp
1. Download and quote itextsharp
Referenced in C# project.
2. Create a new PDF file code
Create a new file and write some content in the current path
using ; using ; // Create a Document objectDocument doc = new Document(); // Create a PdfWriter object and write the document content into the output streamPdfWriter writer = (doc, new FileStream("", )); // Open the document for writing(); // Set font and font sizeBaseFont bfChinese = (, , ); Font bfChineseFont = new Font(bfChinese, 14); // Create the paragraph text to be addedstring rowInfo = "This is a test paragraph"; Paragraph paragInfo = new Paragraph(rowInfo, bfChineseFont); (paragInfo ); // Add the write information to the document // Get the width occupied by the paragraph// float columnWidth = (, , , paragInfo, Element.ALIGN_LEFT); // Calculate the distance between left and right page margins// float marginDistance = columnWidth / 2; // Assuming the left and right margins are equal, so take half of the width as the distance// ("Distance between left and right margins: " + marginDistance + "pixel"); // Close document flow and free resources();
The above is the detailed operation method of using iTextSharp to obtain PDF file bookmark information. For more information about C# iTextSharp to obtain PDF bookmark information, please pay attention to my other related articles!