Table Measurement Issue

1 Answer 339 Views
PdfProcessing
Tim
Top achievements
Rank 3
Iron
Iron
Iron
Tim asked on 21 Jun 2021, 09:13 PM | edited on 21 Jun 2021, 09:55 PM

This code creates a document with 3 tables of increasing height. However, when measured each has the same height. How can a table's height be measured in a way that will account for text that wraps onto multiple lines?

Thanks,

Tim

 

        protected void AddTable(string header, string text, RadFixedDocumentEditor editor)
        {
            Table table = new Table();
            RgbColor bordersColor = new RgbColor(149, 179, 215);
            Border border = new Border(1, Telerik.Windows.Documents.Fixed.Model.Editing.BorderStyle.Single, bordersColor);
            table.Borders = new TableBorders(border);
            table.DefaultCellProperties.Borders = new TableCellBorders(border, border, border, border);
            table.DefaultCellProperties.Padding = new Thickness(5);
            table.Margin = new Thickness(10);

            TableRow tableRow = table.Rows.AddTableRow();
            TableCell tableCell = tableRow.Cells.AddTableCell();
            tableCell.Borders = new TableCellBorders(new Border(BorderStyle.None));

            Block block = tableCell.Blocks.AddBlock();
            block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;

            block.InsertText(header);

            tableRow = table.Rows.AddTableRow();
            tableCell = tableRow.Cells.AddTableCell();
            block = tableCell.Blocks.AddBlock();
            block.InsertText(text);

            double th = table.Measure().Height;
            editor.InsertTable(table);
            block = new Block();
            block.InsertText(th.ToString());
            editor.InsertBlock(block);
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            RadFixedDocument radFixedDocument = new RadFixedDocument();
            using (RadFixedDocumentEditor editor = new RadFixedDocumentEditor(radFixedDocument))
            {
                AddTable("Table 1", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore", editor);

                AddTable("Table 2", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", editor);

                AddTable("Table 3", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", editor);
            }

            PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
            using (FileStream fileStream = File.Open(@"C:\Temp\Test.pdf", FileMode.Create))
            {
                pdfFormatProvider.Export(radFixedDocument, fileStream);
            }
        }

                

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 23 Jun 2021, 06:41 AM

Hi Tim,

When using the Measure method you need to pass the available width, by default the method measures with positive infinity where the text will have only one line. Here is an example: 

double th = table.Measure(new Size(690, double.PositiveInfinity)).Height; 

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

Regards,
Dimitar
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/.

Tim
Top achievements
Rank 3
Iron
Iron
Iron
commented on 23 Jun 2021, 01:44 PM

That works. Thank you!
Tags
PdfProcessing
Asked by
Tim
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or