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

Free Support Forum - aspose.com

Replacing text with an image- centered on the text

Hello,

I’m attempting to load a document and replace text with an image. I need the image vertically and horizontally centered from the center of the text, i.e. the image’s center would be on the “a” in “bar”.

The current samples using Range.Replace and DocumentBuilder only show how to place an image inline before or after the text – not floating in the layout center of it. Is this possible?

I’m currently doing this successfully using Word automation by anchoring a picture shape to the paragraph and offsetting it to the correct place.

I’m evaluating Aspose.Words for purchase as a replacement for this Word automation as we want to move this onto a proper server. This is the last piece of the puzzle to solve before I give the go ahead. Please help!

Hi Cory,

Thanks for your inquiry. I am checking with this scenario and will get back to you soon.

Best regards,

Thank you Awais. Do you have any updates?

Hi Cory,

Thanks for your inquiry.

I have prepared a basic code which I think will help you in achieving what you are looking for. This code finds all occurrences of a particular word in a document and inserts a floating rectangle shape at the horizontal/vertical centre of each word. Please see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
string text = "here";
WalkOver(enumerator, text);
enumerator.MoveNext();
doc.Save(@"C:\Temp\out.docx");
private static void WalkOver(LayoutEnumerator enumerator, string text)
{
    do
    {
        if (enumerator.MoveLastChild())
        {
            WalkOver(enumerator, text);
            enumerator.MoveParent();
        }
        if (enumerator.Type == LayoutEntityType.Span)
        {
            if (enumerator.Text.Contains(text))
            {
                Shape rectangle = new Shape(enumerator.Document, ShapeType.Rectangle);
                rectangle.Width = enumerator.Rectangle.Width;
                rectangle.Height = enumerator.Rectangle.Height;
                rectangle.Top = enumerator.Rectangle.Top;
                rectangle.Left = enumerator.Rectangle.Left;
                rectangle.Fill.Opacity = 0.5;
                rectangle.FillColor = Color.Red;
                rectangle.WrapType = WrapType.None;
                rectangle.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
                rectangle.RelativeVerticalPosition = RelativeVerticalPosition.Page;
                enumerator.Document.FirstSection.Body.FirstParagraph.AppendChild(rectangle);
            }
        }
        if (enumerator.Type == LayoutEntityType.Page)
            return;
    }
    while (enumerator.MovePrevious());
}

I hope, this helps.

PS: This code anchors all Shape nodes to the first Paragraph, but, I think with the help of the ‘DocumentLayoutHelper’ project here you can achieve this either. I have also attached sample input/output Word documents here for your reference.

Best regards,

Thank you, that worked brilliantly!

Hi Cory,

Thanks for your feedback. Please let us know any time you have any further queries. We are always glad to help you.

Best regards,

Hi Awais,

I’m having some trouble getting this technique to save to PDF (doc.Save(“out.pdf”, SaveFormat.Pdf)). For some reason the floating shape is left out of the resulting PDF completely. Please help!

When saving to DOCX (doc.Save(“out.docx”, SaveFormat.Docx)), the shapes show up fine.

Hi Cory,
To ensure a timely and accurate response please supply us with the following information, if you cannot supply us with this information we will not be able to investigate your issue and raise a ticket.

  • What version of Aspose.Words for .NET are you using?
  • Please supply us with the code from your application that is causing the issue
  • Please supply us with the input Word document that is causing the issue
  • Please supply us with the output PDF document showing the undesired behavior
  • Please supply us with the output DOCX document showing the desired behavior

As soon as you get these pieces of information to us we’ll start our investigation into your issue.
Many thanks,

Hi Awais,

I’m using v13.4.0.0. I’ve attached all of the things you’ve asked for. Thanks!

Hi Cory,

Thanks for the additional information. In your case, you simply need to call Document.UpdatePageLayout method before rendering the output document to PDF format as follows:

doc.Save("out.docx",
SaveFormat.Docx);
doc.UpdatePageLayout();
doc.Save("out.pdf", SaveFormat.Pdf);

Also, please refer to the API documentation below:
https://reference.aspose.com/words/net/aspose.words/document/updatepagelayout/

I hope, this helps.

Best regards,