使用OpenXML和RTF向Docx发送​​瑞典语和中文符号

时间:2017-01-05 11:43:18

标签: c# openxml docx rtf

目标

将瑞典语和中文符号传递给RTF格式的DocX文件。 [2]

描述

我需要动态生成包含瑞典语和中文符号的RTF格式的字符串,并将其发送到现有的Docx文件。我设法处理了瑞典的血统(åäö),但我无法让中国的标志得到正确的显示,而是显示为????

    private void buttonSendDiaeresesToDocx_Click(object sender, EventArgs e)
    {
        var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        var filename = @"SpecialCharactersInDocx.docx";
        var filepath = Path.Combine(desktop, filename);

        //Dynamic content fetched from the database.
        var content = "This should be Swedish and Chinese signs -> åäö - 部件名称";

        var rtfEncodedString = new StringBuilder();
        rtfEncodedString.Append(@"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard ");
        rtfEncodedString.Append(content);
        rtfEncodedString.Append(@"\par}");

        removeExistingFile(filepath);
        createEmptyDocx(filepath);

        addRtfToWordDocument(filepath, rtfEncodedString.ToString());

        openDocx(filepath);
    }

    private void addRtfToWordDocument(string filepath, string rtfEncodedString)
    {
        //Implemented as suggested at
        //http://stackoverflow.com/a/14861397/1997617

        using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true))
        {
            string altChunkId = "AltChunkId1";

            MainDocumentPart mainDocPart = doc.MainDocumentPart;
            AlternativeFormatImportPart chunk = mainDocPart.AddAlternativeFormatImportPart(
                AlternativeFormatImportPartType.Rtf, altChunkId);

            using (MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(rtfEncodedString)))
            {
                chunk.FeedData(ms);
            }

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

            mainDocPart.Document.Body.ReplaceChild(
              altChunk, mainDocPart.Document.Body.Elements<Paragraph>().Last());

            mainDocPart.Document.Save();
        }
    }

我曾尝试对内存流使用不同的编码(默认,ASCII,UTF8,GB18030,...),但没有任何接缝可以工作。我还尝试将 rtfEncodedString 变量的编码转换为 addRtfToWordDocument 方法之前。

如何在文档中正确显示瑞典语和中文标记?

注释和参考

  1. 上面的代码片段是我认为与此问题相关的解决方案的一部分。整个代码示例可以在http://www.bjornlarsson.se/externals/SpecialCharactersInDocx02.zip
  2. 下载
  3. 现实世界的应用程序需要RTF格式,因为内容将在文档中显示为表格(带粗体文本)。

1 个答案:

答案 0 :(得分:1)

您可以使用wordpad为您创建rtf字符串。打开wordpad将您的内容复制保存到文件。然后用texteditor读取rtf。 你的rtf字符串看起来像这样:

{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1031{\fonttbl{\f0\fnil Consolas;}{\f1\fnil\fcharset0 Consolas;}{\f2\fnil\fcharset134 SimSun;}{\f3\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.10586}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\f0\fs19\lang7 This should be Swedish and Chinese signs -> \f1\'e5\'e4\'f6 - \f2\'b2\'bf\'bc\'fe\'c3\'fb\'b3\'c6\f3\fs22\par
}

也许有帮助。我用你的代码测试了rtf字符串,它可以工作!

通过richtextbox动态生成rtf字符串:

private void buttonSendDiaeresesToDocx_Click(object sender, EventArgs e)
        {
            var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var filename = @"SpecialCharactersInDocx.docx";
            var filepath = Path.Combine(desktop, filename);

            removeExistingFile(filepath);
            createEmptyDocx(filepath);
            rtfEncodedString = new StringBuilder();
            string contentOriginal = "This should be Swedish and Chinese signs -> åäö - 部件名称";
            string rtfStart =
                "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}{\\f1\\fmodern\\fprq6\\fcharset134 SimSun;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 ";
            RichTextBox rtfBox = new RichTextBox {Text = contentOriginal};
            string content = rtfBox.Rtf;
            content = content.Replace(rtfStart, "");
            rtfEncodedString.Append(rtfStart);
            rtfEncodedString.Append(content);
            rtfEncodedString.Append(@"\par}");
            addRtfToWordDocument(filepath, rtfEncodedString.ToString());

            openDocx(filepath);
        }