How to merge paragraphs with images using Java

Hi team

Here i had attached two sample files which i am facing an issue . In part images each image present in seperate paragraph so i need to convert it into a single paragraph so that it will be easy to extract the images without any difficulties.I am also attached a sample output kindly go through and provide some solution regarding my queries.

Sample Input:input.zip (4.5 MB)

Sample output:output.zip (4.1 MB)

Thanks
Janani

@jan.kathir

We already shared the code example to extract the images from the document. You do not need to merge shapes of paragraph. Please check the code example shared in following post.

@tahir.manzoor
Thanks for your reply.
We are using preclean up process so that i need to merge shapes paragraph please provide some solution for above mentioned requirement with the help of same sample documents.

@jan.kathir

Please iterate over paragraph nodes and get the paragraphs before Fig caption. You can use following method to merge the paragraphs. Hope this helps you.

public static void mergeParagraphs(Paragraph dstPar, Paragraph srcPar)
{
    //move all content from source paragraph into the destination paragraph
    while (srcPar.hasChildNodes())
        dstPar.appendChild(srcPar.getFirstChild());

    //Remove source paragraph
    srcPar.remove();
}

@tahir.manzoor
As you said i tried but didn’t archive the correct output can you please give me the solution in detail code.Here i should merge two paragraph which contains two images with seperate paragraph.kindly provide the solution in detail manner is very needful to me.

@jan.kathir

We will write the code for your scenario and will share it with you soon.

@jan.kathir

Please use the following code example to merge the images. We have attached the output document with this post for your kind reference. Hope this helps you.
20.4.zip (4.0 MB)

Document doc = new Document(MyDir + "Chp_38.doc");

DocumentBuilder builder = new DocumentBuilder(doc);
int bm = 1;
ArrayList nodes = new ArrayList();

for (Paragraph  paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
    {
        Node previousPara = paragraph.getPreviousSibling();
        while (previousPara != null
                && previousPara.getNodeType() == NodeType.PARAGRAPH
                && previousPara.toString(SaveFormat.TEXT).trim().length() == 0)
        {
            previousPara = previousPara.getPreviousSibling();
        }

        builder.moveTo(previousPara);
        builder.startBookmark("bookmark" + bm);
        builder.moveTo(paragraph);
        builder.endBookmark("bookmark" + bm);
        bm++;
    }
}
doc.updatePageLayout();
for(Bookmark bookmark : doc.getRange().getBookmarks())
{
    if(!bookmark.getName().startsWith("bookmark"))
        continue;

    Node node = bookmark.getBookmarkStart();

    while (node != bookmark.getBookmarkEnd())
    {
        node = node.nextPreOrder(doc);

        if(node.getNodeType() == NodeType.PARAGRAPH)
            nodes.add(node);
    }
    merge_Paragraphs(nodes);
    nodes.clear();
}
doc.save(MyDir + "20.4.docx");

public static void merge_Paragraphs(ArrayList nodes)
{
    Paragraph dstPar = (Paragraph) nodes.get(0);
    for(int j = 1; j < nodes.size(); j++)
    {
        Paragraph srcPar = (Paragraph) nodes.get(j);
        //move all content from source paragraph into the destination paragraph
        while (srcPar.hasChildNodes())
            dstPar.appendChild(srcPar.getFirstChild());

        //Remove source paragraph
        srcPar.remove();
    }
}