Free Support Forum - aspose.com

Import Header/Footer from another document

Hello,

I use Aspose.Word 2.4.1 with Java and i have problem to import Header/Footer node from a document to another . I’m seaching for a example but i didn’t find any samples…

I have always this error : "Cannot insert a node of this type at this location."

My code is :
/**
* Method to add a header or a footer to a document
*
* @param document
* Source document
* @param section
* Section source containing Header/Footer
* @param headerFooterType
* Type used to specify if the content added is a HEADER or a
* FOOTER section (use object “com.aspose.words.HeaderFooterType”
* constants to specify the value)
* @return the updated document
* @throws Exception
* @see com.aspose.words.HeaderFooterType
*/
public static Document addHeaderFooter(Document document, Section section,
int headerFooterType) throws Exception {

// Check document input parameter
if (document == null) {
throw new IllegalArgumentException(
“Document object can’t be null !”);
}
if (section == null) {
throw new IllegalArgumentException(
“the section object can’t be null !”);
}

// Create a builder to modify document
DocumentBuilder documentBuilder = new DocumentBuilder(document);

// Move to the HEADER or FOOTER section
documentBuilder.moveToHeaderFooter(headerFooterType);

// Add content to the document
for (int i = 0; i < section.getHeadersFooters().getCount(); i++) {
Node node = document.importNode(section.getHeadersFooters().get(i),
true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
documentBuilder.getCurrentSection().getHeadersFooters().add(node);
}

return document;

}


Thanks in advance for your help

Dominique

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please try using the following code instead yours.

public static Document addHeaderFooter(Document document, Section section, int headerFooterType) throws Exception

{

// Check document input parameter

if (document == null) {

throw new IllegalArgumentException(

"Document object can't be null !");

}

if (section == null) {

throw new IllegalArgumentException(

"the section object can't be null !");

}

// Add content to the document

for (int i = 0; i < section.getHeadersFooters().getCount(); i++)

{

//Import HeaderFooter of specified type only

if(section.getHeadersFooters().get(i).getHeaderFooterType() == headerFooterType)

{

//Remove HeaderFooter of specified tipe from the destination document

for (int j = 0; j < document.getFirstSection().getHeadersFooters().getCount(); j++)

{

if(document.getFirstSection().getHeadersFooters().get(j).getHeaderFooterType() == headerFooterType)

{

document.getFirstSection().getHeadersFooters().get(j).remove();

}

}

//Import HeaderFooter

Node node = document.importNode(section.getHeadersFooters().get(i),

true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

//Insert header footer into the first section of document

document.getFirstSection().getHeadersFooters().add(node);

}

}

return document;

}

I hope this could help you.

Best regards.

In all case thanks for your quick answer…

Best regards,

Dominique

This problem is solved with your version of the method…

Thanks you very much for your support !

Best regards,

Dominique