2

我不知道玩 Richtextbox 控件会这么难。我正在尝试获取剪贴板数据并尝试在 RichTextBox 中进行设置。

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

如何迭代表集合或 Indivisual 表?我们可以从 RichTextBox 中找到表数吗?

我的目标是查找剪贴板 RTF 数据中是否有任何表格,如果有,则检查其中的数据。

4

1 回答 1

-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];

        }

    }
于 2017-11-01T12:42:39.557 回答