This is a migrated thread and some comments may be shown as answers.
Table Cell Width
4 Answers 4 Views
This is a migrated thread and some comments may be shown as answers.
Claude
Top achievements
Rank 1
Claude asked on 27 Dec 2020, 05:39 PM

I have a table that has a variable number of columns (from 2 to 6)

I set preferredWidth  based on number of columns as follows:

 table1.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);

 

I am inserting images into each table cell, but I want specify the size of the image based on the cell width

 

How do I get the cell width?

var inImage = table1.Rows[x].Cells[y].Blocks.AddParagraph().Inlines.AddImageInline();
                        var cellWidth =??
                         inImage.Image.Size = new System.Windows.Size(cellWidth, cellWidth);

 

 

4 Answers, 1 is accepted

Sort by
0
Peshito
Telerik team
answered on 30 Dec 2020, 03:52 PM

Hi Claude,

You can get the width of a column by using the worksheet and specify the column index you would like to use. Then call the GetWidth method. For example:

var width = this.radSpreadsheet.Workbook.ActiveWorksheet.Columns[0].GetWidth().Value.Value;

Hope this helps. Should you have any other questions, please let me know.

Regards,
Peshito
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Claude
Top achievements
Rank 1
answered on 31 Dec 2020, 06:47 PM

This is NOT for a spreadsheet, I am looking at a Table in a WordsProcessing.  document.

If I insert a table into a document (table1)  and have preferred width to be 100%

table1.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);

 

Now I want to set the size of the Image I am placing into the cell by getting with the width of the cell.

 

var inImage = table1.Rows[x].Cells[y].Blocks.AddParagraph().Inlines.AddImageInline();                     
                      var cellWidth = get cell width     
                        inImage.Image.Size = new System.Windows.Size(cellWidth, cellWidth);                   
                        inImage.Image.LockAspectRatio = true;
                        inImage.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(firstImage, "jpg");

0
Claude
Top achievements
Rank 1
answered on 31 Dec 2020, 06:54 PM
I guess I am really looking for the column width
0
Peshito
Telerik team
answered on 04 Jan 2021, 08:58 AM

Hi Claude,

Excuse me for my misunderstanding. Please use the PreferredWidth property of the cell in order to get its width and then pass it as the inline's image width.

For instance:

            RadFlowDocument document = new RadFlowDocument();
            DocxFormatProvider provider = new DocxFormatProvider();

            using (Stream input = File.OpenRead(@"..\..\table.docx"))
            {
                document = provider.Import(input);
            }

            IEnumerable<Table> tables = document.EnumerateChildrenOfType<Table>();
            var table1 = tables.FirstOrDefault();
            table1.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
           
            using (Stream firstImage = File.OpenRead(@"C:\TEMP\imageName.png"))
            {
                var inImage = table1.Rows[0].Cells[2].Blocks.AddParagraph().Inlines.AddImageInline();
                var cellWidth = table1.Rows[0].Cells[2].PreferredWidth.Value;
                inImage.Image.Size = new System.Windows.Size(cellWidth, cellWidth);
                inImage.Image.LockAspectRatio = true;
                inImage.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(firstImage, "png");
            }

            using (Stream output = File.Create("SampleTableEdited.docx"))
            {
                provider.Export(document, output);
            }            

            ProcessStartInfo psi = new ProcessStartInfo()
            {
                UseShellExecute = true,
                FileName = @"SampleTableEdited.docx"
            };
            Process.Start(psi);

Hope this information helps.

Regards,
Peshito
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Asked by
Claude
Top achievements
Rank 1
Answers by
Peshito
Telerik team
Claude
Top achievements
Rank 1
Share this question
or