MailMerge (image) by URL

Hi There,

can’t seem to get this to work (most likely, because I don’t understand the MergeImageField Event Handler yet).

I have created an image handler for a web app that lets me extract images easily from a db with only one line of code. I would like to use this for a MailMerge to enter images in a word document. In essence my question is how to do an image MailMerge via URLs.

sample code :

Sub show_doc(ByVal ID As String)

Dim MyPath As String = HttpContext.Current.Request.PhysicalApplicationPath & "_sampledir/document.doc"

Dim doc As Document = New Document(MyPath)
Dim img1 As String = "ImgHandler.aspx?ID=" & ID
Dim img2 As String = "ImgHandler.aspx?ID=" & ID

' set field Names
Dim names As String() = {"Pic1", "Pic2"}
' Set field Values
Dim values As Object() = {img1, img2}
' MailMergeEventHandler
AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImage
doc.MailMerge.Execute(names, values)

' Send the Doc to the Client
doc.Save("FaxForm.doc", SaveFormat.Doc, SaveType.OpenInBrowser, HttpContext.Current.Response)

End Sub
Private Shared Sub HandleMergeImage(ByVal sender As Object, ByVal e As Aspose.Words.Reporting.MergeImageFieldEventArgs) 

' what goes here ????

End Sub

Thx in advance!
Marcus.

Hi

Thanks for your inquiry. You can try using the following code.

Dim img1 As String = "http://images.leftlanenews.com/content/1-geiger-cars-mustang-gt-520.jpg"

Dim img2 As String = "http://www.mustangautorent.com/images/MUSTANG517.jpg"

Dim doc As Document = New Document("in.doc")

Dim names As String() = {"Pic1", "Pic2"}

Dim values As String() = {img1, img2}

doc.MailMerge.Execute(names, values)

doc.Save("out.doc")

Also you can use MergeImageField event handler to get images from web. See the following code.

Sub Main()

Dim lic As Aspose.Words.License = New Aspose.Words.License()

lic.SetLicense("Aspose.Words.lic")

Dim img1 As String = "http://images.leftlanenews.com/content/1-geiger-cars-mustang-gt-520.jpg"

Dim img2 As String = "http://www.mustangautorent.com/images/MUSTANG517.jpg"

Dim doc As Document = New Document("in.doc")

Dim names As String() = {"Pic1", "Pic2"}

Dim values As String() = {img1, img2}

AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImage

doc.MailMerge.Execute(names, values)

doc.Save("out.doc")

End Sub
Private Sub HandleMergeImage(ByVal sender As Object, ByVal e As MergeImageFieldEventArgs)

Dim imgUrl As String = e.FieldValue

'Prepare the web page we will be asking for

Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(imgUrl), System.Net.HttpWebRequest)

request.Method = "GET"

request.ContentType = "image/jpeg"

request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0"

'Execute the request

Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), System.Net.HttpWebResponse)

'We will read data via the response stream

Dim resStream As Stream = response.GetResponseStream()

'Create Image

Dim img As Image = Image.FromStream(resStream)

'Dim img As Image = Image.FromFile(e.FieldValue)

e.Image = img

End Sub

If you have access to data base then you can get image directly from DB.

Best regards.

Hi Alexey,

thx for your response.
I’m still getting errors on your suggestions. The first method produces the error: “Parameter is not valid”.

The exact path I’m using is http://servername/picHandler.ashx?arg1=1&arg2=2 (using generic names). The path returns a valid image when used in a browser, though. I’ve tried a ‘direct’ path to an image (eg http://servername/image.jpg) and that works fine, so it seems to me that Aspose.Words is stumbling over the arguments in the url somehow.

On a related note, I’ve noticed that Aspose.Words does not interpret the basepath “~/” designator (in fact, it prepends the application path of my VStudio installation), which makes it a little awkward to construct the url - feature request ??

The second method gives me an intellisense error on Image.FromStream(resStream) stating FromStream is not a member of System.Web.UI.WebControls.Image and (as a result) won’t compile.

Thx for your assistance!
Rgds,
Marcus.

Hi

Image is member of System.Drawing namespace. Please try using System.Drawing.Image.

Also you can try using the following code snippet to format absolute URI

const string formatURLWithPort = "{0}{1}:{2}{3}";
const string formatURLWithoutPort = "{0}{1}{3}";
//Get parts of URI
string AppProtocol = "http://"; //Protocol
string AppHost = Request.Url.Host; //Host
int AppPort = Request.Url.Port; //Port (Default is 80)
string AppVirtualDir = Request.ApplicationPath; //Virtual directory
//Format application root URI
string rootPath = String.Format(AppPort != 80 ? formatURLWithPort : formatURLWithoutPort, AppProtocol, AppHost, AppPort, AppVirtualDir);

Best regards.