This is a migrated thread and some comments may be shown as answers.

How to insert into table cells?

3 Answers 984 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 23 Jun 2017, 08:45 PM

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?

3 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 23 Jun 2017, 08:47 PM
(In case it isn't obvious: for my first question, how do I insert text (or images) into the cell referenced by firstCell?)
0
Peshito
Telerik team
answered on 28 Jun 2017, 11:01 AM
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

0
RJ
Top achievements
Rank 1
answered on 01 Feb 2018, 08:14 AM

Hi Peshito,

How about adding a row to existing table from docx template?

I have an existing docx that serves as a template. This file is already formatted with header, footer, text and table with 5 rows and 2 columns. 1st row has merged columns and from row 2 onwards has 2 columns.

Now, how can I reference that existing table and insert row after 3rd row.

Thanks in advance,

RJ

 

Carmine
Top achievements
Rank 1
commented on 15 Oct 2021, 08:04 AM

i would really like to know that too...
Peshito
Telerik team
commented on 18 Oct 2021, 01:24 PM

Hello, 

You can edit an existing table by locating the table element first. This can be achieved like this:

IEnumerable<Table> tables = document.EnumerateChildrenOfType<Table>();
var table = tables.FirstOrDefault();            table.Rows.AddTableRow().Cells.AddTableCell().Blocks.AddParagraph().Inlines.AddRun("new run");

Hope this helps.

Regards,

Peshito

 

Tags
WordsProcessing
Asked by
Phil
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
Peshito
Telerik team
RJ
Top achievements
Rank 1
Share this question
or