如何使用Aspose.Words for.NET将RichText保存到Word?

如何使用Aspose.Words for.NET将RichText保存到Word?,.net,ms-word,richtext,aspose,.net,Ms Word,Richtext,Aspose,我们正在使用Aspose.Words for.NET导出应用程序中的Word文档。 现在我还必须在导出的文档中包含RichText内容(实际上是一个FlowDocument)。 为了导出,我们正在实现IMailMergeDataSource接口。Aspose库调用此IMailMergeDataSource实现的GetValue函数,该函数如下所示: public override bool GetValue(string fieldName, out object fieldValue) {

我们正在使用Aspose.Words for.NET导出应用程序中的Word文档。 现在我还必须在导出的文档中包含RichText内容(实际上是一个FlowDocument)。 为了导出,我们正在实现IMailMergeDataSource接口。Aspose库调用此IMailMergeDataSource实现的GetValue函数,该函数如下所示:

public override bool GetValue(string fieldName, out object fieldValue) {  ...  }
因此,我在Word模板中获取当前字段的字段名,我必须将fieldValue设置为字符串,以便fieldValue中的字符串可以显示在Word文档中


但例如,当我将fieldValue设置为FlowDocument时,结果将是一个XML字符串(FlowDocument对象的ToString表示)

我建议您在fieldValue中传递富文本。将此富文本加载到Aspose.Words文档对象中,如下所示(在FieldMerging事件中):

您需要实现IFieldMergingCallback接口,以便能够控制在邮件合并操作期间如何将数据插入合并字段

private class HandleMergeFields : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);

        builder.MoveToMergeField("fieldName");
        Node node = builder.CurrentNode;

        // doc is an RTF document we created from RTF string
        InsertDocument(node, doc); 

我希望这对你的情况有所帮助。如果没有帮助,请务必告诉我。

它有效!谢谢我只需要将FlowDocument字符串转换为RTF字符串:var xamlString=“…”;var flowDocument=FlowDocumentService.GetFlowDocument(xamlString);字符串dataFormat=DataFormats.Rtf;var documentTextRange=新的TextRange(flowDocument.ContentStart、flowDocument.ContentEnd);var stream=newmemoryStream();documentTextRange.Save(流、数据格式);LoadOptions LoadOptions=新的LoadOptions();loadOptions.LoadFormat=LoadFormat.Rtf;fieldValue=新文档(流、加载选项);
private class HandleMergeFields : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);

        builder.MoveToMergeField("fieldName");
        Node node = builder.CurrentNode;

        // doc is an RTF document we created from RTF string
        InsertDocument(node, doc);