PDFファイルは目次ではなくブックマークを使用していることが判明しました。
ブックマークで機能するソリューションを次に示します: 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);
}