How Can I Add a DocumentVariableField to a Table Cell Programmatically

1 Answer 59 Views
RichTextBox
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Steve asked on 25 Jul 2024, 04:30 PM | edited on 25 Jul 2024, 04:31 PM

Hi,

We are creating a table which then gets inserted into a RichTextBox. We need to add a DocumentVariableField to one of the cells, can anyone please advise.


// The field which needs to be added o the table
            
doc_text.Document.DocumentVariables.Add("1001", addrName);
DocumentVariableField docVariable = new DocumentVariableField() { VariableName = "1001"  };
           
doc_text.InsertField(docVariable, FieldDisplayMode.Result);
            

RadDocument document = new RadDocument();
Section section = new Section();

TableWidthUnit w1 = new TableWidthUnit(100);
TableWidthUnit w2 = new TableWidthUnit(520);

Table table = new Table();
table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;

TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Background = Color.FromRgb(174, 255, 190);
cell1.PreferredWidth = w1;
cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
Paragraph p1 = new Paragraph();
Span s1 = new Span();
s1.FontWeight = FontWeights.Bold;
s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
s1.Text = "Name";
p1.Inlines.Add(s1);
cell1.Blocks.Add(p1);
row1.Cells.Add(cell1);

TableCell cell2 = new TableCell();
cell2.Background = Color.FromRgb(174, 255, 190);
cell2.PreferredWidth = w2;
cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
Paragraph p2 = new Paragraph();
Span s2 = new Span();
s2.FontWeight = FontWeights.Bold;
s2.FontFamily = new FontFamily(CGlobals.docu_default_font);
s2.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
            
s2.Text = addrName;  // We need to change this to the DocumentVariableField
p2.Inlines.Add(s2);
cell2.Blocks.Add(p2);
row1.Cells.Add(cell2);
table.Rows.Add(row1);

section.Blocks.Add(new Paragraph());
section.Blocks.Add(table);
section.Blocks.Add(new Paragraph());
document.Sections.Add(section);

doc_text.Document = document;

 

Many thanks

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimitar
Telerik team
answered on 26 Jul 2024, 08:25 AM

Hi Steve,

In general, we recommend using the RadDocumentEditor for inserting content. In this case, you need to move the caret position to the desired cell: 

RadDocumentEditor editor = new RadDocumentEditor(document);
var cell = document.EnumerateChildrenOfType<TableCell>().ToList()[3];

if (cell !=null)
{
    DocumentPosition position = new DocumentPosition(document);
    position.MoveToDocumentElementStart(cell.Blocks.First());
    document.CaretPosition.MoveToPosition(position);
    DocumentVariableField docVariable = new DocumentVariableField() { VariableName = "1001" };
    editor.InsertField(docVariable);
}
radRichTextBox.Document = document;

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Steve
Top achievements
Rank 2
Iron
Iron
Iron
commented on 26 Jul 2024, 09:35 AM

Hi Dimitar,

Thank you very much, that's now working.

 

Tags
RichTextBox
Asked by
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or