Floating Image insertion layout Issue

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);
            
docBuilder.write("hello123");
docBuilder.write("hello123");
            
Shape imageShapeObj1 = docBuilder.insertImage("test123.jpeg",100.0,100.0);
imageShapeObj1.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
imageShapeObj1.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);
			
imageShapeObj1.setWrapType(WrapType.SQUARE);
imageShapeObj1.setWrapSide(WrapSide.BOTH);
imageShapeObj1.setHorizontalAlignment(HorizontalAlignment.LEFT);
docBuilder.write("hello123");
docBuilder.write("hello123");
 
//What Break or any other way to achieve the expected result
docBuilder.insertBreak(BreakType.PAGE_BREAK);
            
docBuilder.write("hello123");
docBuilder.write("hello123");
            
Shape imageShapeObj2 = docBuilder.insertImage("test123.jpeg",100.0,100.0);
imageShapeObj2.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
imageShapeObj2.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);
imageShapeObj2.setWrapType(WrapType.SQUARE);
imageShapeObj2.setWrapSide(WrapSide.BOTH);
imageShapeObj2.setHorizontalAlignment(HorizontalAlignment.RIGHT);

docBuilder.write("hello123");
docBuilder.write("hello123");
            
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);
saveOptions.setSaveFormat(SaveFormat.DOCX);
            
doc.save("Test.docx",saveOptions);

Above is my code , My issue is the output of the code is this.

I want the output like this

TLDR : I want the a break or any solution such that it next content are after the floated image

How do i achieve the expected output ?

@VishalPro Could you please achieve what you need in MS Word and attach the expected output in DOCX format here for our reference?

Float issue.docx (167.2 KB)

I want the content after break to appear after the floated content inserted above.

@VishalPro Thank you for additional information. In your document the content is moved by a series of empty paragraphs:

You can do the same in the code.

Yes , i have done that to make you understand . But is there any other way to do it without me calculating the floating image’s size and manually adding line breaks ?

@VishalPro I am afraid, I do not see a way to achieve this. You can try using a table without borders and put the first shape in the first row and the second shape into the second row.

I dont think it is feasible for us. Thanks for your time.

1 Like