如何在保留书签的同时向现有pdf添加其他页面?(PDFSharp等)

Dot*_*per 7 .net pdf-generation bookmarks pdfsharp

我有一个PDF,我想添加一个额外的页面,理想情况下作为第一页.我已经能够用PDFSharp实现这一点,但问题是原始PDF包含书签,我想维护.使用PDFSharp似乎删除书签,或者至少我不知道使用包含附加页面的新创建的PDF保存原始TOC的任何选项或命令.

有没有人知道如何保持TOC与PDFSharp或任何其他.NET库,理想是免费的,这将允许我添加页面到现有的PDF并维护其书签?(我知道添加页面作为第一页会使页面引用无效,这就是为什么在最后一页添加页面也没问题.)

谢谢大家!

Je *_*not 6

原来,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);
}
Run Code Online (Sandbox Code Playgroud)