We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Extract Label from image

@Saranya_Sekar

Thanks for your inquiry. Yes, you can extract the nodes from the document without using ExtractContent class. You need to get the nodes between BookmarkStart node and BookmarkEnd node. Please use Node.NextSibling property to get the next sibling of node and use NodeImporter.ImportNode method to import the it into new document.

@tahir.manzoor
Can you please provide the sample code for that.

@Saranya_Sekar

Thanks for your inquiry. Sure, we will share the code snippet shortly.

@Saranya_Sekar

Please use the following method to extract the paragraphs from the source document. We suggest you please read about document object model of Aspose.Words from here:
Aspose.Words Document Object Model

Please also read Programming with Documents.

static ArrayList ExtractContentBetweenParagraphs(Paragraph para1, Paragraph para2) throws Exception
{
    ArrayList nodes = new ArrayList();
    nodes.add(para1);
    Node currentNode = para1;
    while(currentNode != null && !currentNode.equals(para2))
    {
        currentNode = currentNode.getNextSibling();
        nodes.add(currentNode);
    }

    return nodes;
}

ArrayList nodes =  ExtractContentBetweenParagraphs((Paragraph)bm.getBookmarkStart().getParentNode(), (Paragraph) bm.getBookmarkEnd().getParentNode());

@tahir.manzoor
I have a sample document with various scenarios can you help me to extract all the images.Sample input is Multiple_Label.zip (2.0 MB)
Expected output is Multiple-Label-output.zip (2.0 MB)
Kindly help please.

@Saranya_Sekar

Thanks for your inquiry. Please give us some time. We will check all use cases of this document and share the code example with you.

Kindly include this as sample input Multiple_Label.zip (2.3 MB)
and expected output is Multiple-Label-output.zip (2.3 MB)
Thanks in advance.

I am using this code

