对于大型pdf文件,使用目录可以让文档更容易访问。通常,在pdf文档中目录被放在第一个页面。使用spire.pdf, 我们可以添加新的空白页到现有的pdf文件中,然后再创建目录。下面我们将通过详细的代码来实现。
c#
//加载pdf示例文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("spire.pdf");
//获取pdf页数
int pagecount = doc.pages.count;
//在第一页插入空白页
pdfpagebase tocpage = doc.pages.insert(0);
//添加标题,并设置字体、样式和位置
string title = "目录";
pdftruetypefont titlefont = new pdftruetypefont(new font("宋体", 16, fontstyle.bold),true);
pdfstringformat centeralignment = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
pointf location = new pointf(tocpage.canvas.clientsize.width / 2, titlefont.measurestring(title).height);
tocpage.canvas.drawstring(title, titlefont, pdfbrushes.black, location, centeralignment);
//设置目录文本内容
pdftruetypefont titlesfont = new pdftruetypefont(new font("宋体", 10),true);
string[] titles = new string[pagecount];
for (int i = 0; i < titles.length; i )
{
titles[i] = string.format("文件第{0}页", i 1);
}
float y = titlefont.measurestring(title).height 10;
float x = 0;
for (int i = 1; i <= pagecount; i )
{
string text = titles[i - 1];
sizef titlesize = titlesfont.measurestring(text);
pdfpagebase navigatedpage = doc.pages[i];
string pagenumtext = (i 1).tostring();
sizef pagenumtextsize = titlesfont.measurestring(pagenumtext);
tocpage.canvas.drawstring(text, titlesfont, pdfbrushes.black, 0, y);
float dotlocation = titlesize.width 2 x;
float pagenumlocation = tocpage.canvas.clientsize.width - pagenumtextsize.width;
for (float j = dotlocation; j < pagenumlocation; j )
{
if (dotlocation >= pagenumlocation)
{
break;
}
tocpage.canvas.drawstring(".", titlesfont, pdfbrushes.black, dotlocation, y);
dotlocation = 2;
}
tocpage.canvas.drawstring(pagenumtext, titlesfont, pdfbrushes.black, pagenumlocation, y);
//添加动作
location = new pointf(0, y);
rectanglef titlebounds = new rectanglef(location, new sizef(tocpage.canvas.clientsize.width, titlesize.height));
pdfdestination dest = new pdfdestination(navigatedpage, new pointf(-doc.pagesettings.margins.top, -doc.pagesettings.margins.left));
pdfactionannotation action = new pdfactionannotation(titlebounds, new pdfgotoaction(dest));
action.border = new pdfannotationborder(0);
(tocpage as pdfnewpage).annotations.add(action);
y = titlesize.height 10;
//保存已添加目录文档
string output = "目录文档.pdf";
doc.savetofile(output, fileformat.pdf);
system.diagnostics.process.start("目录文档.pdf");
}
vb.net
'加载pdf示例文档
dim doc as new pdfdocument()
doc.loadfromfile("spire.pdf")
'获取pdf页数
dim pagecount as integer = doc.pages.count
'在第一页插入空白页
dim tocpage as pdfpagebase = doc.pages.insert(0)
'添加标题,并设置字体、样式和位置
dim title as string = "目录"
dim titlefont as new pdftruetypefont(new font("宋体", 16, fontstyle.bold), true)
dim centeralignment as new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle)
dim location as new pointf(tocpage.canvas.clientsize.width / 2, titlefont.measurestring(title).height)
tocpage.canvas.drawstring(title, titlefont, pdfbrushes.black, location, centeralignment)
'设置目录文本内容
dim titlesfont as new pdftruetypefont(new font("宋体", 10), true)
dim titles as [string]() = new [string](pagecount - 1) {}
for i as integer = 0 to titles.length - 1
titles(i) = string.format("文件第{0}页", i 1)
next
dim y as single = titlefont.measurestring(title).height 10
dim x as single = 0
for i as integer = 1 to pagecount
dim text as string = titles(i - 1)
dim titlesize as sizef = titlesfont.measurestring(text)
dim navigatedpage as pdfpagebase = doc.pages(i)
dim pagenumtext as string = (i 1).tostring()
dim pagenumtextsize as sizef = titlesfont.measurestring(pagenumtext)
tocpage.canvas.drawstring(text, titlesfont, pdfbrushes.black, 0, y)
dim dotlocation as single = titlesize.width 2 x
dim pagenumlocation as single = tocpage.canvas.clientsize.width - pagenumtextsize.width
for j as single = dotlocation to pagenumlocation - 1
if dotlocation >= pagenumlocation then
exit for
end if
tocpage.canvas.drawstring(".", titlesfont, pdfbrushes.black, dotlocation, y)
dotlocation = 2
next
tocpage.canvas.drawstring(pagenumtext, titlesfont, pdfbrushes.black, pagenumlocation, y)
'添加动作
location = new pointf(0, y)
dim titlebounds as new rectanglef(location, new sizef(tocpage.canvas.clientsize.width, titlesize.height))
dim dest as new pdfdestination(navigatedpage, new pointf(-doc.pagesettings.margins.top, -doc.pagesettings.margins.left))
dim action as new pdfactionannotation(titlebounds, new pdfgotoaction(dest))
action.border = new pdfannotationborder(0)
trycast(tocpage, pdfnewpage).annotations.add(action)
y = titlesize.height 10
'保存已添加目录文档
dim output as string = "目录文档.pdf"
doc.savetofile(output, fileformat.pdf)
system.diagnostics.process.start("目录文档.pdf")
next
截图: