How to insert into table cells?

3 posts, 0 answers
  1. Phil
    Phil avatar
    13 posts
    Member since:
    Mar 2014

    Posted 23 Jun Link to this post

    I haven't seen anything in the sparse documentation, nor in a forum thread, on the topic of inserting text into a particular table cell.

    What I want to know is how to do something that should be very straightforward: get a particular table cell reference and insert formatted text into it.

    For example, with this, I create a RadFlowDocument, attach an editor to it, insert a table, then a row, then a cell into the row:

    RadFlowDocument document = new RadFlowDocument();
    RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
    var table = editor.InsertTable();
    var firstRow = table.Rows.AddTableRow();
    var firstCell = firstRow.Cells.AddTableCell();

    How do I insert text now?

    And actually, more importantly, how would I take a *randomly selected* TableCell from an already constructed document and insert text (and images) into that?

  2. Phil
    Phil avatar
    13 posts
    Member since:
    Mar 2014

    Posted 23 Jun in reply to Phil Link to this post

    (In case it isn't obvious: for my first question, how do I insert text (or images) into the cell referenced by firstCell?)
  3. Peshito
    Admin
    Peshito avatar
    520 posts

    Posted 28 Jun Link to this post

    Hi Phil,

    You should add paragraph to your cell in the table and then add a run in the inline collection. More on that can be found here

    As for adding an image you could use the AddImageInline() method.

    A sample code for achieving both is shown below:
                      RadFlowDocument document = new RadFlowDocument();
                RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
     
                var table = editor.InsertTable();
                var firstRow = table.Rows.AddTableRow();
                var firstCell = firstRow.Cells.AddTableCell();
                var secondCell = firstRow.Cells.AddTableCell();
                var thirdCell = firstRow.Cells.AddTableCell();
     
                //inserting text in the cell
                firstCell.Blocks.AddParagraph().Inlines.AddRun("text1");
                secondCell.Blocks.AddParagraph().Inlines.AddRun("text2");
     
                //inserting image in the cell
                using (Stream firstImage = File.OpenRead(@"D:\immages\logo.png"))
                {
                    var inImage = table.Rows[0].Cells[2].Blocks.AddParagraph().Inlines.AddImageInline();
                    inImage.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(firstImage, "png");
                }
     
                using (Stream output = new FileStream(@"insertImageAtCell.docx", FileMode.OpenOrCreate))
                {
                    DocxFormatProvider provider = new DocxFormatProvider();
                    provider.Export(document, output);
                }
    Hope this helps.

    Regards,
    Peshito
    Progress Telerik

Back to Top