0

I'm making an editor for Word documents in C#. And I'm using a RichTextBox to create text with its formatting, and add a slice of XDocumento docx.

Someone could tell me if you have any way of converting?? include formating...

This example would already be more complex:

RichTextBox format:

{\\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

to this, OpenXml Document format: (default for 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>

and the opposite too.

Did not want to do it manually.

In Micrsoft Office Word, you can convert the RTF format (used in richtextbox) to DOCX. But I can not find the solution to this in Wird library.

Thanks.

2 Answers2

0

You can use Regex. This pattern will match the first example, {Text.*?"(.*?)"}, and here's a Regex 101 to prove it. Then the C# for this would be:

var val = Regex.Replace(input, pattern, "<w:p><w:r><w:t>$0</w:t></w:r></w:p>");
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Thx Michael, I got to thinking about it, the problem would be the various types of formats that I would have to convert. In this case only has text. I add another example. – Thiago Tozzi Nov 25 '13 at 16:44
  • @ThiagoTozzi, unfortunately there's no way around it. You'll need to build multiple Regex patterns for your needs. – Mike Perrenoud Nov 25 '13 at 17:13
0

You should just be able to use the .Text option when referencing the RTFTextBox. For example, mytextbox.rtf vs mytextbox.text. At least thats the way it works in Visual Studio 2010.

Chip
  • 1