@priyanga
Thanks for your inquiry. In this case we suggest you following solution. Hope this helps you.
Document doc = new Document(MyDir + "test (4).DOC");
RemoveSectionBreaks(doc);
int i = 1;
DocumentBuilder builder = new DocumentBuilder(doc);
//Remove empty paragraphs
for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
if (paragraph.toString(SaveFormat.TEXT).trim().length() == 0
&& paragraph.getChildNodes(NodeType.SHAPE, true).getCount() == 0) {
paragraph.remove();
}
}
doc.updatePageLayout();
Boolean hasImage = false;
//Get the paragraphs that start with "Fig".
for (Paragraph paragraph : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(paragraph.toString(SaveFormat.TEXT).trim().contains("Fig"))
{
Node previousPara = paragraph.getPreviousSibling();
while (previousPara != null
&& previousPara.getNodeType() == NodeType.PARAGRAPH
&& previousPara.toString(SaveFormat.TEXT).trim().length() == 0
&& ((Paragraph)previousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
{
previousPara = previousPara.getPreviousSibling();
hasImage = true;
}
if(hasImage && previousPara != null)
{
builder.moveTo(((CompositeNode)previousPara).getFirstChild());
builder.startBookmark("Bookmark"+i);
builder.endBookmark("Bookmark"+i);
builder.moveTo(paragraph.getRuns().get(0));
builder.startBookmark("FigBookmark"+i);
builder.endBookmark("FigBookmark"+i);
i++;
}
hasImage = false;
}
}
for(int b = 1 ; b < i ; b++)
{
Node start = doc.getRange().getBookmarks().get("Bookmark" + b).getBookmarkStart();
Node end = doc.getRange().getBookmarks().get("FigBookmark" + b).getBookmarkEnd();
ArrayList images = ExtractContents.extractContent(start, end, false);
Document dstDoc = ExtractContents.generateDocument(doc, images);
if(dstDoc.getFirstSection().getBody().getFirstParagraph().toString(SaveFormat.TEXT).trim().length() > 0)
for (Run run : (Iterable<Run>)dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.RUN, true))
{
run.setText("");
}
dstDoc.getRange().replace(ControlChar.PAGE_BREAK, "", new FindReplaceOptions());
dstDoc.save(MyDir + "Fig_"+ b + ".docx");
}
private static void RemoveSectionBreaks(Document doc)
{
// Loop through all sections starting from the section that precedes the last one
// and moving to the first section.
for (int i = doc.getSections().getCount() - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc.getLastSection().prependContent(doc.getSections().get((i)));
// Remove the copied section.
doc.getSections().get(i).remove();
}
}