I'm having problems creating a table, for example, I need the first row of the table to have 16 cells with a percentage of different sizes for each cell, in the second line that has 8 cells with a percentage of different sizes for each cell, and a third line that also has different cell counts and sizes. When I open the file with LibreOffice it works as I wanted, but when I open it in Word it doesn't respect the dimensions I pre-established
I wanted the result to be the same as in Libre Office, but I'm not getting any way. :(
I created a structure to facilitate the filling of different texts, see how it works.
public Table TemplateTable()
{
Table table = new Table();
// set the table width
TableProperties tableProperties = table.GetFirstChild<TableProperties>();
if (tableProperties == null)
{
tableProperties = new TableProperties();
table.InsertAt(tableProperties, 0);
}
TableWidth tableWidth = new TableWidth() { Width = "7.88in", Type = TableWidthUnitValues.Dxa };
tableProperties.TableWidth = tableWidth;
// set the left spacing
TableIndentation tableIndentation = tableProperties.GetFirstChild<TableIndentation>();
if (tableIndentation == null)
{
tableIndentation = new TableIndentation() { Type = TableWidthUnitValues.Dxa };
tableProperties.InsertBefore(tableIndentation, tableProperties.TableWidth);
}
string strValue = "-72";
Int32Value intValue = new Int32Value();
intValue = Int32Value.FromInt32(int.Parse(strValue));
tableIndentation.Width = intValue; // -0.05 inches in twentieths of a point (Dxa)
return table;
}
public TableCell TemplateTableCell(
string text,
string width,
JustificationValues justificationValues = JustificationValues.Center,
string fontSize = "12",
string colorHex = "#000000",
string borderColorHex = "#000000"
)
{
EndMargin EndMargin = new EndMargin() { Width = "200" };
TableCellMargin margin = new TableCellMargin(
new LeftMargin() { Width = "0.05%", Type = TableWidthUnitValues.Dxa },
new RightMargin() { Width = "0.05%", Type = TableWidthUnitValues.Dxa }
);
var tableCellBorders = new TableCellBorders();
var topBorder = new TopBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
var leftBorder = new LeftBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
var bottomBorder = new BottomBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
var rightBorder = new RightBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
var insideHorizontalBorder = new InsideHorizontalBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
var insideVerticalBorder = new InsideVerticalBorder { Val = BorderValues.Single, Size = 6, Color = borderColorHex };
tableCellBorders.AppendChild(topBorder);
tableCellBorders.AppendChild(leftBorder);
tableCellBorders.AppendChild(bottomBorder);
tableCellBorders.AppendChild(rightBorder);
tableCellBorders.AppendChild(insideHorizontalBorder);
tableCellBorders.AppendChild(insideVerticalBorder);
TableCellProperties cellProperties = new TableCellProperties(
new TableCellWidth { Width = width, Type = TableWidthUnitValues.Dxa },
new NoWrap { Val = OnOffOnlyValues.Off },
new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Exact, Before = "10", After = "10" },
margin,
tableCellBorders
);
Paragraph paragraph = new Paragraph(new Run(new Text(text == "" ? " " : text))
{
RunProperties = new RunProperties(new FontSize { Val = fontSize }, new RunFonts { Ascii = "Calibri", HighAnsi = "Calibri" }, new Color() { Val = colorHex }),
});
//cellProperties.Append(new Color() { Val = colorHex });
paragraph.ParagraphProperties = new ParagraphProperties(new Justification { Val = justificationValues });
TableCell tableCell = new TableCell(cellProperties);
tableCell.AppendChild(paragraph);
return tableCell;
}
I'm starting to think this isn't possible to do in Word. Has anyone managed to do this? Thank you in advance if you managed and can you explain to me why I'm not getting it.

