本文介绍了Aspose.Word MailMerge在段落中设置字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FieldMergingCallback.FieldMerging中,我将字体设置为Node中的所有Run ==>

In FieldMergingCallback.FieldMerging and I set font to all Runs in Node =>

 public void FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldValue.ToString().Length > 100)
        {
            var node = args.Field.Start.ParentNode.ParentNode;
            if (node is Shape)
            {
                var runlist = node.GetChildNodes(NodeType.Run, true);
                foreach (Run run in runlist)
                {
                    run.Font.Size = 6;
                }
            }
        }
    }

但是结果pdf是:

But in result pdf is:

为什么字体比第三个单词小?

Why is the font smaller than the third word?

推荐答案

在执行邮件合并之前,可以使用以下代码对合并字段中的所有运行"节点应用相同的格式.

Before performing mail merge, you can use the following code to apply same formatting to all Run nodes inside a merge field.

Document doc = new Document("D:\\temp\\input.docx");

foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldMergeField))
    {
        Node currentNode = field.Start;
        bool isContinue = true;
        while (currentNode != null && isContinue)
        {
            if (currentNode.NodeType.Equals(NodeType.FieldEnd))
            {
                FieldEnd end = (FieldEnd)currentNode;
                if (end == field.End)
                    isContinue = false;
            }

            if (currentNode.NodeType.Equals(NodeType.Run))
            {
                Run run = ((Run)currentNode);
                run.Font.Size = 6;
            }

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

doc.Save("D:\\Temp\\18.6.docx");

希望,这会有所帮助.我与Aspose一起担任开发人员推广人员.

Hope, this helps. I work with Aspose as Developer Evangelist.

这篇关于Aspose.Word MailMerge在段落中设置字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 23:07