TOC prints correctly for PDF, but not RTF
I am working on a project where I need to create a PDF and RTF file with a table of contents. I am doing this using MigraDoc + PdfSharp library for C #.
Code for the table of contents for both files:
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");
}
For Pdf, the code works fine, all page numbers are displayed correctly, but for RTF file we get output like:
Table of Contents
Paragraphs............................. < Please update this field. >
AJ..................................... < Please update this field. >
Tables................................. < Please update this field. >
Charts................................. < Please update this field. >
After googling, I figured out that for RTF page numbers displayed in TOC, we would need to manually update the entire document in MS Word using ctrl + A and then F9.
Is there any programmatic way so that I can get the correct table of contents with page numbers for RTF so that we don't have to manually update the document?
source to share
There are probably several ways, such as VBA for Word or a Word add-in that does this. MigraDoc cannot fill in these fields.
According to this thread , there is no way to automatically update fields when Word opens RTF. So this will be an extra step between generating the RTF file and sending it to the client.
source to share