we are using Aspose.Words .NET for a while now. Our users can create templates with mailmergefields.
For inserting images in these templates we are using INCLUDEPICTURE Mergefield ‘variable’. These construct is often used within tables. With help from the Aspose Support (Thanks) we where able to insert these images and fit them to the size of the table cell. This works fine with .DOC documents but not with DOCX documents. We found out, that Aspose.Words ‘changes’ Tables to shapes when reading a DOC document. But not when reading ‘DOCX’ documents.
Any idea what to do?
Code:
For Each shape As Shape In sdoc.GetChildNodes(NodeType.Shape, True)
If (shape.HasImage And shape.ImageData.IsLink) Then
Dim match As Match = Regex.Match(shape.ImageData.SourceFullName, "(?<=«)[^}]*(?=»)")
' If we find an image with SourceFullName containing merge field markers then
' we know this is an INCLUDEPICTURE field with a merge field inside it.
If (match.Success) Then
' Extract the name from the merge field and insert a proper merge field as a child of the shape.
shape.AppendChild(New Paragraph(sdoc))
builder.MoveTo(shape.FirstParagraph)
Dim c As Aspose.Words.Tables.Cell = DirectCast(shape.GetAncestor(NodeType.Cell), Aspose.Words.Tables.Cell)
If Not c Is Nothing Then
And
If shape IsNot Nothing Then
If shape.HasImage Then
lwidth = shape.Width
lHeight = shape.Height
Try
If args.FieldValue <> "" Then
Dim lshape As Shape = lImageSizeBuilder.InsertImage(args.FieldValue.ToString())
ftBildBerechnen(lwidth, lHeight, lshape.ImageData.ImageSize.WidthPixels, lshape.ImageData.ImageSize.HeightPixels, lwidth, lHeight)
End If
Catch
End Try
If args.FieldValue <> "" Then
shape.Width = lwidth
shape.Height = lHeight
shape.ImageData.SourceFullName = args.FieldValue.ToString()
End If
If shape.ParentNode IsNot Nothing Then
If args.FieldValue = "" Then
Try
shape.Remove()
Catch ex As Exception
End Try
Else
Try
shape.RemoveAllChildren()
Catch ex As Exception
End Try
End If
End If
End If
End If
Hi Manfred,
Thanks for your inquiry. To ensure a timely and accurate response please supply us with the following information, if you cannot supply us with this information we will not be able to investigate your issue and raise a ticket.
What version of Aspose.Words for .NET are you using?
Please supply us with the input template document that is causing the issue
Please supply us with the output documents (DOC and DOCX) showing the undesired behavior
As soon as you get these pieces of information to us we’ll start our investigation into your issue.
Many thanks,
I think, you can use LoadOptions.PreserveIncludePictureField property to override the default behaviour and if you need the INCLUDEPICTURE field to be preserved, for example, if you wish to update it programmatically. And of course you can use a MERGEFIELD as a child field to dynamically change the source path of the picture. Please see the following code, it produces all formats correctly:
LoadOptions
options = new LoadOptions();
options.LoadFormat = LoadFormat.Doc;
options.PreserveIncludePictureField = true;
Document doc = new Document(@"C:\Temp\705-04-Offerte-Bild-kurz-1.docx", options);
doc.MailMerge.Execute(new string[] { "╧3╧10428╧BILD1" }, new object[] { @"C:\\Temp\\Sample.png" });
doc.Save(@"C:\Temp\out.doc");
doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.pdf");
thanks for your answer. Unfortunately this is not a solution for us. In the code snippet in our first mail, you can see that the ‘old’ behaviour of aspose.word.dll made it possible for us to scale the images to fit to the cell/shape, which isn’t done by default with ‘IncludePicture’ (MS-Word does it, but Aspose doesn’t)
I’m working on solution. Any hints are welcome.