HeaderFooterCollectionLinkToPrevious Method (HeaderFooterType, Boolean) |
Namespace: Aspose.Words
If the header or footer of the specified type does not exist, creates it automatically.
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create three sections builder.Write("Section 1"); builder.InsertBreak(BreakType.SectionBreakNewPage); builder.Write("Section 2"); builder.InsertBreak(BreakType.SectionBreakNewPage); builder.Write("Section 3"); // Create a header and footer in the first section and give them text builder.MoveToSection(0); builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); builder.Write("This is the header, which will be displayed in sections 1 and 2."); builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); builder.Write("This is the footer, which will be displayed in sections 1, 2 and 3."); // If headers/footers are linked by the next section, they appear in that section also // The second section will display the header/footers of the first doc.Sections[1].HeadersFooters.LinkToPrevious(true); // However, the underlying headers/footers in the respective header/footer collections of the sections still remain different // Linking just overrides the existing headers/footers from the latter section Assert.AreEqual(doc.Sections[0].HeadersFooters[0].HeaderFooterType, doc.Sections[1].HeadersFooters[0].HeaderFooterType); Assert.AreNotEqual(doc.Sections[0].HeadersFooters[0].ParentSection, doc.Sections[1].HeadersFooters[0].ParentSection); Assert.AreNotEqual(doc.Sections[0].HeadersFooters[0].GetText(), doc.Sections[1].HeadersFooters[0].GetText()); // Likewise, unlinking headers/footers makes them not appear doc.Sections[2].HeadersFooters.LinkToPrevious(false); // We can also choose only certain header/footer types to get linked, like the footer in this case // The 3rd section now won't have the same header but will have the same footer as the 2nd and 1st sections doc.Sections[2].HeadersFooters.LinkToPrevious(HeaderFooterType.FooterPrimary, true); // The first section's header/footers can't link themselves to anything because there is no previous section Assert.AreEqual(2, doc.Sections[0].HeadersFooters.Count); Assert.False(doc.Sections[0].HeadersFooters[0].IsLinkedToPrevious); Assert.False(doc.Sections[0].HeadersFooters[1].IsLinkedToPrevious); // All of the second section's header/footers are linked to those of the first Assert.AreEqual(6, doc.Sections[1].HeadersFooters.Count); Assert.True(doc.Sections[1].HeadersFooters[0].IsLinkedToPrevious); Assert.True(doc.Sections[1].HeadersFooters[1].IsLinkedToPrevious); Assert.True(doc.Sections[1].HeadersFooters[2].IsLinkedToPrevious); Assert.True(doc.Sections[1].HeadersFooters[3].IsLinkedToPrevious); Assert.True(doc.Sections[1].HeadersFooters[4].IsLinkedToPrevious); Assert.True(doc.Sections[1].HeadersFooters[5].IsLinkedToPrevious); // In the third section, only the footer we explicitly linked is linked to that of the second, and consequently the first section Assert.AreEqual(6, doc.Sections[2].HeadersFooters.Count); Assert.False(doc.Sections[2].HeadersFooters[0].IsLinkedToPrevious); Assert.False(doc.Sections[2].HeadersFooters[1].IsLinkedToPrevious); Assert.False(doc.Sections[2].HeadersFooters[2].IsLinkedToPrevious); Assert.True(doc.Sections[2].HeadersFooters[3].IsLinkedToPrevious); Assert.False(doc.Sections[2].HeadersFooters[4].IsLinkedToPrevious); Assert.False(doc.Sections[2].HeadersFooters[5].IsLinkedToPrevious); doc.Save(ArtifactsDir + "HeaderFooter.HeaderFooterLink.docx");