在使用aspose.words中插入富文本

想在指定的位置加入传入的富文本内容,应该怎么处理。
在数据库存储的内容为富文本例如:

<p>啊手动阀手动阀</p>
<p>&nbsp;</p>
<p>???</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>手动阀手动阀</p>

在word导出中需要对此富文本进行处理,需保留格式及内容,替换模板中指定位置的内容应如何处理

以上的内容被转为html内容,补充一张图片:
微信截图_20210726172652.png (1.3 KB)

@zgzjhzsyt,

您可以使用 DocumentBuilder.InsertHtml 方法将光标移动到 Word 文档中的任意位置并插入格式化的内容。

Document doc = new Document("C:\\Temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// move cursor to the desired position
builder.MoveTo(doc.LastSection.Body.LastParagraph);
builder.InsertHorizontalRule();
// Insert HTML formatted content in Word document
builder.InsertHtml("<p>啊手动阀手动阀</p><p>&nbsp;</p><p>???</p><p>&nbsp;</p><p>&nbsp;</p><p>手动阀手动阀</p>");
doc.Save("C:\\temp\\21.7.docx");

如何替换模板中指定的占位符呢,如在以下占位符中替换:
{{desicription}}

@zgzjhzsyt,

Aspose.Words for .NET API 的以下 C# 代码应该用 HTML 字符串替换 MS Word 文档中的关键字/标记:

string htmlToReplaceWith = "<p>啊手动阀手动阀</p><p>&nbsp;</p><p>???</p><p>&nbsp;</p><p>&nbsp;</p><p>手动阀手动阀</p>";

Document doc = new Document("C:\\Temp\\test.docx");

FindReplaceOptions options = new FindReplaceOptions(FindReplaceDirection.Backward);
options.ReplacingCallback = new ReplaceWithHtmlEvaluator(htmlToReplaceWith);

doc.Range.Replace(new Regex(@"{{desicription}}"), String.Empty, options);

doc.Save(@"C:\Temp\\21.7.docx");

private class ReplaceWithHtmlEvaluator : IReplacingCallback
{
    private string _html = "";

    internal ReplaceWithHtmlEvaluator(string html)
    {
        _html = html;
    }

    //This simplistic method will only work well when the match starts at the beginning of a run. 
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add(currentNode);
        }

        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo((Run)runs[0]);

        // Replace '<CustomerName>' text with a red bold name.
        builder.InsertHtml(_html);

        foreach (Run run in runs)
            run.Remove();

        return ReplaceAction.Skip;
    }

    private static Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run)run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring((0), (0) + (position));
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;
    }
}