private static void labelledImagesExtraction(Document interimdoc) throws Exception
{
Document doc = interimdoc;
DocumentBuilder builder = new DocumentBuilder(doc);

	int bookmark = 1;
	int i = 1;
	NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
	for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
	{
	    if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
	    {

	        Node PreviousPara = paragraph.getPreviousSibling();
	        
	        String label = PreviousPara.toString(SaveFormat.TEXT).trim();
	          String pattern = "(.*?)";
	          Pattern regExp = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
	          Matcher match = regExp.compile(pattern).matcher(label);
	          if(match.matches()) 
	        if(PreviousPara.toString(SaveFormat.TEXT).trim().contains(match.group()))
	        {
	            PreviousPara = PreviousPara.getPreviousSibling();
	            if(((Paragraph)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
	            {
	                if(PreviousPara == null)
	                {
	                    builder.moveToDocumentStart();
	                    builder.insertParagraph();
	                    builder.startBookmark("Bookmark" + bookmark);
	                    builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
	                    builder.endBookmark("Bookmark" + bookmark);
	                    bookmark++;
	                }
	                else if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
	                {
	                    while(PreviousPara != null && ((Paragraph)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() == 0)
	                        PreviousPara = PreviousPara.getNextSibling();

	                    Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
	                    builder.moveTo(node);
	                    builder.startBookmark("Bookmark" + bookmark);
	                    builder.moveTo(paragraph);
	                    builder.endBookmark("Bookmark" + bookmark);
	                    bookmark++;
	                }
	                
	            }
	        }
	    }
	}

	for (Bookmark bm : doc.getRange().getBookmarks())
	{
	    if(bm.getName().startsWith("Bookmark"))
	    {
	    	ArrayList nodes =  ExtractContentBetweenParagraphs((Paragraph)bm.getBookmarkStart().getParentNode(), (Paragraph) bm.getBookmarkEnd().getParentNode());
	        Document dstDoc = generateDocument(doc, nodes);

	        PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
	        dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
	        dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
	        dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());

	        dstDoc.updatePageLayout();
	        if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
	            dstDoc.getLastSection().getBody().getLastParagraph().remove();

	        while(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)
	            dstDoc.getFirstSection().getBody().getFirstParagraph().remove();

	        dstDoc.updatePageLayout();
	        String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
	        dstDoc.save(folderName + filename.substring(0, 7) + "_out.docx");
	        dstDoc.save(folderName + filename.substring(0, 7) + "_out.pdf");
	        dstDoc.save(folderName + filename.substring(0, 7) + "_out.jpeg");
	        i++;
	    }
	}

	//Add fig text at the place of images
	for (Bookmark bm : doc.getRange().getBookmarks()) {
	    if (bm.getName().startsWith("Bookmark")) {
	        bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
	    }
	}
	doc.updatePageLayout();
	for (Bookmark bm : doc.getRange().getBookmarks()) {
	    if (bm.getName().startsWith("Bookmark")) {
	        bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
	        String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
	        bm.setText("<Fig>"+figText.substring(0, 7)+"</Fig>" + ControlChar.PARAGRAPH_BREAK );
	        interimdoc.save(interim);
	    }
	}
}
	
	
	
	
	
	
	
	
static ArrayList ExtractContentBetweenParagraphs(Paragraph para1, Paragraph para2) throws Exception
{
    ArrayList nodes = new ArrayList();
    nodes.add(para1);
    Node currentNode = para1;
    while(currentNode != null && !currentNode.equals(para2))
    {
        currentNode = currentNode.getNextSibling();
        nodes.add(currentNode);
    }

    return nodes;
}


public static Document generateDocument(Document srcDoc, ArrayList nodes) throws Exception {

    // Create a blank document.
    Document dstDoc = new Document();
    // Remove the first paragraph from the empty document.
    dstDoc.getFirstSection().getBody().removeAllChildren();

    // Import each node from the list into the new document. Keep the original formatting of the node.
    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

    for (Node node : (Iterable<Node>) nodes) {
        Node importNode = importer.importNode(node, true);
        dstDoc.getFirstSection().getBody().appendChild(importNode);
    }

    // Return the generated document.
    return dstDoc;
}

@Saranya_Sekar

Thanks for sharing the document and code. We will check the use cases for this document and write the code examples. We will share the examples as soon as possible.

@Saranya_Sekar

Please use the following code example to get the desired output. Please check the attachment. CodeExamples.zip (1.2 KB)

Document doc = new Document(MyDir + "Multiple_Label.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
UseCase1(doc, builder);
ExtractImages(doc, "uc1");

UseCase2(doc, builder);
ExtractImages(doc, "uc2");

@tahir.manzoor
Thank you very much for the code sample.

@Saranya_Sekar

Thanks for your inquiry. To generate the interim document, please check the code example shared in my post in this thread.

@tahir.manzoor
Thank you very much. I tried to bookmark the labelled figures using the previous post mentioned , my code is labelledImageExtraction.zip (1.6 KB)
But few text above the images are missing Multiple_Label.zip (2.3 MB).
The derived output interim document is extracting un-labelled image as well here Fig-20 is
bookmarked and the interim misses some text…Multiple_Label_Interim.zip (9.5 KB)
Kindly help please.

@Saranya_Sekar

Thanks for your inquiry. Could you please share some detail about your query along with screenshots of problematic sections of output document? We will then answer your query accordingly.

@tahir.manzoor

The lines marked in yellow are missing after bookmark
Multiple_Label.zip (2.3 MB)
Input file is Multiple_Label_Input.zip (2.3 MB)

and the Fig 20 is bookmarked as labelled image.Multiple_Label_Interim.zip (9.5 KB)

@Saranya_Sekar

Thanks for sharing the detail. Please give us some time. We will write the code for your “Interim” document.

@Saranya_Sekar

Following code example extracts the images from “Multiple_Label.docx” and generates the “Interim” document. Please note that this code example covers only the uses cases in “Multiple_Label.docx”. We suggest you please read the complete code and write the code for your other use cases.

We have attached the output documents along with “Interim” document with this post for your kind reference. Docs.zip (2.3 MB)

Document doc = new Document(MyDir + "Multiple_Label.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

UseCase3(doc, builder);
ExtractImages(doc, "uc2", builder);

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

public static void UseCase3(Document doc, DocumentBuilder builder) throws Exception
{
    int bookmark = 1;
    int i = 1;
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
    {
        if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
        {
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null &&
                    (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                        (
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(a)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(d)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().startsWith("(Fig"))
                        )
                    )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
                bln = true;
            }

            if(!bln)
                continue;

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                //builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else
            if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("Bookmark" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
        }
    }
}

public  static void ExtractImages(Document doc, String uc, DocumentBuilder builder) throws Exception
{
    int i = 1;
    String bookmark = "bm_extract";
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
        }
    }
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            Node currentNode = bm.getBookmarkStart();
            while (currentNode.getNodeType() != NodeType.SHAPE)
                currentNode = currentNode.nextPreOrder(doc);

            builder.moveTo(currentNode);
            builder.startBookmark(bookmark + i);
            builder.moveTo(bm.getBookmarkEnd());
            builder.endBookmark(bookmark + i);
            i++;
        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.remove();
        }
    }
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks())
    {
        if(bm.getName().startsWith("bm_extract"))
        {
            ArrayList nodes =  ExtractContents.extractContent(bm.getBookmarkStart(), bm.getBookmarkEnd(), true);
            Document dstDoc = ExtractContents.generateDocument(doc, nodes);

            PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
            dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
            dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
            dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());

            dstDoc.updatePageLayout();
            if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
                dstDoc.getLastSection().getBody().getLastParagraph().remove();

            dstDoc.updatePageLayout();
            while(dstDoc.getFirstSection().getBody().getFirstParagraph()!= null && dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)
                dstDoc.getFirstSection().getBody().getFirstParagraph().remove();

            dstDoc.updatePageLayout();
            if(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            {
                String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
                if(filename.trim().length() > 0)
                    dstDoc.save(MyDir + filename.substring(0, 7) + "_out.docx");
                i++;
            }

        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("bm_extract")) {
            String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
            if(figText.trim().length() > 0)
                bm.setText("<Fig>"+figText.trim().substring(0, 7)+"</Fig>" + ControlChar.PARAGRAPH_BREAK);
        }
    }
}

