【问题标题】:DocX ReplaceText using rich text from Xceed's WPF ToolkitDocX ReplaceText 使用来自 Xceed 的 WPF 工具包的富文本
【发布时间】:2018-10-06 15:11:00
【问题描述】:

无法使用Microsoft.Office.Interop.Word,我正在使用Xceed DocX 从 WPF 字段中获取数据并将它们写入 Word 文件。我需要用RTB 中的文本和格式替换Word 文件中的«field»。如果我使用这样简单的东西:

/// <summary>
/// Replaces field location with given formatted text
/// </summary>
/// <param name="mergeField">Target location</param>
/// <param name="textToWrite">Rich, formatted text to process and insert</param>
public static void FindReplaceRich(string mergeField, string textToWrite)
{
    while (document.FindAll(mergeField).Count > 0)
    {
        document.ReplaceText("«" + mergeField + "»", textToWrite);                
    }
}

上面的代码用于未格式化的文本,因此在这种情况下,我的 Word 文件得到了一个愚蠢的 RTF 简介,像这样直接添加到文档中:{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff0{ \fonttbl{\f0\fcharset0 Times New Roman;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red124\green252\blue0;}\loch\hich\dbch\pard\plain\ltrpar \itap0{\lang1033\fs18\f0\cf0 \cf0\ql{\f0 {\ltrch 这是 }{\highlight2\ltrch 真的 }{\ltrch 愚蠢。 }\li0\ri0\sa0\sb0\fi0\ql\par} } }

那么如何处理这些富文本,以便可以使用 Xceed 的 DocX 库将其插入到我的 Word 文件中?

到目前为止,我已经尝试使用 Clibboard as described here,但我不确定我是否正确复制了文本或我如何应用粘贴的内容。我也在尝试使用 DocX 的 InsertParagraph 但这看起来就像它在文档中附加了一个段落(我仍然没有解决格式问题)。任何帮助将不胜感激。

【问题讨论】:

标签: c# richtextbox docx wpftoolkit xceed


【解决方案1】:

如果你想用值替换标签,我建议你使用 docx 的“模板引擎”。我碰巧自己创建了一个:https://github.com/open-xml-templating/docxtemplater

使用开源版本,如果你想用简单的文本替换标签或做循环/条件(其他高级功能,如 HTML 插入是付费扩展),你会很好。

【讨论】:

    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 2018-12-14
    • 2016-08-17
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多