【问题标题】:Richtextbox and Docx convertingRichtextbox 和 Docx 转换
【发布时间】:2013-12-10 05:40:58
【问题描述】:

我正在用 C# 制作 Word 文档的编辑器。我正在使用 RichTextBox 来创建具有其格式的文本,并添加一段 XDocumento docx。

有人可以告诉我你是否有任何转换的方法?包括格式化...

这个例子已经很复杂了:

RichTextBox 格式:

{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1046{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n{\\colortbl ;\\red255\\green0\\blue0;}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 A \\b Nissan \\b0 est\\'e1 \\i mesmo \\i0 disposta a \\ul fincar \\ulnone seus p\\'e9s em solo \\cf1 brasileiro\\cf0 . \\par\r\n}\r\n

至此,OpenXml 文档格式:(默认为 docx)

<w:p w:rsidR = "00685F88" w:rsidRDefault = "00685F88">
            <w:pPr>
                <w:widowControl w:val = "0"/>
                <w:autoSpaceDE w:val = "0"/>
                <w:autoSpaceDN w:val = "0"/>
                <w:adjustRightInd w:val = "0"/>
                <w:spacing
                    w:after = "0pt"
                    w:line = "12pt"
                    w:lineRule = "auto"/>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">A</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:b/>
                    <w:bCs/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">Nissan</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">está</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:i/>
                    <w:iCs/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">mesmo</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">disposta a</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                    <w:u w:val = "single"/>
                </w:rPr>
                <w:t xml:space = "preserve">fincar</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">seus pés em solo</w:t>
            </w:r>
            <w:r>
                <w:rPr>`enter code here`
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:color w:val = "FF0000"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t>brasileiro</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts
                        w:ascii = "Microsoft Sans Serif"
                        w:hAnsi = "Microsoft Sans Serif"
                        w:cs = "Microsoft Sans Serif"/>
                    <w:sz w:val = "17"/>
                    <w:szCs w:val = "17"/>
                </w:rPr>
                <w:t xml:space = "preserve">.</w:t>
            </w:r>
        </w:p>

反之亦然。

不想手动操作。

在 Micrsoft Office Word 中,您可以将 RTF 格式(用于 Richtextbox)转换为 DOCX。但我在 Wird 库中找不到解决方案。

谢谢。

【问题讨论】:

  • 使用 RTF 会让您的生活变得相当困难。

标签: c# richtextbox docx openxml-sdk novacode-docx


【解决方案1】:

您可以使用正则表达式。此模式将匹配第一个示例{Text.*?"(.*?)"},这里有一个Regex 101 来证明它。那么C# 将是:

var val = Regex.Replace(input, pattern, "<w:p><w:r><w:t>$0</w:t></w:r></w:p>");

【讨论】:

  • 谢谢迈克尔,我开始考虑它,问题在于我必须转换的各种类型的格式。在这种情况下只有文本。我再添加一个例子。
  • @ThiagoTozzi,不幸的是没有办法解决它。您需要根据需要构建多个正则表达式模式。
【解决方案2】:

在引用 RTFTextBox 时,您应该只能使用 .Text 选项。例如,mytextbox.rtf 与 mytextbox.text。至少它在 Visual Studio 2010 中是这样工作的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多