内容列表正确打印PDF但不适用于RTF

时间:2015-06-15 14:06:57

标签: c# pdfsharp migradoc

我正在开发一个项目,我需要创建一个包含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("Paragraphs\t");
        hyperlink.AddPageRefField("ParaBookmark");

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

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

        paragraph = section.AddParagraph();
        paragraph.Style = "TOC";
        hyperlink = paragraph.AddHyperlink("ChartsBookmark"); 
        hyperlink.AddText("Charts\t");
        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的页码获取正确的内容表,以便我们不需要手动更新文档?

1 个答案:

答案 0 :(得分:1)

Probably there are several ways, like VBA for Word or a Word Add-In that does it. MigraDoc cannot fill those fields.

According to this thread there is no way to have the fields being updated automatically when Word opens the RTF. So this would be an extra step between RTF file creation and sending it to the customer.