目录
- 一、新建项目,引用iTextSharp.dll
- 二、获取PDF的书签
- 三、拓展:
- C#使用iTextSharp合并多个PDF
- C#使用itextsharp新建PDF文件
一、新建项目,引用iTextSharp.dll
新建Winform项目,并且下载iTextSharp.dll,并在项目中引用。
二、获取PDF的书签
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
// 递归函数,用于获取指定书签下的所有子书签并保持结构
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();
-
定义递归函数 GetAllSubBookmarks :
- 这个函数通过递归方式获取指定书签下的所有子书签并保持结构。它接受两个参数:书签列表和父书签的标题。
- 函数首先创建一个空的结果列表 result 用于存储子书签信息。
- 然后遍历书签列表中的每个书签,如果书签的标题与指定的父标题匹配,则继续处理该书签。
- 如果该书签包含子书签(即有 “Kids” 键),则递归调用 GetAllSubBookmarks 函数来获取子书签,并将子书签信息添加到当前书签的子书签列表中。
- 最后,将当前书签及其子书签信息添加到结果列表中,并最终返回结果列表。
-
加载PDF文件和获取目录信息:
- 使用 PdfReader 类加载指定的PDF文件。
- 使用 SimpleBookmark.GetBookmark(reader) 方法获取PDF文件的目录信息,并将其存储在 bookmarks 列表中。
-
获取第一个书签下的所有子书签:
- 从目录信息中获取第一个书签的标题,然后调用 GetAllSubBookmarks 函数来获取该书签下的所有子书签,并将结果存储在 allSubBookmarks 列表中。
-
输出所有子书签:
- 遍历 allSubBookmarks 列表,输出每个子书签的标题和页码信息。
- 如果子书签包含子书签(即有 “Kids” 键),则继续遍历并输出每个子书签的标题和页码信息。
-
关闭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有所区别
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;
// 创建一个Document对象
Document doc = new Document();
// 创建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);
// 创建要添加的段落文本
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书签信息的资料请关注其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)