加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 内容表正确打印PDF但不适用于RTF

发布时间:2020-12-15 22:15:43 所属栏目:百科 来源:网络整理
导读:我正在开发一个项目,我需要创建一个包含Table Of Content的PDF文件和RTF文件.我是使用MigraDoc PdfSharp库为C#做的. 这两个文件的内容表代码是: public static void DefineTableOfContents(Document document) { Section section = document.LastSection; s
我正在开发一个项目,我需要创建一个包含Table Of Content的PDF文件和RTF文件.我是使用MigraDoc PdfSharp库为C#做的.

这两个文件的内容表代码是:

public static void DefineTableOfContents(Document document)
    {
        Section section = document.LastSection;

        section.AddPageBreak();
        Paragraph paragraph = section.AddParagraph("Table of Contents");
        paragraph.Format.Font.Size = 14;
        paragraph.Format.Font.Bold = true;
        paragraph.Format.SpaceAfter = 24;
        paragraph.Format.OutlineLevel = OutlineLevel.Level1;

        paragraph = section.AddParagraph(); 
        paragraph.Style = "TOC";
        Hyperlink hyperlink = paragraph.AddHyperlink("ParaBookmark");
        hyperlink.AddText("Paragraphst");
        hyperlink.AddPageRefField("ParaBookmark");

        paragraph = section.AddParagraph();
        paragraph.Style = "TOC";
        hyperlink = paragraph.AddHyperlink("AJBookmark");
        hyperlink.AddText("AJt");
        hyperlink.AddPageRefField("AJBookmark");

        paragraph = section.AddParagraph();
        paragraph.Style = "TOC";
        hyperlink = paragraph.AddHyperlink("TablesBookmark");
        hyperlink.AddText("Tablest");
        hyperlink.AddPageRefField("TablesBookmark");

        paragraph = section.AddParagraph();
        paragraph.Style = "TOC";
        hyperlink = paragraph.AddHyperlink("ChartsBookmark"); 
        hyperlink.AddText("Chartst");
        hyperlink.AddPageRefField("ChartsBookmark"); 
    }

对于Pdf,代码工作正常,所有页码都正确显示,但对于RTF文件,我们得到如下输出:

Table of Contents
Paragraphs............................. < Please update this field. >
AJ..................................... < Please update this field. >
Tables................................. < Please update this field. >
Charts................................. < Please update this field. >

谷歌搜索后,我开始明白,为了使RTF的页码显示在TOC上,我们必须在MS Word中手动更新整个文档,方法是使用ctrl A然后使用F9.

是否有任何编程方式,以便我可以获得RTF页码的正确内容表,以便我们不需要手动更新文档?

解决方法

可能有几种方法,比如VBA for Word或Word Add-In. MigraDoc无法填补这些字段.

根据this thread,当Word打开RTF时,无法自动更新字段.因此,这将是RTF文件创建和发送给客户之间的额外步骤.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读