目标

以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方法之前,我也尝试过对其进行编码。

如何使瑞典语和中文符号在文件中正确显示?

注释和参考


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

最佳答案

您可以使用写字板为您创建rtf字符串。打开写字板,将您的内容复制到文件中。然后使用文本编辑器读取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);
        }

关于c# - 使用OpenXML和RTF将瑞典语和中文符号发送到Docx,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41484211/

10-13 06:38