Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Aug 03, 2019 3:28 pm

Set header like this:
case 1. for word document with one page: no header for the first page.
case 2. for word document with two pages: no header for the first page, header shows as Completed for the second/last page.
case 3. for word document with three pages: no header for the first page, header shows as In Progress for the second page, header shows as Completed for the third/last page.
case 4. for word document with four pages: no header for the first page, header shows as In Progress for the second page & third page, header shows as Completed for the fourth/last page.
case 5. see case 4.

solution: Set different sections for each page and add the corresponding header for different sections, but i don't know how to implement it. Could anyone help?

492720891
 
Posts: 1
Joined: Sat Aug 03, 2019 2:47 pm

Mon Aug 05, 2019 4:01 am

Hello,

Thanks for your inquiry.
Please refer to the following sample code to set different sections for each page and add the corresponding header for different sections. If there is any question, please provide your desired output file for our reference, then we will further look into it.
Code: Select all
  Document document = new Document();
  //add the first section
  Section sec1 = document.AddSection();
  Paragraph para1 = sec1.AddParagraph();
  //append text for paragraph of the first section
  para1.AppendText("Page 1");

  //insert the second section break and start a new page
  document.Sections[0].Paragraphs[document.Sections[0].Paragraphs.Count - 1].InsertSectionBreak(SectionBreakType.NewPage);
  Paragraph para2 = document.Sections[1].AddParagraph();
  //append text for paragraph of the second section
  para2.AppendText("Page 2");
  //add header paragraph
  HeaderFooter header = document.Sections[1].HeadersFooters.Header;
  Paragraph headerPara1 = header.AddParagraph();
  headerPara1.AppendText("E-iceblue");
  headerPara1.Format.HorizontalAlignment = HorizontalAlignment.Right;

  //insert the third section break and start a new page
  document.Sections[1].Paragraphs[document.Sections[1].Paragraphs.Count - 1].InsertSectionBreak(SectionBreakType.NewPage);
  Paragraph para3 = document.Sections[2].AddParagraph();
  //append text for paragraph of the third section
  para3.AppendText("Page 3");

  //insert the fourth section break and start a new page
  document.Sections[2].Paragraphs[document.Sections[2].Paragraphs.Count - 1].InsertSectionBreak(SectionBreakType.NewPage);
  Paragraph para4 = document.Sections[3].AddParagraph();
  //append text for paragraph of the fourth section
  para4.AppendText("Page 4");
  //add header paragraph
  document.Sections[3].HeadersFooters.Header.ChildObjects.Clear();
  HeaderFooter newHeader = document.Sections[3].HeadersFooters.Header;
  Paragraph headerPara2 = newHeader.AddParagraph();
  headerPara2.AppendText("Spire.Doc for .NET");
  headerPara2.Format.HorizontalAlignment = HorizontalAlignment.Left;
  document.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc