Extract Mailmergefield from within Includepicture

Hi All,

I’m new to Aspose (excuse my bad english). We are using wordautomation for mailmerge purposes. We want to go away from automation and are now using the Aspose tools. We are using INCLUDEPICTURE Mergefield ‘fieldname’ in our existing word templates. I read that this is not supported by Aspose. Instead I have to use image:Mergefield ‘fieldname’. Because our customers can create their own templates, I have to change this 'on the fly’
Now I’m looking for a codesample to search all INCLUDEPICTURE-fields, check if there is a mergefield instead of a filename, extract the mergefield and replace the construction with image:Mergefield ‘fieldname’.
Can anyone help me

Thanks

Regards
Manfred

Hi Manfred,
Thanks for your inquiry. I think, you can achieve what you need after reading the article suggested here:
https://docs.aspose.com/words/net/working-with-form-fields/
I hope, this will help.
Best Regards,

Hi Manfred,

Thanks for your inquiry.

In addition, I can think of another way you may want to use to process such merge fields without doing any field replacement.

First a little bit a background. The reason why such a setup can not be merged is because INCLUDEPICTURE fields are imported into Aspose.Words as Shape objects. This allows you to access image properties but at the same time restricts any extra parameters of the field, in this case the included merge field. This most likely won’t be fixed any time soon as this case doesn’t appear to often.

You can however still merge INCLUDEPICTURE fields which contain merge fields by using the work around below. Any nested field in INCLUDEPICTURE appears through the SourceFullName property. Using this we can parse the field name and insert a proper field into the model.

In the example below the merge field name in the picture field is called “ImagePath”.

doc.MailMerge.FieldMergingCallback = new HandleIncludePictureFields(doc);
doc.MailMerge.Execute(new string[]
{
    "ImagePath"
}, new object[]
{
    "Image.jpg"
});

public class HandleIncludePictureFields: IFieldMergingCallback
{
    public HandleIncludePictureFields(Document doc)
    {
        DocumentBuilder builder = new DocumentBuilder(doc);
        foreach(Shape shape in doc.GetChildNodes(NodeType.Shape, true))
        {
            if (shape.HasImage && shape.ImageData.IsLink)
            {
                Match 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)
                {
                    // Extract the name from the merge field and insert a proper merge field as a child of the shape.
                    shape.AppendChild(new Paragraph(doc));
                    builder.MoveTo(shape.FirstParagraph);
                    builder.InsertField("MERGEFIELD " + match.Value);
                }
            }
        }
    }
    public void FieldMerging(FieldMergingArgs args)
    {
        Shape shape = (Shape) args.Field.Start.GetAncestor(NodeType.Shape);
        // If this merge field belongs to a INCLUDEPICTURE shape node then merge the image path
        // to the source path property.
        if (shape != null && shape.HasImage)
        {
            shape.ImageData.SourceFullName = args.FieldValue.ToString();
            shape.RemoveAllChildren();
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do Nothing
    }
}

If we can help you with anything else, please feel free to ask.

Thanks,

Hi Adam.

thanks for your answer. I’m sorry, but I’m not familiar with C# and I’m new to VB.NET. Would you be so kind to translate your example for VB.NET. I’ve tried it for myself, with no success.

Thank you very much

Best regards

Manfred

Hi
Thanks for your request. Here is the same code in VB:
_

Public Sub Test001()
    Dim doc As New Document("C:\Temp\in.doc")
    doc.MailMerge.FieldMergingCallback = New HandleIncludePictureFields(doc)
    doc.MailMerge.Execute(New String() {"ImagePath"}, New Object() {"Image.jpg"})
    doc.Save("C:\Temp\out.doc")

End Sub
Public Class HandleIncludePictureFields
    Implements IFieldMergingCallback
    Public Sub New(ByVal doc As Document)
        Dim builder As DocumentBuilder = New DocumentBuilder(doc)
        For Each shape As Shape In doc.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(doc))
                    builder.MoveTo(shape.FirstParagraph)
                    builder.InsertField("MERGEFIELD " + match.Value)

                End If
            End If
        Next
    End Sub
    Public Sub FieldMerging(ByVal args As Aspose.Words.Reporting.FieldMergingArgs) Implements Aspose.Words.Reporting.IFieldMergingCallback.FieldMerging
        Dim shape As Shape = CType(args.Field.Start.GetAncestor(NodeType.Shape), Shape)
        ' If this merge field belongs to a INCLUDEPICTURE shape node then merge the image path

        ' to the source path property.

        If (shape IsNot Nothing And shape.HasImage) Then
            shape.ImageData.SourceFullName = args.FieldValue.ToString()
            shape.RemoveAllChildren()

        End If
    End Sub
    Public Sub ImageFieldMerging(ByVal args As Aspose.Words.Reporting.ImageFieldMergingArgs) Implements Aspose.Words.Reporting.IFieldMergingCallback.ImageFieldMerging
    End Sub
