Hi,
What is the best way to programmatically insert an image in a word document?
The best information I have found is at http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-insert-images-from-a-database.html
But, I get an error when trying to do something like this.
First, I am unsure about the Image:XXX mail merge field.
I have just created a field like this: { MERGEFIELD Image:myimagename * MERGEFORMAT}
Is that the right way to do it?
So, I don’t really have any other mail merge fields for now, but I included two others for good measure: { MERGEFIELD myfirstfield * MERGEFORMAT}{ MERGEFIELD mysecondfield * MERGEFORMAT}
In my code I have
Private Sub mysub()
Dim myworddoc = New Aspose.Words.Document(“myworddoc.doc”)
Dim names = New String() {“myfirstfield”, “mysecondfield”, “Image:myimagename”}
Dim values = New String() {“First”, “Second”, “Third”}
AddHandler myworddoc.MailMerge.MergeImageField, AddressOf HandleMyImageMerge
myworddoc.MailMerge.Execute(names, values)
myworddoc.Save(“mydoc.pdf”)
End Sub
Private Shared Sub HandleMyImageMerge(ByVal sender As Object, ByVal e As Aspose.Words.Reporting.MergeImageFieldEventArgs)
e.ImageFileName = "c:\myimagefile.gif"
End Sub
However, this produces an error when it gets to the myworddoc.MailMerge.Execute line:
"Attempted to copy a by reference attribute ‘4152’, but the attribute does not expose the complex attribute interface."
What am I doing wrong? Is there a better way to do this?
Thanks.
<p class=“Default” style=“mso-list:none;mso-list-ins:“Beth Gassett” 20041102T1223”><o:p></o:p>
Hi
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks for your inquiry. DocumentBuilder provides several overloads of the InsertImage method that allow you to insert an inline or floating image from a file or .NET Image object. Please see the following link:
Please try using the following code
// Open the document.
Document doc = new Document(@"Test\in.doc");
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
// Go to bookmark (for example) or .MoveToMergeField("myimagename");
documentBuilder.MoveToBookmark("Image");
// Insert Image
documentBuilder.InsertImage(@"Test\img.jpg");
doc.Save(@"Test\out.doc");
Best regards,