Free Support Forum - aspose.com

Includepicture and mergefields

Hi,

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


Thanks

Manfred


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,

Hi Awais Hafeez,

we are using Aspose.words for .NET 11.11.0.0
attached files are

Templates:

705-04-Offerte-Bild-kurz-1.doc
705-04-Offerte-Bild-kurz-1.docX

Output wordfiles:

00000001938.DOC (from 705-04-Offerte-Bild-kurz-1.doc)
00000001939.DOCX (from 705-04-Offerte-Bild-kurz-1.docx)

PDF-Files Created via document.print to Amyuni PDF-Converter:

00000001938.PDF (00000001938.DOC)
00000001939.PDF (00000001939.DOCX)

Thanks in advance

Manfred






Hi Manfred,


Thanks for the additional information. We are checking with this scenario and will get back to you soon.

Best regards,

Hi Manfred,


Thanks for your patience.

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");


Please let us know if this solves your problem.

Best regards,

Hi Awais,

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.

Thanks

Best regards

Manfred

Hi Manfred,


Thanks for the additional information. You can use the following code to resize the final image to the dimensions of parent Cell node:

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.FieldMergingCallback = new HandleMergeField();

doc.MailMerge.Execute(new string[] { "Ï3Ï10428ÏBILD1" },

new object[] { @"C:\\Temp\\Sample.png" });

foreach (FieldStart start in doc.GetChildNodes(NodeType.FieldStart, true))

{

if (start.FieldType == FieldType.FieldIncludePicture)

RemoveField(start);

}

doc.Save(@"C:\Temp\out.docx");


private static void RemoveField(FieldStart fieldStart)

{

Node currentNode = fieldStart;

bool isRemoving = true;

while (currentNode != null && isRemoving)

{

if (currentNode.NodeType == NodeType.FieldEnd)

isRemoving = false;

Node nextNode = currentNode.NextPreOrder(currentNode.Document);

currentNode.Remove();

currentNode = nextNode;

}

}


public class HandleMergeField : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (args.FieldName.Equals("Ï3Ï10428ÏBILD1"))

{

Cell cell = args.Field.Start.GetAncestor(NodeType.Cell) as Cell;

DocumentBuilder builder = new DocumentBuilder(args.Document);

builder.MoveTo(cell.FirstParagraph);

builder.InsertImage(args.FieldValue.ToString(),

cell.CellFormat.Width,

cell.ParentRow.RowFormat.Height);

}

}

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

// Do nothing.

}

}


I hope, this helps.

Best regards,