3

I didn't know it would be so difficult to play with Richtextbox control. I am trying to fetch Clipboard data and trying to set in a RichTextBox.

RichTextBox rtb = new RichTextBox();
rtb.Rtf = Clipboard.GetText(TextDataFormat.Rtf);

How to iterate over table collection or Indivisual tables ? Can we find table count from RichTextBox ?

My goal is to find if there are any table in the clipboard RTF data and if yes then check those for data in it.

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
  • Yep, not easy. You will have to examine the rtf codes themselves. Things like `\trowd`, etc. In my experience, rtf doesn't really have a concept of "tables", it's more like, rows of cells. – LarsTech Jun 21 '13 at 14:24
  • @LarsTech- guess i will have to go with some unorthodox ways. for ex. if rtf data has a word `trowd`, that means it definitely has some rows in it. – Sangram Nandkhile Jun 21 '13 at 18:04
  • This surely requires you to work with some kind of `RTF Parser`, you may want to search for a library or write a one (just handling with Table structure) yourself. In case you would like to write your own one, I think using `Regular Expression` is the best choice to parse the Table structure, of course you should refer some document on `RTF` structure to know its syntax, especially syntax on Table. I think this is not easy, but must be very interesting :) – King King Jun 21 '13 at 20:55
  • @KingKing any complete guide to understand the rtf syntax? – Sangram Nandkhile Jun 23 '13 at 12:13
  • @Sangram you may want to Bing (not Goolge :) for more, I've found these documentation which should be helpful: http://www.biblioscape.com/rtf15_spec.htm and the newer version: https://www.google.com.vn/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&sqi=2&ved=0CDoQFjAC&url=http%3A%2F%2Frepositorium.googlecode.com%2Fsvn%2Ftrunk%2FRtfConverter%2FWord2007RTFSpec9.pdf&ei=iO7GUaCvCoKEiAe604H4CQ&usg=AFQjCNEodEHav3SHNTMGTa9JaTsi5SBv_w&sig2=TVDJyX3xyvpnTlj1druWeA&bvm=bv.48293060,d.aGc I think you may have to work hard with it if you don't have much time. – King King Jun 23 '13 at 12:51
  • You may use the table format in rtf: http://stackoverflow.com/questions/8349827/using-tables-in-rtf/13321729#13321729 – Jerry Jun 29 '13 at 02:40

1 Answers1

-1

Although, its bit too late to answer this question. I also came across same requirement.this way i proceeded:

private static void findTableinRtf(string rtf)
    {
        var flowDocument = new FlowDocument();
        var textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(rtf)))
        {
            textRange.Load(ms, DataFormats.Rtf);
        }
        var blocks = flowDocument.Blocks;
        foreach (var block in flowDocument.Blocks)
        {
            switch (block)
            {
                case List list:
                     //implement List;
                    break;

                case Table table:
                    workWithTable(table);
                    break;

                case Paragraph paragraph:
                    convertParagraph(paragraph);
                    break;
                case Section section:
                    convertSection(section);
                    break;

            }
        }

    }
    private static void workWithTable(Table rtfTable)
    {
        TableColumnCollection columns = rtfTable.Columns;
        TableRowGroupCollection rowGroups = rtfTable.RowGroups;
        foreach (var row in rowGroups[0].Rows)
        {
            //access cells
            // row row.Cells[i];

        }

    }
JharPaat
  • 321
  • 1
  • 2
  • 12