End Class

Best regards,

Hi Alexey,

I tried both examples, but IncludePicture fields are not found. With your example no shapes are found. With the nodelist of alle node.fieldstarts in another expample I also didn’t find any ‘Includepicture’ field
I’ve attached the template I’m using.
Maybe you find what I’m doing wrong.

Thanks in advance

Regards

Manfred

Hi Manfred,
Thank you for additional information. For some reason INCLUDEPICTURE field is missed upon loading your document. That is why the code does not work. I logged this issue in our defect database. We will let you know once it is resolved.
In your case, if you need simply insert image upon mail merge, I would suggest you to use the approach suggested here:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Best regards,

Hi Alexey,

sorry,but this is an important feature of our application. Our customers have a lot of templates with the construct INCLUDEPICTURE MERGEFIELD … So I’ve to find a solution anyway. I hope your developers find the answer soon. In the meanwhile I have to transfer another portion of our existing word automation solution to ‘Aspose.Words’. In a Worddocument, a part of the text can be marked with the words @BEGIN@ and @END@. I’ve to find these words, extract the text between them into a new document. This document will be handled in a special way (Mailmerge) and the result has to be inserted instead of then original text. I’ve found a similar question in the forum. But because I’m not familiar with C# (only VB.NET) I was not able to use the solution. Can you help me?

Thanks in advance

Best regards

Manfred

Hello
Thanks for your request. Please see the following link to learn how to extract content between nodes:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/
After this, you can try using the way described here to insert this content into another document:
https://docs.aspose.com/words/java/insert-and-append-documents/
Best regards,

Hi Andrey,

thanks for the quick answer. You and your colleagues are doing a very good job.

Best regards

Hi Andrey

can you tell me, when the issue in the combination ‘INCLUDEPICTURE MERGEFIELD’ is solved.
I’ve updated to Version 10.8 (January 2012) . But the problem is still occuring.

Thanks in advance

Best regards

Manfred Richter

Extract Mailmergefield from within Includepicture

Hi Manfred,

Thanks for your inquiry. Unfortunately, the issue is still unresolved. Currently this issue is pending for analysis and is in the queue. I hope, the responsible developer will analyse the issue shortly and we will then be able to provide you more information. Sorry for inconvenience.

Best Regards,

Hi all,

are there any news for this issue?

Best regards

Manfred Richter

Hi Manfred,

Thanks for your inquiry. Unfortunately, your issues are in analysis process. Once our developers analyze these issues, we will be able to provide you an estimate. You will be notify as soon as it is fixed. Sorry for inconvenience.

**Status:**Status: In Analysis

Thanks to all of the Aspose team.

This issue seems to be resolved in Version 11.1.0.

I have an additional question.

In your solution INCLUDEPICTURE is converted to a shape with height/width set to 0. In our word templates the INCLUDEPICTURE is often a child of a tablecell, textframe or textbox. Is there a possibilty to fit the shape to this container?

Thanks in advance

Manfred

Hi Manfred,

Thanks for your feedback. Please note that Aspose.Words preserves the viewing fidelity during converting documents from one format to others. Could you please attach your input document you are getting problems with here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Hi Awais,

here is the document an a piece aof code I’m using.

Private Class HandleMergeFieldInsertImage Implements IFieldMergingCallback
    '''
    ''' This is called when merge field is actually merged with data in the document.
    ''' 
    '''
    Public Sub New(ByVal sdoc As Aspose.Words.Document)

        Dim builder As DocumentBuilder = New DocumentBuilder(sdoc)
        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)
                    builder.InsertField("MERGEFIELD " + match.Value)

                    ' When I' m not doing this the width and height is 0

                    shape.Width = 200

                    shape.Height = 200

                End If
            End If
        Next
    End Sub

Thanks in advance

Best Regards

Manfred Richter

Hi Manfred,

Thanks for your inquiry. In your case, the width and height of shape should be preserved. You don’t actually need to set it manually. I have asked the responsible developer for a review. As soon as, any information is shared by them, I will be more then happy to share that with you. Sorry for inconvenience.

As a temporary workaround, please try using the following code snippet:

Private Class HandleMergeFieldInsertImage Implements IFieldMergingCallback
''' 
''' This is called when merge field is actually merged
With data In the document.
''' 
Public Sub New(ByVal sdoc As Aspose.Words.Document)
        Dim builder As DocumentBuilder = New DocumentBuilder(sdoc)


        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 Cell = DirectCast(Shape.GetAncestor(NodeType.Cell), Cell)

                    Shape.Width = c.CellFormat.Width
                    Shape.Height = c.ParentRow.RowFormat.Height
                End If
            End If
        Next
    End Sub

I hope, this will help.

Best Regards,

The issues you have found earlier (filed as WORDSNET-5572) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.