spire.pdf不仅支持,还支持多级书签。本文将介绍如何使用spire.pdf来获取pdf文档中的多级书签。
c#
static void main(string[] args)
{
//加载含有多级书签的pdf文件
pdfdocument doc = new pdfdocument();
doc.loadfromfile("示例.pdf");
//获取文档的书签
pdfbookmarkcollection bookmarks = doc.bookmarks;
//声明一个可变字符串
stringbuilder stringbuilder = new stringbuilder();
//获取父书签和子书签
getbookmarktitle(bookmarks, stringbuilder);
//声明txt文件,并将获得的多级书签写入到文件.txt
string filename = "d://文件.txt";
file.writealltext(filename, stringbuilder.tostring());
console.readline();
}
static void getbookmarktitle(pdfbookmarkcollection bookmarks, stringbuilder stringbuilder)
{
if (bookmarks.count > 0)
{
foreach (pdfbookmark parentbookmark in bookmarks)
{
stringbuilder.appendline(parentbookmark.title);
//递归文档多级书签
getbookmarktitle(parentbookmark, stringbuilder);
}
}
}
vb.net
private shared sub main(args as string())
'加载含有多级书签的pdf文件
dim doc as new pdfdocument()
doc.loadfromfile("示例.pdf")
'获取文档的书签
dim bookmarks as pdfbookmarkcollection = doc.bookmarks
'声明一个可变字符串
dim stringbuilder as new stringbuilder()
'获取父书签和子书签
getbookmarktitle(bookmarks, stringbuilder)
'声明txt文件,并将获得的多级书签写入到文件.txt
dim filename as [string] = "d://文件.txt"
file.writealltext(filename, stringbuilder.tostring())
console.readline()
end sub
private shared sub getbookmarktitle(bookmarks as pdfbookmarkcollection, stringbuilder as stringbuilder)
if bookmarks.count > 0 then
for each parentbookmark as pdfbookmark in bookmarks
stringbuilder.appendline(parentbookmark.title)
'递归文档多级书签
getbookmarktitle(parentbookmark, stringbuilder)
next
end if
end sub