事实证明,PDF 文件使用书签,而不是 TOC。
此处显示了适用于书签的解决方案:http:
//forum.pdfsharp.net/viewtopic.php?p=6660#p6660
打开现有文件进行修改,在文档开头插入一个新页面 - 所有书签仍然有效。
这是代码片段:
static void Main(string[] args)
{
const string filename = "sample.pdf";
File.Copy(Path.Combine("D:\\PDFsharp\\PDFfiles\\sample\\", filename),
Path.Combine(Directory.GetCurrentDirectory(), filename), true);
// Open an existing document for editing and loop through its pages
PdfDocument document = PdfReader.Open(filename);
var newPage = document.InsertPage(0);
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(newPage);
// Create a font
XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, newPage.Width, newPage.Height),
XStringFormats.Center);
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}