@tahir.manzoor
Thanks for all the help.

@tahir.manzoor

Bookmark from labelled image is overlapping bookmark from anchored images. How to resolve the issue.How about using this bookmark

((Paragraph) paragraph).getChildNodes(NodeType.SHAPE, true).clear();

Paragraph p = ((Paragraph) paragraph);

p.getChildNodes(NodeType.SHAPE, true).clear();

p.appendChild(new BookmarkStart(interimdoc, "MyBookmark"));

Run run = new Run(interimdoc, "<Fig>Numbered_Figure</Fig>");

run.getFont().setColor(Color.RED);

p.getRuns().add(run);

p.appendChild(new BookmarkEnd(interimdoc, "MyBookmark"));

The code I am using is as follow.

UseCase5(doc, builder);
ExtractImages5(doc, “uc5”, builder);
doc.save(interim);

	pageproperty(interimdoc);
	numberedImagesExtraction(interimdoc);
	inlineImagesExtraction(interimdoc);
	
	
	UseCase1(doc, builder);
	ExtractImages(doc, "uc1", builder);

	UseCase2(doc, builder);
	ExtractImages(doc, "uc2", builder);
	
	//tableImagesExtraction(interimdoc);
	
	UseCase3(doc, builder);
	ExtractImages(doc, "uc2", builder);

	doc.save(interim);

public static void ExtractImages5(Document doc, String uc, DocumentBuilder builder) throws Exception
{
int i = 1;
String bookmark = “bm_extract”;
for (Bookmark bm : doc.getRange().getBookmarks()) {
if (bm.getName().startsWith(“Bookmark”)) {
bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
}
}
doc.updatePageLayout();

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            Node currentNode = bm.getBookmarkStart();
            try{
            while (currentNode.getNodeType() != NodeType.SHAPE && currentNode.getNodeType() != NodeType.GROUP_SHAPE)
                currentNode = currentNode.nextPreOrder(doc);

            builder.moveTo(currentNode);
            builder.startBookmark(bookmark + i);
            builder.moveTo(bm.getBookmarkEnd());
            builder.endBookmark(bookmark + i);
            i++;
            }
            catch(Exception e){
            	
            }
        }
    }
    
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.remove();
        }
    }
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks())
    {
        if(bm.getName().startsWith("bm_extract"))
        {
            ArrayList nodes =  ExtractContents.extractContent(bm.getBookmarkStart(), bm.getBookmarkEnd(), true);
            Document dstDoc = ExtractContents.generateDocument(doc, nodes);

            PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
            dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
            dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
            dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());

            dstDoc.updatePageLayout();
            if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
                dstDoc.getLastSection().getBody().getLastParagraph().remove();

            dstDoc.updatePageLayout();
            while(dstDoc.getFirstSection().getBody().getFirstParagraph()!= null && dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)
                dstDoc.getFirstSection().getBody().getFirstParagraph().remove();

            dstDoc.updatePageLayout();
            if(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            {
                String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
                if(filename.trim().length() > 0)
                    dstDoc.save(folderName + filename.substring(0, 7) + "_anchored.docx");
                i++;
            }

        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("bm_extract")) {
            String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
            if(figText.trim().length() > 0)
                bm.setText("<Anchored-Fig>"+figText.trim().substring(0, 7)+"</Anchored-Fig>" + ControlChar.PARAGRAPH_BREAK);
        }
    }
}
public static void UseCase5(Document doc, DocumentBuilder builder) throws Exception
{
    int bookmark = 1;
    int i = 1;
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
    {
        if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
        {
            System.out.println(paragraph.getText());
            Boolean bln = false;
            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();
            }

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                //builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else
            if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("BookmarkUC1" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("BookmarkUC1" + bookmark);
                bookmark++;
            }
        }
    }
}

