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

# How to inline images with text by using aspose.words for .NET

@nethmi I have checked your HTML and code on my side and unfortunately, I cannot reproduce the same output document you have shared. Could you please attach your output DOCX document with the problem?
I suspect the shapes are anchored to the same paragraph and as a solution you can check whether the shapes are in the same paragraph and placed on the same page, if so you can adjust position of such shapes and place them one above another. However, this in only my guess. Once I have your real problematic document I can analyze the issue closer.

ok i’ll send you

1 Like

Hiii

HTML MYHTML.zip (2.1 KB)
word document document.docx (17.5 KB)
used Image picture.jpg (5.7 KB)

In the 3rd page you can see the two wrapped images.
current outcomecurrent.PNG.jpg (135.9 KB)

expected outcome expected.PNG.jpg (163.7 KB)

shapes are in different paragraphs

@nethmi Thank you for additional information. I have create a code example that demonstrates the basic technique of adjusting shapes positions to get the required output. But note, the code demonstrates only the basic technique and does not guaranty to work with more complicated cases. For more complicated cases, you have to implement more complicated logic to adjust positions of shapes on the page. Implementation of such logic is out of Aspose.Words scope.

Document doc = new Document(@"C:\Temp\in.docx");
CorrectShapesPosition(doc);
doc.Save(@"C:\Temp\out.docx");

private static void CorrectShapesPosition(Document doc)
{
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

// Get all shapes in the document.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Collect shapes per page.
Dictionary<int, List<ShapeRect>> shapesPerPage = new Dictionary<int, List<ShapeRect>>();
foreach (Shape s in shapes)
{
enumerator.Current = collector.GetEntity(s);
if (!shapesPerPage.ContainsKey(enumerator.PageIndex))

}

foreach (int page in shapesPerPage.Keys)
{
List<ShapeRect> shapesOnPage = shapesPerPage[page];
// If there is only one shape on the page no actiona is required.
if (shapesOnPage.Count == 1)
continue;

// Adjust vertical position of shapes to avoid overlapping.
// The code demonstrates the basic technique and does not guaranly to work in all case.
// More complicated cases requires implemeting core complicated logic.
for (int i = 0; i < shapesOnPage.Count - 1; i++)
{
ShapeRect current = shapesOnPage[i];
ShapeRect next = shapesOnPage[i + 1];
if (current.Rectangle.Bottom > next.Rectangle.Top)
current.Shape.Top -= current.Rectangle.Bottom - next.Rectangle.Top;
}
}
}

private class ShapeRect
{
public ShapeRect(Shape shape, RectangleF rect)
{
mShape = shape;
mRect = rect;
}

public Shape Shape { get { return mShape; } }
public RectangleF Rectangle { get { return mRect; } }

private Shape mShape;
private RectangleF mRect;
}


here is the output document produced on my side: out.docx (17.5 KB)

1 Like

got it. Thank you very much for your effort.

1 Like

Hii

When I make changes in the HTML, my wrapped images in the converted word document, go here and there. sometimes those pictures go to another page. why those positions are changing like that?? This happens only for wrapped images. They escape their wrapped content and go everywhere in the document, when I made new changes to the document.

changes means -I gave page margins to the document. I added header image to the document. I used aspose.words logics to avoid page breaks, I add extra content to the html, I removed some paragraphs from the html etc.

Do you have a solution to bind those wrapped images with the wrapped content to avoid such kind of issue???

@nethmi You should note that both HTML and MS Word documents are flow documents and when you add some content into the document other content is reflowed. Images in MS Word documents are anchored to a paragraph. If anchor point is moved the shape position is also moved and document content is reflowed again.
To fix position of shape on the page, you can set its relative vertical and horizontal position to Page, just like I have suggested in this post.

// Set relative shape position to page.
s.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
s.RelativeVerticalPosition = RelativeVerticalPosition.Page;


But in this case you have to calculate absolute position of shape on the page. And anyways if the anchor point the absolutely positioned shape will be moved to the next page, the shape will be moved to the next page too and content will be reflowed.

1 Like

got it. I’ll try

1 Like