Shape object movement - java

Hi Team,

I am writing to request your assistance with a document formatting issue that I have encountered. Specifically, the issue article has the first paragraph containing 2 shape objects and the second paragraph containing a null shape object. We expect the first node of the last shape to move to the next paragraph.

I have attached both the input and expected document files for your reference. As you can see, the formatting of the input document is incorrect, and I am hoping that you can help me rectify this issue.

Input: Input.zip (3.3 MB)

Excepted output: ExceptedOutput.zip (3.3 MB)

Thanks & Regards,
Mahesh

@Mahi39 to get the expected output you can use the following code:

Document doc = new Document(@"C:\Temp\input.doc");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

foreach (Paragraph p in doc.FirstSection.Body.Paragraphs)
{
    p.ParagraphFormat.NoSpaceBetweenParagraphsOfSameStyle = false;
    p.ParagraphFormat.SpaceAfterAuto = true;

    var shapes = p.GetChildNodes(NodeType.Shape, true);
    if(shapes.Count > 1)
    {
        Shape lastVisibleShape = (Shape)shapes.Last();
        var lastPageIndex = 1;
        float lastTop = 0;
        foreach(Shape shape in shapes)
        {
            enumerator.Current = collector.GetEntity(shape);
            if(enumerator.PageIndex > lastPageIndex || (enumerator.PageIndex == lastPageIndex && enumerator.Rectangle.Top > lastTop))
            {
                lastPageIndex = enumerator.PageIndex;
                lastTop = enumerator.Rectangle.Top;
                lastVisibleShape = shape;
            }
        }
        Paragraph newP = new Paragraph(doc);
        newP.Runs.Add(new Run(doc));

        lastVisibleShape.Top = 0;
        newP.PrependChild(lastVisibleShape);

        doc.FirstSection.Body.InsertAfter(newP, p);
    }
}

doc.Save(@"C:\Temp\output.doc");

@eduardo.canal, Is it possible to share the java code?

@Mahi39 this is the code in Java:

Document doc = new Document("C:\\Temp\\input.doc");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

for (Paragraph p : (Iterable<Paragraph>) doc.getFirstSection().getBody().getParagraphs()) {
    p.getParagraphFormat().setNoSpaceBetweenParagraphsOfSameStyle(false);
    p.getParagraphFormat().setSpaceAfterAuto(true);

    NodeCollection shapes = p.getChildNodes(NodeType.SHAPE, true);
    if (shapes.getCount() > 1) {
        Shape lastVisibleShape = (Shape) shapes.getLast();
        int lastPageIndex = 1;
        float lastTop = 0;
        for (Node node : (Iterable<Node>) shapes) {
            Shape shape = (Shape) node;
            enumerator.setCurrent(collector.getEntity(shape));
            if (enumerator.getPageIndex() > lastPageIndex
                    || (enumerator.getPageIndex() == lastPageIndex && enumerator.getRectangle().getTop() > lastTop)) {
                lastPageIndex = enumerator.getPageIndex();
                lastTop = enumerator.getRectangle().getTop();
                lastVisibleShape = shape;
            }
        }
        Paragraph newP = new Paragraph(doc);
        newP.getRuns().add(new Run(doc));

        lastVisibleShape.setTop(0);
        newP.prependChild(lastVisibleShape);

        doc.getFirstSection().getBody().insertAfter(newP, p);
    }
}

doc.save("C:\\Temp\\output.doc");