Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Wed Nov 10, 2021 2:22 pm

Hello,

I want to read the header text already available in de header text of the word document.
I tested it with the code below but I can't find the text, how should I do that?

Document document = new Document();
document.LoadFromFile(@"D:\1111.docx");

HeaderFooter header = document.Sections[0].HeadersFooters.Header;

string xxx = "";
Paragraph HParagraph = null;

foreach (Paragraph obj in header.ChildObjects)
{
HParagraph = obj;
xxx =HParagraph.Text;


}

marcel.werksma
 
Posts: 1
Joined: Wed Nov 10, 2021 2:17 pm

Thu Nov 11, 2021 6:30 am

Hello,

Thank you for your inquiry.
Please use my modified code to read the header text. If the code fails to read the header text of your Word file, please provide your Word file for our investigation. You could attach it here or send it to us via email (support@e-iceblue.com). Thanks in advance.
Code: Select all
 static void Main(string[] args)
 {
     Document document = new Document();
     document.LoadFromFile(@"D:\1111.docx");         
     StringBuilder sb = new StringBuilder();
     Section section = document.Sections[0];

     HeaderFooter header = section.HeadersFooters.Header;
     if(header != null)
     {
         readTextHeader(header, sb);
     }

     HeaderFooter firstHeader = section.HeadersFooters.FirstPageHeader;
     if (firstHeader != null)
     {
         readTextHeader(firstHeader, sb);
     }

     HeaderFooter oddHeader = section.HeadersFooters.OddHeader;
     if (oddHeader != null)
     {
         readTextHeader(oddHeader, sb);
     }

     HeaderFooter evenHeader = section.HeadersFooters.EvenHeader;
     if(evenHeader != null)
     {
         readTextHeader(evenHeader, sb);
     }         
     Console.WriteLine(sb.ToString());
 }
 private static StringBuilder readTextHeader(HeaderFooter header, StringBuilder sb)
 {
     foreach (DocumentObject obj in header.ChildObjects)
     {
         if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
         {
             Paragraph HParagraph = (Paragraph)obj;
             sb.Append(HParagraph.Text + "\n");
         }
     }         
     return sb;
 }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Mon Nov 15, 2021 8:05 am

Hello,

Hope you are doing well!
How is your issue going? Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc