Image Placeholder Replacement

Hello,

Having some problems replacing the image placeholder.

Trying to follow the example: Find and Replace text in a Word file from C# / VB.NET applications

Sub Main()

    ' If using the Professional version, put your serial key below.
    ComponentInfo.SetLicense("FREE-LIMITED-KEY")

    Dim document = DocumentModel.Load("FindAndReplaceContent.docx")

    ' The easiest way how you can find and replace text is with "Replace" method.
    document.Content.Replace("%INSPECTION_CREATED_ON%", Now.ToShortDateString)
    document.Content.Replace("%MLS%", frmMain.statbar_MLS.Text)
    document.Content.Replace("%INSPECTION_ADDRESS%", frmMain.statbar_ClientAddress.Text)
    document.Content.Replace("%INSPECTION_COUNTY%", "County")
    document.Content.Replace("%INSPECTION_HOME_TYPE%", "Home Type")
    document.Content.Replace("%INSPECTION_ARCHITECTURES%", "Architectures")
    document.Content.Replace("%INSPECTION_HOME_BUILT%", "Home Built")
    document.Content.Replace("%SQUARE_FOOTAGE%", "Square Footage")
    document.Content.Replace("%INSPECTION_DATE_TIME%", "Inspection Date")

    ' Find an image placeholder.
    Dim picturePlaceholder = document.Content.Find("%Image1%").first()
    Dim picture As New Picture(document, "C:\Users\RayVi\OneDrive\Pictures\company_logo.png")
    ' Replace the placeholder text with image.
    picturePlaceholder.Set(picture.Content)

    document.Save("FoundAndReplacedContent.docx")

End Sub

Here is the link to the word document: https://file.io/dL76YKdemI7o

What kind of problem?

The document has 5 occurrences of "%Image1% text, you can replace all of them like this:

Dim picture As New Picture(document, "C:\Users\RayVi\OneDrive\Pictures\company_logo.png")
For Each picturePlaceholder In document.Content.Find("%Image1%")
    picturePlaceholder.Set(picture.Content)
Next

Or if you want to target just those content controls, then you could try this:

For Each contentControl In document.GetChildElements(True).OfType(Of IContentControl)()
    If contentControl.Properties.Title = "%Image1%" OrElse contentControl.Properties.Tag = "%Image1%" OrElse contentControl.Content.ToString().Contains("%Image1%") Then
        contentControl.Content.Set(picture.Content)
        If contentControl.ContentControlType = ContentControlType.Picture Then contentControl.Properties.IsShowingPlaceholderText = False
    End If
Next

I hope this helps.

Regards,
Mario

Hi Mario,

I guess we are getting the same results as before with images not showing. I found out why…
Apparently, under “Show document content” I needed to uncheck the “Show picture placeholders”. And now the images appear.

However, the contentControl In document.GetChildElements(True).OfType(Of IContentControl) gets an error of Type ‘IContentControl’ is not defined.

Hi Raymond,

Try adding the following Imports statement:

Imports GemBox.Document.CustomMarkups

If that doesn’t work, what version of GemBox.Document are you using?
Try using the current latest version:

Regards,
Mario

Hi Mario,
I have the latest version of GemBox.Document 35.0.1538
However, we are still getting the same error following this code snippet.

Error

OfType is an extension method from System.Linq, I’m afraid that I was unable to reproduce your issue.
Can you please send us a small Visual Studio project that reproduces this so that we can investigate it?

Absolutely! Here is a link: Dropbox - WaterMark.zip - Simplify your life

Thank you

Raymond, I’m not reproducing any issue with your project.

Hi Mario,

Thank you for your time. Strangely we aren’t getting the errors now.
The only thing we did was turn off my computer during the weekend.

Thank you