Free Support Forum -

How to Get Text from Header


i have the following code that will get me all the text in the word document

Document doc=new Document("C://sample.docx");

string templateText = doc.GetText();

But i need a code,that i will get text only from header in the word document

Am attaching a sample template here,i have a header and and table in that.I need to get all the text in header not from the Table


Ajeesh M J

Hi Ajeesh,

Thanks for your inquiry. In Ms Word document each section can have 3 types of headers and 3 types of footers. If you need to get text of the particular header or footer of the particular section, you should just get the HeaderFooter object of the appropriate type and get its text. The HeaderFooterType identifies the type of header or footer found in a Word file.

The following code shows how to get text from the primary header of the first section of MS Word document:

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>
Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document(MyDir

  • “Headout.docx”);<o:p></o:p>

// Get primary header of the appropriate type and print its text.

HeaderFooter primaryHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];

if (primaryHeader != null)


Please use the following code snippet to get the text of Primary header of all sections in the document.

Document doc = new Document(MyDir + "Headout.docx");

String headerText = "";

foreach (Section section in doc.Sections)


foreach (HeaderFooter headerfooter in section.HeadersFooters)


if (headerfooter.HeaderFooterType == HeaderFooterType.HeaderPrimary)


headerText += headerfooter.GetText();