Please help me to remove additional empty paragraph from word document.
I have attached sample file(TEST.doc) which does have additional empty space in between sections. I have tried to use below code approach to delete the empty space but its deleting the previous content bookmark[HD_CL_STD] too.
Document doc1 = new Document(pathToSaveContract + "TEST.doc");
removeExtraSpaceBeforeTitle(doc1, "PRICE VALUES");
…
…
public boolean removeExtraSpaceBeforeTitle(Document doc, String secTitle)
{
try
{
// ================ Node[] paras = doc.getChildNodes(NodeType.PARAGRAPH, true)
.toArray();
int paraId = 0;
for (int i = 0; i <paras.length; i++)
{
Paragraph p = (Paragraph) paras[i];
if (p.getText() != null &&
p.getText().trim().equalsIgnoreCase(secTitle))
{
System.out.println("Para (" + i + ")=" + p.getText());
paraId = i;
break;
}
}
System.out.println("Para id=" + paraId);
if (paraId> 0)
{
Paragraph para = (Paragraph) paras[paraId - 2];
if (para.getChildNodes().getCount() == 0 || para.toString(SaveFormat.TEXT).trim().equals(""))
{
para.remove();
System.out.println("Empty para Removed for " + secTitle);
}
else
{
return true;
}
removeExtraSpaceBeforeTitle(doc, secTitle);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
Thanks for your inquiry. The BookmarkEnd node of bookmark “HD_CL_STD” is inside empty paragraph. In this case, we suggest you please insert the BookmarkEnd node in the previous node of it’s parent node. Please check the following code example. Hope this helps you.
Your suggestion is working for one scenario, but other scenarios also need to cover. Sorry forget to explain the complete requirement in first query itself.
Inside [HD_CL_STD] bookmark, document having another bookmark also [SVC_SEC]. Sometime it will have many other bookmarks. So all these bookmarks should not delete after removing the empty space(s). And also mostly before the empty space(s), content will be TABLE or TEXT.
I have tried below code to manage the TABLE & TEXT objects and its removing the space if its single empty space only.
The BookmarkEnd node of bookmark [HD_CL_STD] and [SVC_SEC] are inside same bookmark. Please check the attach DOM image for detail. Please use the same approach to insert these BookmarkEnd nodes. Move the cursor to the previous node of paragraph that contains the BookmarkEnd nodes and insert these nodes using DocumentBuilder.endBookmark method.
Is there any way to list out all the bookmarks which all are having same BookmarkEnd node of [HD_CL_STD]. And then process for making the DocumentBuilder.endBookmark to all the BookmarkEnd nodes. Because [SVC_SEC] is not fixed one, sometime document will have some other bookmarks.
Below method is working fine if its single empty line. And please note that I have used getNextSibling() method only, because getPreviousSibling() is not working properly.
If i have more then one empty lines, below method is not working. And its throwing “java.lang.IllegalArgumentException: node” exception. NODE object getting null. Please provide the code snippet to handle the multiple empty lines (some time it will be 2 or 3 or 4 empty lines).
Hey there people, thanks a lot for all your answers. I already got lots of headaches because of this (no really, i even used an Analgin) but I do seemed I found some useful information here… thanks again!!