Write new pargraph after current pargraph with same formatting

Hi there,

I want to add two lines (both must be in separate paragraph) after one paragraph with same formatting of above paragraph. to do this i am using below code but it is not working as it is adding both lines before current paragraph not after current paragraph. can you please help me in this?

	Document doc = new Document(dataDir + "/source.docx");
	Paragraph start = null;
	
	NodeCollection<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

	for (Paragraph para : paragraphs) {
		if (para.toString(SaveFormat.TEXT).startsWith("BODY_CONTENT")) {
			start = para;
			start.getRuns().get(0).setText("==START==");
		}
	}
	
	DocumentBuilder builder = new DocumentBuilder(doc);
	builder.moveToParagraph(paragraphs.indexOf(start) , 0);
	builder.writeln("INSERT_HTML");
	builder.writeln("==END==");
	doc.save(dataDir + "output.docx");

@babo123

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I have attached the required docx files. Please let me know if anything else is needed.

Attachment:-data.zip (13.8 KB)

@babo123

Thanks for sharing the detail. Please use second parameter of DocumentBuilder.MoveToParagraph method as -1 to move the cursor to the end of the paragraph

Please use the following modified code to get the desired output.

Document doc = new Document(MyDir + "source.docx");
Paragraph start = null;

NodeCollection<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

for (Paragraph para : paragraphs) {
    if (para.toString(SaveFormat.TEXT).startsWith("BODY_CONTENT")) {
        start = para;
        start.getRuns().get(0).setText("==START==");
        break;
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToParagraph(paragraphs.indexOf(start) , -1);
builder.writeln();
builder.writeln("INSERT_HTML");
builder.write("==END==");
doc.save(MyDir + "output.docx");

Thanks for the quick reply. Above solution is not working for attached sample.

Attachment: data.zip (34.1 KB)

@babo123

Please try the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "source.docx");
Paragraph start = null;

NodeCollection<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

for (Paragraph para : paragraphs) {
    if (para.toString(SaveFormat.TEXT).startsWith("MED_BODY_CONTENT")) {
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.moveTo(para);
        builder.writeln();
        builder.writeln("==START==");
        builder.writeln("INSERT_HTML");
        builder.write("==END==");
        para.remove();
        break;
    }
}

doc.save(MyDir + "output.docx");

Hi Tahir,

Solution works fine but it does not preserve the actual formatting of MED_BODY_CONTENT in newly created paragraphs that i have already mentioned on original question.

Attchment :- data.zip (28.1 KB)

@babo123

In this case, we suggest you please clone the paragraph node as shown below and set it text.

Document doc = new Document(MyDir + "source.docx");
NodeCollection<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

for (Paragraph para : paragraphs) {
    if (para.toString(SaveFormat.TEXT).startsWith("MED_BODY_CONTENT")) {
        if(para.getRuns().getCount() > 0)
        {
            Run run = (Run)para.getRuns().get(0).deepClone(true);
            run.setText("==START==");
            para.removeAllChildren();
            para.getRuns().add(run);

            Paragraph para2 =  (Paragraph) para.deepClone(true);
            para2.getRuns().get(0).setText("INSERT_HTML");
            para2 = (Paragraph) para.getParentNode().insertAfter(para2, para);

            Paragraph para3 =  (Paragraph) para.deepClone(true);
            para3.getRuns().get(0).setText("==END==");

            para2.getParentNode().insertAfter(para3, para2);
            break;
        }
    }
}

doc.save(MyDir + "output.docx");