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.

Wed Jan 09, 2019 4:34 am

Hi ,

I want to create a section and each section has a different header and footer.

Regards,

mkm090
 
Posts: 7
Joined: Tue Dec 01, 2015 10:25 am

Wed Jan 09, 2019 6:39 am

Hi,

Please refer to the code below to reach your goal.
Code: Select all
Document document = new Document();
//add a section and add header and footer for this section
Section sec = document.AddSection();
sec.HeadersFooters.Header.AddParagraph().AppendText("Header for section1.");
sec.HeadersFooters.Footer.AddParagraph().AppendText("Footer for section1.");

//Add another section and set different header footer.
Section sec1 = document.AddSection();
sec1.HeadersFooters.Header.AddParagraph().AppendText("Header for section2.");
sec1.HeadersFooters.Footer.AddParagraph().AppendText("Footer for section2.");
document.SaveToFile("headerFooter.docx", Spire.Doc.FileFormat.Docx);


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Jan 09, 2019 6:59 am

ptrnew (26).rar
Hi,

Kindly find my code below :
Code: Select all
// Investigation Section
                Section InvestigationSection = document.AddSection();
                InvestigationSection.PageSetup.InsertPageNumbers(false, Spire.Doc.Documents.PageNumberAlignment.Left);
                InvestigationSection.AddParagraph().AppendHTML(InvestigationContent);
                InvestigationSection.HeadersFooters.Header.AddParagraph().AppendHTML(Header);
                InvestigationSection.HeadersFooters.Footer.AddParagraph().AppendHTML(InvestigationFooter);
                InvestigationSection.Paragraphs[InvestigationSection.Paragraphs.Count - 1].InsertSectionBreak(Spire.Doc.Documents.SectionBreakType.NoBreak);

                // Decision Section
                Section DecisionSection = null;
                if (DecisionContent.Length > 0)
                {
                    DecisionSection = document.AddSection();
                    DecisionSection.AddParagraph().AppendHTML(DecisionContent);
                    DecisionSection.HeadersFooters.Header.AddParagraph().AppendHTML(Header);
                    InvestigationSection.HeadersFooters.Footer.AddParagraph().AppendHTML(DecisionFooter);
                }

                ms = new MemoryStream();
                document.SaveToStream(ms, FileFormat.Docx);


The footer and header are duplicated in all pages even If I try to delete or clear one of the header and footer section is not working.

Please find the file in attachment.

Regards,

mkm090
 
Posts: 7
Joined: Tue Dec 01, 2015 10:25 am

Wed Jan 09, 2019 8:07 am

Hi,

Thank you for sharing the details.
Please set the property "LinkToPrevious" to false to avoid the duplication of the previous header footer content.
Code: Select all
 // Investigation Section
Section InvestigationSection = document.AddSection();
InvestigationSection.PageSetup.InsertPageNumbers(false, Spire.Doc.Documents.PageNumberAlignment.Left);
InvestigationSection.AddParagraph().AppendHTML(InvestigationContent);
InvestigationSection.HeadersFooters.LinkToPrevious = false;// added by jane
InvestigationSection.HeadersFooters.Header.AddParagraph().AppendHTML(Header);
InvestigationSection.HeadersFooters.Footer.AddParagraph().AppendHTML(InvestigationFooter);
InvestigationSection.Paragraphs[InvestigationSection.Paragraphs.Count - 1].InsertSectionBreak(Spire.Doc.Documents.SectionBreakType.NoBreak);

// Decision Section
Section DecisionSection = null;
 if (DecisionContent.Length > 0)
{
     DecisionSection = document.AddSection();
     DecisionSection.AddParagraph().AppendHTML(DecisionContent);
     DecisionSection.HeadersFooters.LinkToPrevious = false;// added by jane
     DecisionSection.HeadersFooters.Header.AddParagraph().AppendHTML(Header);
     DecisionSection.HeadersFooters.Footer.AddParagraph().AppendHTML(DecisionFooter);
}

ms = new MemoryStream();
document.SaveToStream(ms, FileFormat.Docx);


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc

cron