【问题标题】:How to add an additional page to existing pdf while keeping bookmarks? (PDFSharp etc.)如何在保留书签的同时向现有 pdf 添加附加页面? (PDFSharp等)
【发布时间】:2013-01-04 14:21:30
【问题描述】:

我有一个 PDF,我想在其中添加一个附加页面,最好是作为第一页。我已经能够使用 PDFSharp 实现这一点,但问题是原始 PDF 包含我想维护的书签。使用 PDFSharp 似乎会删除书签,或者至少我不知道有任何选项或命令可以将原始 TOC 与包含附加页面的新创建的 PDF 一起保存。

是否有人知道如何将 TOC 与 PDFSharp 或任何其他 .NET 库(最好是免费库)一起保存,这样我就可以将页面添加到现有 PDF 并维护其书签? (我知道添加一个页面作为第一页会使页面引用无效,这就是为什么添加一个页面作为最后一页也可以。)

谢谢大家!

【问题讨论】:

    标签: .net pdf-generation bookmarks pdfsharp


    【解决方案1】:

    原来 PDF 文件使用书签,而不是目录。

    此处显示了一个适用于书签的解决方案:
    http://forum.pdfsharp.net/viewtopic.php?p=6660#p6660

    打开现有文件进行修改,在文档开头插入一个新页面 - 所有书签仍然有效。

    这里是sn-p的代码:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2013-11-18
      相关资源
      最近更新 更多