Isert editable Rtf document in the docx document

Hi want insert the Output.rtf document in the Acutetoxicityfinalreport_dl.docx document in the place of “{output}” tag (this tag is located at 5th page of Acutetoxicityfinalreport_dl.docx document)as it is.
i.e lay out,pattern,no of pages,headers,footers,data per page of Output.rtf should not disturb at the time of insertion.Output.rtf document should replace the "{output}"tag.
Please find the attached documents.

Thanks in advance.

can any one please respond for this…

Hi Sreelakshmi,

Thanks for your inquiry. I think, you can achieve this after using the code suggested in the following article:
https://reference.aspose.com/words/java/com.aspose.words/IReplacingCallback

Best Regards,

In the above code i am not able to found
insertDocument(para, subDoc) method.

can you please explain me the insertDocument(para, subDoc) method

private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");
        // Insert a document after the paragraph, containing the match text.

        Paragraph para = (Paragraph)e.getMatchNode().getParentNode();

        insertDocument(para, subDoc);
        // Remove the paragraph with the match text.

        para.remove();

        return ReplaceAction.SKIP;
    }
}

I am using Aspose.words.jdk16.jar that was released on 31 may 2012.

Hi
Sreelakshmi,

Thanks for your inquiry and sorry for the delayed response. You can find this method in this page here:

Best Regards,

I have tried with the code what u have sent me, but i am not able to insert the headers and footers of the editable rtf document into the destination document.I have attached the documents in the first post. please find the documents and suggest me how to insert the editable rtf document into Docx Documents as it is i.e headers,footers,no of pagbs should not trunkate in the destination file .
please reply me as early as possible.
Thanks in advance.

Can any one please reply me?

Hi
Sreelakshmi,

Thanks for the additional information.

You’re right, standard InsertDocument method does not copy sections, but copies the content from the Document Body only; that is why headers/footers are getting lost in the final document. Moreover, if you closely look into ‘Output.rtf’ (which is to be inserted in Acutetoxicityfinalreport_dl.docx), you’ll find that there are actually five Sections inside. The first Section has its own ‘Primary Header’. The second Section has both ‘Primary Header’ and ‘Primary Footer’. The 3rd, 4th and 5th Sections have their own ‘Primary Headers’ and ‘Primary Footers’.

In addition, please note that there can be a maximum of only one HeaderFooter of each HeaderFooterType per Section. Talking about headers, there are of three types i.e. HeaderFirst, HeaderPrimary and HeaderEven; so up to three different headers are possible in a Section (i.e. for first, even and odd pages).

It would be great if you could create your Target final document by using MS WORD and attach it here for our reference. We will investigate as to how you are expecting your output document to be generated like and provide you the desired code snippet.

Best Regards,

I need target file in the form of “target final doc.dcox”.
Please find the attached target final doc.dcox document .
Please reply me as early as possible.

Thanks

Hi Sreelakshmi,

Thanks for the additional information. Please try using the following code snippet:

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

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

Paragraph startPara = null;

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

for (Paragraph p : (Iterable<Paragraph>)paras)
{

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

        startPara = p;

        break;

    }

}

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

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

Document beforeKeywordDoc = generateDocument(mainDoc, beforeKeyword);

Document afterKeywordDoc = generateDocument(mainDoc, afterKeyword);

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}"))
    {

        n.remove();

    }

}

finalDoc.save("c:\\test\\outJAVA.docx");

Moreover, you can use extractContent and generateDocument methods from the following API page:
https://docs.aspose.com/words/java/extract-selected-content-between-nodes/

Please let me know if I can be of any further assistance.

Best Regards,

Hi ,
By using the above code I am not getting the expected output.
I want the output as in the outJAVA .docx document.
when i use the above code i am getting the out put as in the outJAVAbefore.docx document.(getting first page as blank page).
Then i have modified the code as bellow

Document beforeKeywordDoc = generateDocument(mainDoc, beforeKeyword);
Document afterKeywordDoc = generateDocument(mainDoc, afterKeyword);
beforeKeywordDoc.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
beforeKeywordDoc.appendDocument(afterKeywordDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
beforeKeywordDoc.save("c:\\test\\outJAVA.docx");

when i use this code i am getting the output as in the** outJAVAafter.docx.(headers and footers of subdoc is pasted to the afterKeywordDoc ).
i don’t want to paste the headers and footer for each page.

Please reply me as early as possible.
Thanks

If i use the attached Acutetoxiciryfinalreport_dl.docx (having its own headers and footer)document as main document i am getting exception.
can you please suggest me how to resolve this issue.
please find the attached document.

Hi,

Thanks for the additional information. I have modified the code a bit and I think this will meet your requirements:

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

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

Paragraph startPara = null;

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

for (Paragraph p : (Iterable<Paragraph>)paras)
{

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

        startPara = p;

        break;

    }

}

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

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

Document beforeKeywordDoc = generateDocument(mainDoc, beforeKeyword);

Document afterKeywordDoc = generateDocument(mainDoc, afterKeyword);

Document finalDoc = new Document();

finalDoc.removeAllChildren();

finalDoc.appendDocument(beforeKeywordDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

finalDoc.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

afterKeywordDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);

afterKeywordDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

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}"))
    {

        n.remove();

    }

}

finalDoc.save("c:\\test\\outJAVA.docx");

I hope, this will help.

Best Regards,

Hi,
If i use the “sample.docx” (which is having headers and footers) insted of
Acutetoxicityfinalreport_dl.docx i am getting java.lang.IllegalArgumentException saying that
“Start node and end node must be a child or descendant of a body”.
It should work for both the documents.
Please find the attached sample.docx document.

Thanks

Hi Sreelakshmi,

Thanks for your inquiry.

The problem occurs because you have header/footer nodes in your Sample.docx document. Actually, in the solution suggested in my previous post, we’re splitting the main document into three portions. The first portion comprises of all the nodes in Document Body that comes before “{Output}” tag. The last portion contains all the nodes in Document Body that comes after “{Output}” tag. The middle portion will be replaced by your RTF file. We’re then instantiating new Document instances for each portion and merging them all to a final document afterwards.

In case you have headers/footers in your document, you can make a check on this and modify the problematic statement like this: NodeCollection paras = mainDoc.getFirstSection().getBody().getChildNodes(NodeType.PARAGRAPH, true);. Of-course, this will exclude the content in Headers/Footers from the final document; but, you can always import those headers/footers by using the code suggested here.

Please let me know if I can be of any further assistance.

Best Regards,

can any one please reply on this?

Please can any one reply on this?

Hi
Sreelakshmi,

Thanks for your inquiry and sorry for the delayed response. I am currently working over your query and will get back to you as soon as possible.

Best Regards,

Hi
I amusing the above code but i am not able to get the expected output. and i am including the below statements int the code.
import org.apache.crimsion.tree.XmlWriteContext;

import org.apache.crimsion.tree.XmlDocument;

import org.apache.crimsion.tree.XmlDocumentBuilder;

i don’t want to delete the above import statements in my code because i am using these statements in some where in the same class.
in the output document i need to insert the utpu.rtf file in
template.docx file in the place of {output} tag and the headers of the
template.docx file should merge with the output.rtf headers.
Please find the attached documents
template.docx is the main document
and outpu.rtf is the document which i need to insert int he place of “{output}” in the template.docx file.({output} string is there in template.docx ).
outputtemp.docx is the dcument wich i nedd as the outptu file.

this is very critical issue please any one can help me on this.