【问题标题】:Aspose Calling UpdateFields() on document loses mail merge field dataAspose 在文档上调用 UpdateFields() 会丢失邮件合并字段数据
【发布时间】:2019-04-01 05:16:29
【问题描述】:

我正在尝试使用 Aspose 将 Word 文档(指向未按预期工作的示例文件的链接如下所示)导出到 PDF。 https://1drv.ms/w/s!AheHNqR6oXmSmd5H80L0vzCTfVVrTg

相同的代码如下。

var doc=new Document(<streamFromTheFile>); // Aspose.Words.Document
doc.UpdateFields();// This is required for any possible formula
var outStream=new MemoryStream();
doc.Save(outStream, SaveFormat.Pdf); // Aspose.Words.SaveFormat
File.WriteAllBytes(<exportPdfFilePath>, outStream.ToArray());

所有其他文件都可以正常工作,除了示例文档中具有合并字段的文件,其中甚至当前值都丢失并被合并字段名称替换,如 «AtpIssueDate»。取消 UpdateFields() 方法调用可以解决问题,但无法完成,因为它破坏了逻辑。请帮助如何保留合并字段的值而不删除导出时的 UpdateFields() 调用。

【问题讨论】:

    标签: c# aspose mailmerge aspose.words


    【解决方案1】:

    您可以使用以下代码解决此问题:

    Document doc = new Document("D:\\temp\\so.docx");
    
    // LOCK merge fields before Updatefields method call
    foreach(Field field in doc.Range.Fields)
    {
        if (field.Type == FieldType.FieldMergeField)
        {
            field.IsLocked = true;
        }
    }
    
    doc.UpdateFields();
    
    // UN-LOCK merge fields after Updatefields method call
    foreach (Field field in doc.Range.Fields)
    {
        if (field.Type == FieldType.FieldMergeField)
        {
            field.IsLocked = false;
        }
    }
    
    doc.Save("D:\\temp\\18.10.pdf");
    

    希望,这会有所帮助。我与 Aspose 一起担任开发人员宣传员。

    【讨论】:

    • ❤ 好样的。那工作:) 干得好,Awaiz。非常感谢
    【解决方案2】:

    听起来你需要专门遍历你的文档。

    我编写了下一个代码来重现和解决您的问题。您可以根据需要对其进行自定义。您可以像本例中一样获取字段类型FieldType Enumeration

    using System.IO;
    using System.Linq;
    using Aspose.Words;
    using Aspose.Words.Fields;
    
    namespace ConsoleApplication1
    {
      public class Program
      {
        public static void Main()
        {
          Stream file = new FileStream("SO.docx", FileMode.Open);
          var doc = new Document(file);
    
          var nodes = doc.GetChildNodes(NodeType.Any, true);
    
          foreach (var node in nodes)
          {
            if (node.NodeType != NodeType.Paragraph)
              continue;
            if (!(node is Paragraph paragraph))
              continue;
    
            if (paragraph.ChildNodes.Any(x => x.NodeType == NodeType.FieldStart))
            {
              var childNodes = paragraph.ChildNodes;
              var isParagraphContainsMergedField = childNodes.Any(x => (x as FieldChar)?.FieldType == FieldType.FieldMergeField);
              if (isParagraphContainsMergedField)
                continue;
            }
    
            node.Range.UpdateFields();
          }
    
          var outStream = new MemoryStream();
          doc.Save(outStream, SaveFormat.Pdf);
          File.WriteAllBytes("test.pdf", outStream.ToArray());
        }
      }
    }
    

    希望对您有所帮助。

    P.S.:不要忘记使用 using 或处理您的流。

    【讨论】:

    • 感谢您的回复。但这会带来一些开销。
    • @AnanthanUnni,是的,Awais Hafeez 解决方案更加准确和简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    相关资源
    最近更新 更多