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

Free Support Forum - aspose.com

Isert editable Rtf document in the docx document

Hi Sreelakshmi,

Thanks for your patience and sorry for the delayed response. Could you please run the following code on your side and let us know if it produces the desired output.

Document mainDoc = new Document("c:\test\template.docx");

Document subDoc = new Document("c:\test\Output.rtf");
NodeCollection paras = mainDoc.getFirstSection().getBody().getChildNodes(NodeType.PARAGRAPH, true);

Paragraph startPara = null;

Paragraph endPara = (Paragraph)paras.get(paras.getCount() - 1);

for (Paragraph p : (Iterable<Paragraph>

    if (p.getRange().getText().contains("{Output}"))

        startPara = p;




ArrayList beforeKeyword = extractContent(paras.get(0), startPara, true);

ArrayList afterKeyword = extractContent(startPara, endPara, true);

Document beforeKeywordDoc = generateDocument(mainDoc, beforeKeyword);

Document afterKeywordDoc = generateDocument(mainDoc, afterKeyword);

beforeKeywordDoc = addHeaderFooter(beforeKeywordDoc, mainDoc.getFirstSection(), HeaderFooterType.HEADER_PRIMARY);

afterKeywordDoc = addHeaderFooter(afterKeywordDoc, mainDoc.getFirstSection(), HeaderFooterType.HEADER_PRIMARY);

Document finalDoc = new Document();


finalDoc.appendDocument(beforeKeywordDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

finalDoc.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);



finalDoc.appendDocument(afterKeywordDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

// Before saving remove {Output}

Node[] nodes = finalDoc.getChildNodes(NodeType.PARAGRAPH, true).toArray();

for (Node n : nodes)

    if (n.getRange().getText().contains("{Output}"))




for (Section s : finalDoc.getSections())




I hope, this will help.

Best Regards,

Hi I am using the aspose.words.jar file which was released on march 2010.
In the final output file the template headers are not merging withe the Editable rtf headers.
I want he final output file having the main document headers and footers for the before key word and after key word and sub document should append with the headers or the main document(header of the page containing key word).
refer 7,8,9th pages in attached document.
Please find the attached sample output file.

Hi Sreelakshmi,

Thank you for providing the additional information and your patience. We are working over you query and please expect a reply within next 24 hours.

Best Regards,


Thanks for your patience. I have attached the code here (please see Final code.txt) for your reference. Please let me know if I can be of any further assistance.

Best Regards,

Hi Hafeez,
Where can I find code for the method ‘addHeaderFooter’. I am getting the exception when I integrate the attached code.

beforeKeywordDoc = addHeaderFooter(beforeKeywordDoc, maindoc.getFirstSection(), HeaderFooterType.HEADER_PRIMARY);
afterKeywordDoc = addHeaderFooter(afterKeywordDoc, maindoc.getFirstSection(), HeaderFooterType.HEADER_PRIMARY);


I can see the code. Ignore previous post.

Thanks it works, the headers are merging. I have some other issues that i am going to track under the below thread.


Thanks for your feedback. It is perfect that you managed to merge headers on your side. Please let us know any time you have any further queries. Regarding your other issue, I would suggest you please follow this thread for further proceedings.

Best Regards,

I need to use paragraphFormat.setSpaceAfter(),paragraphFormat.setSpaceBefore() in the header part.
can you please suggest me how to set spaceAfter,spacebefore in the header part?

Hi Sreelakshmi,

Thanks for your inquiry.

Well, please get a reference to a Paragraph node in your header and set the amount of spacing before/after that Paragraph by using ParagraphFormat.SpaceAfter and ParagraphFormat.SpaceBefore properties. I would suggest you please read the Paragraph Formatting section in the following article:

I hope, this helps.

Best regards,