Hello,
I have a problem when adding bold text in a cell and then getting the row height. There are some specific situations when the added text fits on one line of the cell, but if I make it bold it goes on another line. I’ve tested this for different fonts and I found that for some fonts it works properly but for other it doesn’t.
For fonts like Arial or Calibri the row height is returned correctly in this scenario, but for fonts like Montserrat and Rubik the row height is smaller than the actual height.
Screenshot with the slide ( I used shapes to indicate the end of the row height ):
table example.png (17.7 KB)
Code sample:
Presentation presentation = new();
ISlide slide = presentation.Slides[0];
// Define column widths and row heights
double[] columnWidths = { 100, 100, 100 }; // Widths of 3 columns
double[] rowHeights = { 20, 20, 20 }; // Heights of 3 rows
// Add a table shape to the slide
ITable tableMontserrat = slide.Shapes.AddTable(30, 30, columnWidths, rowHeights);
var fontDataMontserrat = new FontData("Montserrat");
// Access individual cells and set some text
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
ICell cell = tableMontserrat[row, col];
IPortion portionMontserra = cell.TextFrame.Paragraphs[0].Portions[0];
portionMontserra.PortionFormat.LatinFont = fontDataMontserrat;
portionMontserra.PortionFormat.FontBold = NullableBool.True;
portionMontserra.Text = $"Row {row + 1}";
if (row == 0)
{
portionMontserra.Text = $"Row {row + 1}, 11 i";
}
slide.Shapes.AddAutoShape(
ShapeType.Rectangle,
(float)(30 + cell.OffsetX),
(float)(30 + cell.OffsetY + tableMontserrat.Rows[row].Height),
5,
5);
}
}
// Add a table shape to the slide
ITable tableRubik = slide.Shapes.AddTable(360, 30, columnWidths, rowHeights);
var fontDataRubik = new FontData("Rubik");
// Access individual cells and set some text
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
ICell cell = tableRubik[row, col];
IPortion portionRubik = cell.TextFrame.Paragraphs[0].Portions[0];
portionRubik.PortionFormat.LatinFont = fontDataRubik;
portionRubik.PortionFormat.FontBold = NullableBool.True;
portionRubik.Text = $"Row {row + 1}";
if (row == 0)
{
portionRubik.Text = $"Row {row + 1}, i";
}
slide.Shapes.AddAutoShape(
ShapeType.Rectangle,
(float)(360 + cell.OffsetX),
(float)(30 + cell.OffsetY + tableRubik.Rows[row].Height),
5,
5);
}
}
// Add a table shape to the slide
ITable tableArial = slide.Shapes.AddTable(30, 360, columnWidths, rowHeights);
var fontDataArial = new FontData("Arial");
// Access individual cells and set some text
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
ICell cell = tableArial[row, col];
IPortion portionArial = cell.TextFrame.Paragraphs[0].Portions[0];
portionArial.PortionFormat.LatinFont = fontDataArial;
portionArial.PortionFormat.FontBold = NullableBool.True;
portionArial.Text = $"Row {row + 1}";
if (row == 0)
{
portionArial.Text = $"Row {row + 1}, i";
}
slide.Shapes.AddAutoShape(
ShapeType.Rectangle,
(float)(30 + cell.OffsetX),
(float)(360 + cell.OffsetY + tableArial.Rows[row].Height),
5,
5);
}
}
// Add a table shape to the slide
ITable tableCalibri = slide.Shapes.AddTable(360, 360, columnWidths, rowHeights);
var fontDataCalibri = new FontData("Calibri");
// Access individual cells and set some text
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
ICell cell = tableCalibri[row, col];
IPortion portionCalibri = cell.TextFrame.Paragraphs[0].Portions[0];
portionCalibri.PortionFormat.LatinFont = fontDataCalibri;
portionCalibri.PortionFormat.FontBold = NullableBool.True;
portionCalibri.Text = $"Row {row + 1}";
if (row == 0)
{
portionCalibri.Text = $"Row {row + 1},cc i";
}
slide.Shapes.AddAutoShape(
ShapeType.Rectangle,
(float)(360 + cell.OffsetX),
(float)(360 + cell.OffsetY + tableCalibri.Rows[row].Height),
5,
5);
}
}
// Save the presentation
presentation.Save("output.pptx", SaveFormat.Pptx);