我正在开发一个项目,我需要创建一个包含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页码的正确内容表,以便我们不需要手动更新文档?
解决方法:
可能有几种方法,比如VBA for Word或Word Add-In. MigraDoc无法填补这些字段.
根据this thread,当Word打开RTF时,无法自动更新字段.因此,这将是RTF文件创建和发送给客户之间的额外步骤.