We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy page header into other section

Hi

I have a word document with one page and a header.
Using aspose i insert several section page breaks and unlink the headers from all new sections, because i need each header to be slightly different than the one on the first page.
Unlike in microsoft office, this results in the disappearance of the headers on the newly inserted sections (which kinda makes sense).
What i tried to do now is to create a deep clone of the header from the first section and insert it on all the other sections using document-builder. This results in a “Cannot insert a node of this type at this location.” - exception.
I’m able to insert content manually though (plain text).

Here’s what i tried to do:

SectionCollection sections = doc.getSections();

Node header = sections.get(0).getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY).getChildNodes().get(0).deepClone(true);

for (int sectionIndex = 1; sectionIndex < sections.getCount(); sectionIndex++) {

	Section currentSection = sections.get(sectionIndex);
	currentSection.getHeadersFooters().linkToPrevious(HeaderFooterType.HEADER_PRIMARY, false);

	builder.moveToSection(sectionIndex);
	builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
	builder.insertNode(header);
}

The node that i’m cloning and trying to insert is a table.

Could you tell me how i can archive what i want to do? Maybe there’s a way to copy the header as a whole instead of every single node in the header?

Regards
J.D.

@Jonas_D

Thanks for your inquiry. Please check CopyHeadersFootersFromPreviousSection() method in following example. It will help you to accomplish the task. However, if issue persists then please share following resources, we will look into these and will guide you accordingly.

  • Input Word document
  • Existing output document
  • Expected output document, you may create it using MS Word
  • Sample console application( code with any compilation error).

Create Header Footer using DocumentBuilder

Best Regards,

Thank’s a lot, it’s now working.

The problem seemed to be that i unlinked the headers before inserting them.

@Jonas_D

Thanks for your feedback. It is good to know that you have managed to resolve the issue.

Please keep using Aspose.Words and feel free to contact us for any further assistance.

Best Regards,