Free Support Forum - aspose.com

Showing Image (dynamically) in PDF from Word Template

Dear Support,

I have been using the Aspose PDF from Word template using the ExecuteByRegions option and is working well. Now I am confronted with a situation where one such data element to be shown is an Image (from the webserver not from DB). Image works well when creating new PDF from scratch I am unable to get the image on the PDF derived from the .DOT file. No error but I get 'System.Byte()' in place of the intended bookmark. Here's the code. Many thanx in advance - Rgds Pavan

Dim license As New Aspose.Word.License()

license.SetLicense("Aspose.Enterprise.Word.lic")

Dim doc As New Document(Request.MapPath(Request.ApplicationPath) & "\QoutationSamples\StandardTemplate.dot")

Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream()

Dim b As System.Drawing.Bitmap = New System.Drawing.Bitmap(Request.MapPath(Request.ApplicationPath) & "\Imgs\bottom_logo.gif")

b.Save(mstream, System.Drawing.Imaging.ImageFormat.Gif)

Dim licensePDF As New Aspose.Pdf.License()

licensePDF.SetLicense("Aspose.PDF.lic")

Dim pdf1 As Pdf = New Pdf()

Dim sec1 As Aspose.Pdf.Section = New Aspose.Pdf.Section(pdf1)

pdf1.Sections.Add(sec1)

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)

sec1.Paragraphs.Add(image1)

image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp

image1.ImageInfo.OpenType = ImageOpenType.Memory

image1.ImageScale = 0.5F

Dim reader As System.IO.BinaryReader = New System.IO.BinaryReader(mstream)

mstream.Position = 0

image1.ImageInfo.MemoryData = reader.ReadBytes(CType(mstream.Length, Integer))

b.Dispose()

Dim fieldNames() As String = {"CUST_NAME"}

Dim fieldValues() As Object = {image1.ImageInfo.MemoryData}

doc.MailMerge.Execute(fieldNames, fieldValues)

'pdf1.Save("MyQoute.pdf", Aspose.Pdf.SaveType.OpenInAcrobat, Me.Response)

SendToBrowserAsPdf(doc)

Response.End()

Thanks for considering Aspose.

Your codes seem to be correct, would u mind attaching the StandardTemplate.dot & bottom_logo.gif? We want to have a test to find out reasons.

You can alternatively mail them to kevin.zuo@aspose.com. Thanks for cooperations.

Thanks for considering Aspose.

I 've gotten the requested files, after I re-ran your codes, I just got a pdf file including the bottom_logo.gif. It seems there are no operations on dot document in your codes. It makes me feel confusion. So could u illustrate extactly what did u want to do with those files?

Dear Kevin,

Could you send me your output? I use the DOT file as a template

Dim doc As New Document(Request.MapPath(Request.ApplicationPath) & "\QoutationSamples\StandardTemplate.dot")

After opening this I pass the data. The final output is a PDF.

SendToBrowserAsPdf(doc)

Rgds

Pavan

Thanks for considering Aspose.

I found this error may be caused by uncorrectly using memory stream.

Please try the following codes .

Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream

Dim pdf1 As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf
Dim sec1 As Aspose.Pdf.Section = pdf1.Sections.Add()

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)

image1.ImageInfo.ImageFileType = Aspose.Pdf.ImageFileType.Gif

image1.ImageInfo.ImageStream = mstream

sec1.Paragraphs.Add(image1)

pdf1.Save("MyQoute.pdf")

and also check out other post for helpful information:

http://www.aspose.com/Community/Forums/41930/ShowPost.aspx

Dear Kevin,

Thanx for the continuing support. This will work but would only show the image. My case is to create a PDF from a Template by passing the image to the MailMerge fields in the template. So creating a PDF from scratch will not help. Infact in my code the most important part would be

Dim fieldNames() As String = {"CUST_NAME"}

Dim fieldValues() As Object = {image1.ImageInfo.MemoryData}

doc.MailMerge.Execute(fieldNames, fieldValues)

Could you check whther this is possible?

cheers

Pavan

Dear Pavan,

Thank you for considering Aspose.

That is a problem of Aspose.Words side. Can you please post your question in the Aspose.Words forum?

Dear Kevin,

It is indeed a Word issue and MergeField to be created should have a 'Image:' in front. Now it works. Thanx for your help.

rgds

Pavan