0

I am using Aspose with Word for a Mail Merge.

I have a 3x2 table as follows

<a>    <b>
<c>    <d>
<e>    <f>

However under certain conditions one field might be blank, if this is the case I'd like to ommit the entire cell - i.e. not just have an empty cell. i.e.

<a>    <b>
<e>    <d>
       <f>

so in the above example c is empty and thus not displayed?

Can this be done?

I have tried IF and blank MERGEFIELDS also NextIf.

4

1 回答 1

1

@乔恩,

您可能需要实现 IFieldMergingCallback 接口。事件处理程序“FieldMerging”可用于实现对邮件合并过程的自定义控制。例如,您可以检查传入 MergeField 的值是否为“null”或“空字符串”,然后基于此决定删除父 Cell。这是示例代码:

Document doc = new Document(@"C:\Temp\template.docx");
doc.MailMerge.FieldMergingCallback = new HandleMergeFields();
doc.MailMerge.Execute(new string[] { "a", "b", "c", "d", "e", "f" },
                        new object[] { "<a>", "<b>", "", "<d>", "<e>", "<f>" });
doc.Save(@"C:\Temp\out.doc");

private class HandleMergeFields : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder(args.Document);
        if (string.IsNullOrEmpty(args.FieldValue.ToString()))
        {
            Field field = args.Field;
            Cell cell = field.Start.GetAncestor(NodeType.Cell) as Cell;
            // Remove the MergeField
            builder.MoveToMergeField(args.FieldName);
            //Remove the Cell
            cell.Remove();
        }
    } 
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing
    }
}
于 2013-02-12T12:34:30.167 回答