Hi Team,
I have requirement to show the header field in the word TOC.
how can i get the page header field in the word TOC.
please check the attachement.
in that file i want to customize the ‘Contents’ name, and need to display the header field (HI) in the TOC.
can you please check this
Thanks
Sai Lanka
Hi Sai Lanka,
Thanks for your inquiry. As per my understanding, you want to replace “Contents” word with “Hi” text from the header of document. In this case, I suggest you please bookmark the text ‘Hi’ and insert Ref field ({ REF bookmarkname }) at the position of text “Contents”.
Please use DocumentBuilder.InsertField method to insert a Word field into a document and optionally updates the field result.
Hope this helps you. Please let us know if you have any more queries.
Thanks Tahir,
how can i include the header field in to my TOC, in last example, (HI) was the header field, i want to show that header field into TOC.
example:
Table Of Contents:
1)Hi
2)heading1
2.1) heading 2
2.2) heading2
3) heading 1
3.1)…
.
.
.
.
like this…
can you please help me
thanks
sai lanka
Hi Sai Lanka,
Thanks for sharing the detail. You can achieve your requirements by enclosing Header contents into bookmark and insert Ref field ({ REF bookmarkname }) at the position of text “Contents”.
Please check following code example for your kind reference.
Document doc = new Document(MyDir + "Example+doc+for+TOC.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.StartBookmark("bm_header");
Paragraph paragraph = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].LastParagraph;
builder.MoveTo(paragraph);
builder.EndBookmark("bm_header");
// Get the Content Control
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
foreach (Paragraph para in sdt)
{
if (para.ToString(SaveFormat.Text).Trim() == "Contents")
{
builder.MoveTo(para);
para.Range.Replace("Contents", "", false, false);
builder.InsertField("Ref bm_header");
break;
}
}
doc.UpdateFields();
doc.Save(MyDir + "Out.docx");
Hope this helps you. Please let us know if you have any more queries.
Thanks Tahir,
please find the attachment, i want to generate the word report like this, can you please check this.
thanks
sai lanka
Hi Sai Lanka,
Thanks for sharing the
detail. First, you need to get understanding about HeaderFooter and TOC field.
The Document can contain only Section objects. In Microsoft Word, a valid document needs to have at least one section. Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section.
E.g if your document have 100 pages with one Section, the text “HI” will be displayed on all pages. In this case, what will be the page number as you shared in your document? However, you can insert Ref Field as shared in my previous post.
A table of contents in a Word document can be built in a number of ways and formatted using a variety of options. The way the table is built and displayed by Microsoft Word is controlled by the field switches.
Note that InsertTableOfContents will only insert a TOC field, but will not actually build the table of contents. The table of contents is built by Microsoft Word when the field is updated.
If you insert a table of contents using this method and then open the file in Microsoft Word, you will not see the table of contents because the TOC field has not yet been updated. In Microsoft Word, fields are not automatically updated when a document is opened, but you can update fields in a document at any time by pressing F9.
Hope this answers your query. Please let us know if you have any more queries.