Replacing text portions with images

I work with vb.net / studio 2005 and have a web report (html string) from which I create a Word document. Eg. I paste a html-table string into Aspose.words. Everything fine so far. My problem is that I want to replace tags (eg. ###mykey###) in the created word document with a picture read from the database. I need to do it with the string tags because of the usage of the html code to create the document. Which is the best way of doing this? How can I assure that the picture is scaled down so that it will not go outside the page definition?

That question was already discussed in the forums. Here is the link:
https://forum.aspose.com/t/123314

Sorry to say, but I do not understand how to write this in VB and also how to pass a binary image string to the IncludeImageEvaluator because I do not want to use files for the images. The structure is the following that I need to use:
Calling function:

Loop
Read image from database
Call the replacement
Next

End calling function
IncludeImageEvaluator with a parameter with the read database object
Find the node
bulder.insertImage(imageobject received as a parameter)

End IncludeImageEvaluator

Well, in Visual Basic it would be something like that:

Private Sub IncludeImage()

Dim doc As Document = Open("Doc1.doc")
doc.Range.Replace(New Regex("\"), New ReplaceEvaluator(IncludeImageEvaluator), False)
SaveShow(doc, "Doc1 Out.doc")
End Sub
Private imageStream as MemoryStream;
Private Function IncludeImageEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction

Dim builder As DocumentBuilder = New DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(e.MatchNode)
builder.InsertImage(imageStream)
' Eraze "" text.
e.Replacement = ""
' commit replacement
Return ReplaceAction.Replace
End Function

Please note, that you cannot pass the image information into the replace evaluator as a parameter. You need to use some private member instead. If image file name is not suitable as image info parameter then you can pass the image information as Stream or Image object. InsertImage can take either as a parameter.
Hope this helps,

Still not working the .range.replace never calls IncludeImageEvaluator. Here my complete code:

Private bPic As Byte()
Public Function insertFormPictures(ByRef wordDoc As Aspose.Words.Document) As Boolean
Dim strCmd As String = ""
Dim conn As SqlConnection = Nothing
Dim dr As SqlDataReader = Nothing
Try
With wordDoc
.... get records from database
Do While dr.Read
bPic = dr("Attachment")
.Range.Replace(New Regex("###" & dr("Counter").ToString.Trim & "###"), New ReplaceEvaluator(AddressOf IncludeImageEvaluator), False)
Loop
End With
Return True
Catch ex As Exception
Return False
Finally
If Not dr Is Nothing Then dr.Close()
If Not conn Is Nothing Then
conn.Close()
conn.Dispose()
End If
End Try
End Function
Private Function IncludeImageEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction
Try
Dim builder As DocumentBuilder = New DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(e.MatchNode)
Dim bmp As New System.Drawing.Bitmap(New System.IO.MemoryStream(bPic))
builder.InsertImage(bmp)
e.Replacement = ""
Return ReplaceAction.Replace
Catch ex As Exception
End Try

Try to check what is passed into Regex ctor, i.e. what string is produced by “###” & dr(“Counter”).ToString.Trim & “###”.

Have checked this, seems ok in my eyes. Can the problem be that the search string is in a table?

No, it should not be a problem. Can you attach some of your documents here? I would like to check them myself.

Sorry for this: A logical problem: fetched the key from the wrong table. Completely my fault, owe you one beer…

Ok, one beer on you

Hi!

I today updated from Aspose.Words 9.1 to Aspose.Words 9.7. I noticed that several changes had happen to to syntax on commands which have produced big problems because I am not able to get the software to run anymore before the syntax is changed in my code. How should the following VB.NET row be written to get it work with the Aspose.Words 9.7:

doc.Range.Replace(New Regex("<image>"), New ReplaceEvaluator(IncludeImageEvaluator), False)

Regards,
Bo Andér

Hello,
Thanks for your request.
For your goal, you should use IReplacingCallback. Please see the article in our documentation. In it you will find an example of the Document.Range.Replace method.
All changes regarding the events and delegates, you can see here. This is a list of what changed in version 9.2.0.0.

I have now tried to convert my code to follow the documentation you mentioned. Now I still have problems how I can access bPic within IReplacingCallback_Replacing (see the code in the beginning of this thread). E.g. it is that code I am trying to convert to the new syntax.

Here is my code:

Public Class QARoutinesWords
Private bPic As Byte()

Public Function insertFormPictures(ByRef wordDoc As Aspose.Words.Document, ByVal formNumber As Long) As Boolean
Dim sqlStr As String = ""
Dim conn As SqlConnection = Nothing
Dim dr As SqlDataReader = Nothing
Try
With wordDoc
If HttpContext.Current.Session("Questionnaire") = 0 Then Return False
conn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("DBLocation").ToString)
sqlStr = "SELECT Question.Counter AS Counter, Attachments.Attachment"
sqlStr &= " FROM Attachments"
sqlStr &= " INNER JOIN Question ON Attachments.Question=Question.counter"
sqlStr &= " WHERE Question.Research=" & HttpContext.Current.Session("Questionnaire")
sqlStr &= " AND Question.QType=" & TYPEPICTUREUPLOAD
sqlStr &= " AND QAnswer = " & formNumber
Dim cmd As New SqlCommand(sqlStr, conn)
conn.Open()
dr = cmd.ExecuteReader()
cmd.Dispose()
Do While dr.Read
bPic = dr("Attachment")
.Range.Replace(New Regex("###" & dr("Counter").ToString.Trim & "###"), New InsertImageAtReplaceHandler(), False)
Loop
End With
Return True
Catch ex As Exception
QARoutinesLog.doLog(ex.ToString, TYPEERRORWORD)
Return False
Finally
If Not dr Is Nothing Then dr.Close()
If Not conn Is Nothing Then
conn.Close()
conn.Dispose()
End If
End Try
End Function

Private Class InsertImageAtReplaceHandler
Implements IReplacingCallback
Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
Try
Dim QARoutinesWords As New QARoutinesWords
Dim builder As DocumentBuilder = New DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(e.MatchNode)
Dim bmp As New System.Drawing.Bitmap(New System.IO.MemoryStream(QARoutinesWords.bPic))
builder.InsertImage(bmp)
e.Replacement = ""
Return ReplaceAction.Replace
Catch ex As Exception
End Try
End Function
End Class
End Class

Hi
Thanks for your request. I think, in your case you can put bitmap into constructor. For instance see the following code:

Private Class InsertImageAtReplaceHandler
Implements IReplacingCallback
Public Sub New(ByVal bmp As System.Drawing.Bitmap)
mBmp = bmp
End Sub
Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
Try
Dim QARoutinesWords As New QARoutinesWords
Dim builder As DocumentBuilder = New DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(e.MatchNode)
builder.InsertImage(mBmp)
e.Replacement = ""
Return ReplaceAction.Replace
Catch ex As Exception
End Try
End Function
Private mBmp As System.Drawing.Bitmap
End Class

Hope this helps. Please let us know if you need more assistance, We will be glad to help you.
Best regards,