Aspose 在文档上调用 UpdateFields() 会丢失邮件合并字段数据
Aspose Calling UpdateFields() on document loses mail merge field data
我正在尝试使用 Aspose 将一个 Word 文档(link 导出到一个示例文件,该文件没有按预期工作,如下所示)到 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() 的情况下保留合并字段的值。
听起来您需要专门遍历您的文档。
我编写了下一个代码来重现并解决您的问题。您可以根据需要自定义它。您可以像本示例中那样获取字段类型 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
或处理你的流。
您可以使用以下代码解决此问题:
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 工作,担任开发人员传道者。
我正在尝试使用 Aspose 将一个 Word 文档(link 导出到一个示例文件,该文件没有按预期工作,如下所示)到 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() 的情况下保留合并字段的值。
听起来您需要专门遍历您的文档。
我编写了下一个代码来重现并解决您的问题。您可以根据需要自定义它。您可以像本示例中那样获取字段类型 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
或处理你的流。
您可以使用以下代码解决此问题:
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 工作,担任开发人员传道者。