【问题标题】:Replace word with word with umlaut in RTF and C#在 RTF 和 C# 中用带有变音符号的单词替换单词
【发布时间】:2014-08-07 08:24:16
【问题描述】:

我正在使用 RTF 模板。在这个模板中有几个地方需要替换文本。当使用 ASCII 字符时,这很好用。当我使用非 ASCII 字符时,字符变为问号。

我的程序代码:

memo = memo.Replace("%TITEL%", titel);

memo 是 RTF 文件,在我的代码中作为字符串读取。 %TITEL% 存在于 RTF 模板中 en titel 具有以下文本:

Förderband

所以%TITEL%Förderband 替换。

当我打开文档时,文字显示:

F?rderband

我将 RTF 字符串添加到 Word 文档中:

// Read RTF document content.
                        string rtfDocumentContent = memo;

                        using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(rtfDocumentContent)))
                        {
                            chunk.FeedData(ms);
                        }

                        AltChunk altChunk = new AltChunk();
                        altChunk.Id = altChunkId;

                        // Embed AltChunk after the last paragraph.
                        mainDocPart.Document.Body.InsertAfter(
                          altChunk, mainDocPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());

谁能帮帮我?

【问题讨论】:

标签: c# rtf non-ascii-characters


【解决方案1】:

您正在使用 ASCII 编码。也许您应该使用以下代码块更改为 UTF-8

using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(rtfDocumentContent)))
{
    chunk.FeedData(ms);
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 2011-08-29
    • 2022-12-14
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多