I couldn't find a way to copy the header from an OpenOffice writer doc to another with ODFDOM.
I tried this:

TextDocument docIntestazione = TextDocument.loadDocument("intestazione.odt");//the file with only the header
TextDocument docBody = TextDocument.loadDocument("body.odt");//the file with another header which has to be replaced
Header headerBody = docBody.getHeader();
Header headerIntestazione = docIntestazione.getHeader();
StyleHeaderElement styleHeaderIntestazione =  headerIntestazione.getOdfElement();
docBody.insertOdfElement(headerBody.getOdfElement(), (Document)docIntestazione, (OdfElement)styleHeaderIntestazione, true);

But insertOdfElement method returns null and does nothing.

I also tried:

StyleHeaderElement styleHeaderBody =  headerBody.getOdfElement();
StyleHeaderElement clone = (StyleHeaderElement)styleHeaderIntestazione.cloneNode(true);
styleHeaderBody.removeChild(styleHeaderBody.getFirstChild());//it works. It removes the table that in my case is the only child of the header which has to be replaced
styleHeaderBody.appendChild(clone);

But I get this exception:

org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

For org.w3c.dom.Document there is an importNode method, but I couldn't find it for org.odftoolkit.simple.TextDocument

0

There are 0 best solutions below