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

Free Support Forum - aspose.com

Problems with mail merging images

Dear Sir,
In our application the mail merge document loads an image which is from a file but the reference of whihc is in the database. When the image is large it throws and error saying that, the ht should be less than 1584 pts. Is there a way around to this so that the scanned image will be resized on the fly while uploading it? I tried to physically reduce the image sizes from the file using image ready and now it does not throw exception. But the image loads as if it zoomed out. How can I sort out these problems?
This is the code used for uploading the file

Function uploadFile(ByVal MyFile As HtmlInputFile, ByVal strSide As String) As Boolean
If Not (MyFile.PostedFile Is Nothing) Then
Dim intFileNameLength As Integer
Dim strFileNamePath, strFileExtention, strFileNameOnly, strNewFileName As String
strFileNamePath = MyFile.PostedFile.FileName
strFileExtention = LCase(Right(strFileNamePath, Len(strFileNamePath) - InStrRev(strFileNamePath, ".")))
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
strNewFileName = "Ref" + Request.QueryString("ReferralID") + "_" + UCase(txtSurname.Text) + UCase(Left(txtForename.Text, 1)) + strSide + "." + strFileExtention
If strFileExtention = "jpg" Or strFileExtention = "jpeg" Or strFileExtention = "gif" Then
Try
MyFile.PostedFile.SaveAs(MapPath("ReferralImages") & "\" & strNewFileName)
If strSide = "F" Then
strReferralFront = strNewFileName
Else
strReferralBack = strNewFileName
End If
Return True
Catch
Return False
End Try
Else
popUp("*** Invalid file type! You may only upload JPG or GIF files ***", 0, "")
If File.Exists(MapPath("ReferralImages") & "\" & strReferralFront) Then
File.Delete(MapPath("ReferralImages") & "\" & strReferralFront)
End If
Return False
End If
End If
End Function

Please hlep as the application is being used extensively
Regds

Hi
Thanks for your inquiry. I think that you can try using the following function to resize images on fly.

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
''' 
''' Resize image
''' 
''' Source image
''' Target width
''' Target height
''' Resized image
''' 
Function ResizeImage(ByVal sourceImage As Image, ByVal targetWidth As Integer, ByVal targetHeight As Integer) As Image
Dim ratioWidth As Double = CType(sourceImage.Width, Double) / targetWidth
Dim ratioHeight As Double = CType(sourceImage.Height, Double) / targetHeight
If (ratioWidth > ratioHeight) Then
targetHeight = CType((targetHeight * (ratioHeight / ratioWidth)), Integer)
Else
If (ratioWidth < ratioHeight) Then
targetWidth = CType((targetWidth * (ratioWidth / ratioHeight)), Integer)
End If
End If
'create target image
Dim targetImage As Bitmap = New Bitmap(targetWidth, targetHeight, PixelFormat.Format24bppRgb)
targetImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution)
'set transform parameters 
Dim g As Graphics = Graphics.FromImage(targetImage)
Using (g)
g.CompositingQuality = CompositingQuality.HighQuality
g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
'resize image
Dim rc As Rectangle = New Rectangle(0, 0, targetImage.Width, targetImage.Height)
g.DrawImage(sourceImage, rc)
End Using
Return CType(targetImage, Image)
End Function

I hope this could help you.
Best regards.

Thanks for that, but what can i do for the exisiting images which though I have reduced size appears zoomed out?

I think that you can try using MergeImageField event. You can resize your images in this event handler. See the following link for more information.
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
Best regards.

I am really sorry to ask you as to how to insert the event handler in this case. For the existing big images it come s up with the error “The image should be between 0 to 1584 pixels”
This is how we are populating the image.

Private Function createWordDoc() As Document
' Dim app As word = New Word
Dim doc As New Document(MapPath("CaseFile") + "\" + reportName + ".doc")
Dim license As License = New License
license.SetLicense(MapPath("License") + "\Aspose.Custom.lic")
'Insert images into word document
Dim imagesTable As DataTable = New DataTable("ReferralImages")
imagesTable.Columns.Add("ReferralDocFront")
imagesTable.Columns.Add("ReferralDocBack")
imagesTable.Rows.Add(New Object() {imagesPath + "\" + executeQuery("SELECT ReferralDocFront FROM tblReferral WHERE ReferralID = " & Request.QueryString("ReferralID"), "ReferralDocFront"), imagesPath + "\" + executeQuery("SELECT ReferralDocBack FROM tblReferral WHERE ReferralID = " & Request.QueryString("ReferralID"), "ReferralDocBack")})
doc.MailMerge.Execute(imagesTable)

Please hlep

Hi
Thanks for your request. You can try using the following code as example.

Sub Main()
Dim doc As Document = New Document("in.doc")
Dim names As String() = {"myImage"}
Dim values As String() = {"test.jpg"}
AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImageResize
doc.MailMerge.Execute(names, values)
doc.Save("out.doc")
End Sub
Private Sub HandleMergeImageResize(ByVal sender As Object, ByVal e As MergeImageFieldEventArgs)
Dim img As Image = Image.FromFile(e.FieldValue)
e.Image = ResizeImage(img, 100, 100)
End Sub

I hope this could help you.
Best regards.

Where do I use this code? While displaying? I did send you the code. I am still at loss as to how I insert this code within my code Can you please help

Hi
Please see the following code.

Private Function createWordDoc() As Document
' Dim app As word = New Word
Dim doc As New Document(MapPath("CaseFile") + "\" + reportName + ".doc")
Dim license As License = New License
license.SetLicense(MapPath("License") + "\Aspose.Custom.lic")
'Insert images into word document
Dim imagesTable As DataTable = New DataTable("ReferralImages")
imagesTable.Columns.Add("ReferralDocFront")
imagesTable.Columns.Add("ReferralDocBack")
imagesTable.Rows.Add(New Object() {imagesPath + "\" + executeQuery("SELECT ReferralDocFront FROM tblReferral WHERE ReferralID = " & Request.QueryString("ReferralID"), "ReferralDocFront"), imagesPath + "\" + executeQuery("SELECT ReferralDocBack FROM tblReferral WHERE ReferralID = " & Request.QueryString("ReferralID"), "ReferralDocBack")})
AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImageResize
doc.MailMerge.Execute(imagesTable)
Private Sub HandleMergeImageResize(ByVal sender As Object, ByVal e As MergeImageFieldEventArgs)
Dim img As Image = Image.FromFile(e.FieldValue)
e.Image = ResizeImage(img, 100, 100)
End Sub

Hope this helps.
Best regards.

I tried exactly the same thing and still it comes up with the error! That is when I wrote to you

Could you please attach your template and sample image for testing?
Best regards.

I am attaching the sample gif

Am attaching the template

Please help, There are quite a lot of problems being reported and whne I manually reduce the size of the image, it does not come up with an error but loads zoomed out.

Hi
Thanks for additional information. I can’t reproduce this problem on my side. I tried to use the following code and no error occurs.

Dim doc As Document = New Document("OTFile.doc")
Dim names As String() = {"ReferralDocFront", "ReferralDocBack"}
Dim values As String() = {"Ref2153_PRYNNDF.gif", "Ref2153_PRYNNDF.gif"}
doc.MailMerge.Execute(names, values)
doc.Save("out.doc")

Which version of Aspose.Words do you use? Maybe you should try using the latest version.
Best regards.

The dll version is 3.5.0. If you try to embed the gif in the template that I provided then may be you can reprduce the error. As it was a patinet referral I had to mask all info from the file bwefore attaching

As we have a license already can we downlaod the latest dll? If we can then form where shall I download?

Hi
Every Aspose license carries a one-year subscription for free upgrades to any new versions or fixes that come out during this time. You can download the latest version from here.
https://releases.aspose.com/words/net
Best regards.

We have version 3.5.0.0. So which version of aspose should I downlaod so that it will work fine with the existing reports?

Hi
Open your license file using notepad. You will see the following line there.
20081031
This means that the latest version that you can download is (will be) released on 10/31/2008.
Best regards.

Dear Alexey,
20060301 this is what it syas on our license. Now which version can I downlaod as I could find only the 21/01/2006,30/04/2006 etc in your download page?
Could you reproduce the error by embedding thre image on the template? I keep receiving phone calls from my customers, and I wish i could sort this out quickly. Sorry to be a pain.