public static void UseCase1(Document doc, DocumentBuilder builder) throws Exception
{
int bookmark = 1;
int i = 1;
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable) paragraphs)
{
if(paragraph.toString(SaveFormat.TEXT).trim().startsWith(“Fig”))
{
Boolean bln = false;
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();
bln = true;
}

            if(!bln)
                continue;

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("BookmarkUC1" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("BookmarkUC1" + bookmark);
                bookmark++;
            }
        }
    }
}

//Use case 2
public static void UseCase2(Document doc, DocumentBuilder builder) throws Exception
{
    int bookmark = 1;
    int i = 1;
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
    {
        if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
        {
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null &&
                    (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                        (
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(a)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(c)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(d)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().startsWith("(Fig"))
                        )
                    )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
                bln = true;
            }

            if(!bln)
                continue;

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("BookmarkUC1" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("BookmarkUC1" + bookmark);
                bookmark++;
            }
        }
    }
}
	public static void UseCase3(Document doc, DocumentBuilder builder) throws Exception
{
    int bookmark = 1;
    int i = 1;
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
    {
        if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
        {
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null &&
                    (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                        (
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(a)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(c)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(d)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().startsWith("(Fig"))
                        )
                    )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
                bln = true;
            }

            if(!bln)
                continue;

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                //builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else
            if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("Bookmark" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
        }
    }
}
public  static void ExtractImages(Document doc, String uc, DocumentBuilder builder) throws Exception
{
    int i = 1;
    String bookmark = "bm_extract";
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
        }
    }
    try{
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            Node currentNode = bm.getBookmarkStart();
            while (currentNode.getNodeType() != NodeType.SHAPE)
                currentNode = currentNode.nextPreOrder(doc);

            builder.moveTo(currentNode);
            builder.startBookmark(bookmark + i);
            builder.moveTo(bm.getBookmarkEnd());
            builder.endBookmark(bookmark + i);
            i++;
        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.remove();
        }
    }
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks())
    {
        if(bm.getName().startsWith("bm_extract"))
        {
        	 ArrayList nodes = ExtractContentBetweenParagraphs((Paragraph)bm.getBookmarkStart().getParentNode(), (Paragraph) bm.getBookmarkEnd().getParentNode());
	            Document dstDoc = generateDocument(doc, nodes);

            PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
            dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
            dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
            dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());

            dstDoc.updatePageLayout();
            if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
                dstDoc.getLastSection().getBody().getLastParagraph().remove();

            dstDoc.updatePageLayout();
            while(dstDoc.getFirstSection().getBody().getFirstParagraph()!= null && dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)
                dstDoc.getFirstSection().getBody().getFirstParagraph().remove();

            dstDoc.updatePageLayout();
            if(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            {
                String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
                if(filename.trim().length() > 0)
                    dstDoc.save(folderName + filename.substring(0, 7) + "_label_Image.docx");
                i++;
            }

        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("bm_extract")) {
            String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
            if(figText.trim().length() > 0)
                bm.setText("<Label-Fig>"+figText.trim().substring(0, 7)+"</Label-Fig>" + ControlChar.PARAGRAPH_BREAK);
        }
    }
    }
    catch(Exception e)
    {
    	
    }
}

@Saranya_Sekar

Thanks for your inquiry. Please share some detail about overlapped bookmark. Could you please check the output documents in my previous post and share the screenshot of problematic sections?