@Vadivel_S_S,
Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.
Document doc = new Document(MyDir + "input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.insertImage(MyDir + "input.png");
//Remove empty paragraphs from the end of document.
while (doc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0)
{
if(doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.GROUP_SHAPE, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.FORM_FIELD, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.FOOTNOTE, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.COMMENT, true).getCount() > 0
)
break;
//Check if last paragraph contains the page break
if(doc.getLastSection().getBody().getLastParagraph().isEndOfDocument())
{
doc.getLastSection().getBody().getLastParagraph().getRange().replace(ControlChar.PAGE_BREAK, "", new FindReplaceOptions());
}
if (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling() != null &&
(doc.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH))
break;
doc.getLastSection().getBody().getLastParagraph().remove();
// If the current section becomes empty, we should remove it.
if (!doc.getLastSection().getBody().hasChildNodes())
doc.getLastSection().remove();
// We should exit the loop if the document becomes empty.
if (!doc.hasChildNodes())
break;
}
//Remove list items
for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(paragraph.isListItem())
{
paragraph.getListFormat().removeNumbers();
}
if(paragraph.toString(SaveFormat.TEXT).trim().endsWith(".") &&
isNumeric(paragraph.toString(SaveFormat.TEXT).trim() + "0"))
{
paragraph.remove();
}
}
doc.save(MyDir + "17.10.docx");
public static boolean isNumeric(String str)
{
return str.matches("-?\\d+(.\\d+)?");
}