Import all HeaderFooter from a document to another

Hello,

I use Aspose.Words 2.4.2 for Java and i would like to import all HeaderFooter from a document to another.

I have writed the code below and :

  1. The HeaderFooter in target document are correctly removed
  2. The HeaderFooter of the source document are imported because i can see the information if i display the text() value of the “node” variable during the processing

But when i save the document and i open it i didn’t see the imported HeaderFooter…

In the code below :
document is the variable with the target document
headerFooterSourceDocument is the variable with the source document

Code:

Section section = headerFooterSourceDocument.getFirstSection();
int importFormatMode = (keepSourceFormat) ? ImportFormatMode.KEEP_SOURCE_FORMATTING
: ImportFormatMode.USE_DESTINATION_STYLES;
// Remove all HeaderFooter of the target document for each
// Section
for (int s = 0; s < document.getSections().getCount(); s++)
{
    if (document.getSections().get(s).getHeadersFooters() != null)
    {
        while (document.getSections().get(s).getHeadersFooters().getCount() != 0)
        {
            document.getFirstSection().getHeadersFooters().removeAt(0);
        }
    }
}
// Add content to the document (Import all HeaderFooter)
for (int i = 0; i < section.getHeadersFooters().getCount(); i++)
{
    // Import HeaderFooter**
    Node node = document.importNode(section.getHeadersFooters().get(i), true, importFormatMode);
    // Insert header footer into the first section of
    // document
    document.getFirstSection().getHeadersFooters().add(node);
}

My code seems to be correct but i didn’t understand where is my error…

I have put in attachment the source and target documents in a zip file.

Thanks in advance.

Dominique

Hi

Thanks for your inquiry. Please try using the following code:

Section section = headerFooterSourceDocument.getFirstSection();
int importFormatMode = (keepSourceFormat) ? ImportFormatMode.KEEP_SOURCE_FORMATTING
: ImportFormatMode.USE_DESTINATION_STYLES;
// Remove all HeaderFooter of the target document for each
// Section
for (int s = 0; s < document.getSections().getCount(); s++)
{
    if (document.getSections().get(s).getHeadersFooters() != null)
    {
        while (document.getSections().get(s).getHeadersFooters().getCount() != 0)
        {
            document.getFirstSection().getHeadersFooters().removeAt(0);
        }
    }
}
document.getFirstSection().getPageSetup().setDifferentFirstPageHeaderFooter(section.getPageSetup().getDifferentFirstPageHeaderFooter());
document.getFirstSection().getPageSetup().setOddAndEvenPagesHeaderFooter(section.getPageSetup().getOddAndEvenPagesHeaderFooter());
// Add content to the document (Import all HeaderFooter)
for (int i = 0; i < section.getHeadersFooters().getCount(); i++)
{
    // Import HeaderFooter
    Node node = document.importNode(section.getHeadersFooters().get(i), true, importFormatMode);
    // Insert header footer into the first section of
    // document
    document.getFirstSection().getHeadersFooters().add(node);
}

Hope this helps.
Best regards.

Hello,

Your answer has fixed my issue.

Thanks for your helps.

Best regards,

Dominique