How to Programmatically add a Content Control to a Table Cell in RadRichTextBox

1 Answer 65 Views
RichTextBox
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Steve asked on 25 Jul 2024, 09:36 AM

Hi,

We are creating a table and adding it to a RichTextBox:


        public void AddAddress()
        {
            string addrName = string.Empty;
            string[] rowName = new string[] { "Job Title", "Employer", "Address", "Tel.", "Tel. Dir.", "Mobile", "Email", "Website", "Fax", "Details", "Address", "Tel.", "Mobile", "Email", "Details" };
            if(_selelement != null)
            {
                addrName = _selelement.GetAttribute("title");
            }
            SdtProperties sdtProperties = new SdtProperties(SdtType.RichText)
            {
                Alias = "AddressName",
                Lock = Lock.SdtContentLocked,
                ID = 1001,
            };
            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();
            p1.Background = Color.FromRgb(174, 255, 190);
            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();
            p2.Background = Color.FromRgb(174, 255, 190);
            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;
            p2.Inlines.Add(s2);
            cell2.Blocks.Add(p2);
            row1.Cells.Add(cell2);
            table.Rows.Add(row1);

            int rowCount = 0;
            foreach(string rname in rowName)
            {
                rowCount++;
                row1 = new TableRow();
                cell1 = new TableCell();
                p1 = new Paragraph();
                if (rowCount < 11)
                {
                    cell1.Background = Color.FromRgb(255, 229, 153);
                    p1.Background = Color.FromRgb(255, 229, 153);
                }
                else
                {
                    cell1.Background = Color.FromRgb(196, 255, 255);
                    p1.Background = Color.FromRgb(196, 255, 255);
                }
                cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                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 = rname;
                p1.Inlines.Add(s1);
                cell1.Blocks.Add(p1);
                row1.Cells.Add(cell1);

                cell2 = new TableCell();
                cell2.Background = Color.FromRgb(255, 255, 255);
                cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                p2 = new Paragraph();
                s2 = new Span();
                s2.FontFamily = new FontFamily(CGlobals.docu_default_font);
                s2.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                s2.Text = " ";
                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;   
        }

How can we add a Content Control with StdProperties to one of the table cells, similar to:

SdtProperties sdtProperties = new SdtProperties(SdtType.RichText) 
{ 
    Alias = "AliasName", 
    Lock = Lock.SdtContentLocked, 
}; 
doc_text.InsertStructuredDocumentTag(sdtProperties); 

1 Answer, 1 is accepted

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

Hi Steve,

You can use the following approach for this:

cell2 = new TableCell();
cell2.Background = Color.FromRgb(255, 255, 255);
cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);

SdtProperties sdtProperties = new SdtProperties(SdtType.RichText)
{
    Alias = "AddressName",
    Lock = Lock.SdtContentLocked,
    ID = 1001,
};

SdtRangeStart sdtRangeStart = new SdtRangeStart();
SdtRangeEnd sdtRangeEnd = new SdtRangeEnd();
sdtRangeStart.SdtProperties = sdtProperties;
sdtRangeStart.End = sdtRangeEnd;
p2 = new Paragraph();
p2.Inlines.Add(sdtRangeStart);
p2.Inlines.Add(new Span(rname));
p2.Inlines.Add(sdtRangeEnd);

cell2.Blocks.Add(p2);
row1.Cells.Add(cell2);
table.Rows.Add(row1);

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, 07:30 AM

Hi Dimitar,

Thank you for your answer.

Could a similar approach also be used to add a DocumentVariableField to a table cell?

See https://www.telerik.com/forums/how-can-i-add-a-documentvariablefield-to-a-table-cell-programmatically

Dimitar
Telerik team
commented on 26 Jul 2024, 08:51 AM

Hi Steve, 

This would not work for the fields since their content is generated depending on some document properties. I have answered your question in the referred forum. 

Let me know if you have additional questions.

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