一、新建项目,引用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书签信息的资料请关注代码网其它相关文章!
发表评论