Trying to make the text content in the TableCell object wrapping, but doesn’t work out, any body has ideas? Many thanks in advanced.
Hi Henry,
Can you please share a snippet code that demonstrates the issue you have?
I’m not sure what you mean exactly by “TableCell object wrapping” because in the documents the text in the cells is wrapped.
Regards,
Mario
Hi Mario,
Thank you for reaching out. Here is the sound block of the codes:
var tblOPosition = new Table(document);
TableStyle tsTableMultiPostionOverview = new TableStyle("tsTableMultiPostionOverview") { TableFormat = { PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage), DefaultCellSpacing = 0, AutomaticallyResizeToFitContents = false, Alignment = GemBox.Document.HorizontalAlignment.Center } };
tsTableMultiPostionOverview.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.Single, new Color(0, 0, 0), 0.5);
document.Styles.Add(tsTableMultiPostionOverview);
tblOPosition.TableFormat.Style = tsTableMultiPostionOverview;
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(5, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(5, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(8, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(10, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(20, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(6, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(16.5, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(16.5, TableWidthUnit.Percentage)).Value });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(13, TableWidthUnit.Percentage)).Value });
TableCell cC1 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
cC1.Blocks.Add(new Paragraph(document, new Run(document, " c1 header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC2 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224), WrapText = true } };
cC2.Blocks.Add(new Paragraph(document, new Run(document, " c2 header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC3 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
cC3.Blocks.Add(new Paragraph(document, new Run(document, " c3 Header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC4 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
cC4.Blocks.Add(new Paragraph(document, new Run(document, " c4 Header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC5 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
cC5.Blocks.Add(new Paragraph(document, new Run(document, " c5 Header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC6 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
cC6.Blocks.Add(new Paragraph(document, new Run(document, " c6 Header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC7 = new TableCell(document) { ColumnSpan = 2 };
Table tSubTable = new Table(document);
tSubTable.TableFormat.Style = tsTableMultiPostionOverview;
tSubTable.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(50, TableWidthUnit.Percentage)).Value });
tSubTable.Columns.Add(new TableColumn() { PreferredWidth = (new TableWidth(50, TableWidthUnit.Percentage)).Value });
TableCell cC7Top = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224), WrapText = true }, ColumnSpan = 2 };
cC7Top.Blocks.Add(new Paragraph(document, new Run(document, " C7 Header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableRow r1 = new TableRow(document) { RowFormat = { Height = new TableRowHeight(14, TableRowHeightRule.Exact) } };
r1.Cells.Add(cC7Top);
tSubTable.Rows.Add(r1);
TableCell cC7Sub1 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224), WrapText = true } };
cC7Sub1.Blocks.Add(new Paragraph(document, new Run(document, " Sub1-C7") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableCell cC7Sub2 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224), WrapText = true } };
cC7Sub2.Blocks.Add(new Paragraph(document, new Run(document, " Sub2-C7") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableRow r2 = new TableRow(document) { RowFormat = { Height = new TableRowHeight(14, TableRowHeightRule.Exact) } };
r2.Cells.Add(cC7Sub1);
r2.Cells.Add(cC7Sub2);
tSubTable.Rows.Add(r2);
cC7.Blocks.Add(tSubTable);
TableCell cC8 = new TableCell(document) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
cC8.Blocks.Add(new Paragraph(document, new Run(document, "C8 Header") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = true } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
TableRow headerRow = new TableRow(document) { RowFormat = { Height = new TableRowHeight(29, TableRowHeightRule.Exact) } };
headerRow.Cells.Add(cC1);
headerRow.Cells.Add(cC2);
headerRow.Cells.Add(cC3);
headerRow.Cells.Add(cC4);
headerRow.Cells.Add(cC5);
headerRow.Cells.Add(cC6);
headerRow.Cells.Add(cC7);
headerRow.Cells.Add(cC8);
tblOPosition.Rows.Add(headerRow);
int i = 1;
foreach (SRPositionVM position in srForm.SRPosition)
{
var rRow = new TableRow(document) { RowFormat = { Height = new TableRowHeight(15, TableRowHeightRule.Exact) } };
cC1 = new TableCell(document);
cC1.Blocks.Add(new Paragraph(document, new Run(document, " " + i) { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
cC2 = new TableCell(document);
cC2.Blocks.Add(new Paragraph(document, new Run(document, "short content2") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
cC3 = new TableCell(document);
cC3.Blocks.Add(new Paragraph(document, new Run(document, "short content3") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
cC4 = new TableCell(document);
cC4.Blocks.Add(new Paragraph(document, new Run(document, "short content2") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
cC5 = new TableCell(document);
cC5.Blocks.Add(new Paragraph(document, new Run(document, " " + "short content5") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center } });
cC6 = new TableCell(document) { CellFormat = { WrapText = true } };
cC6.Blocks.Add(new Paragraph(document, new Run(document, "short content6") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center, KeepLinesTogether = false } });
cC7Sub1 = new TableCell(document) { CellFormat = { WrapText = true } };
cC7Sub1.Blocks.Add(new Paragraph(document, new Run(document, " " + "long content7 expecting wrap text1") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center, KeepLinesTogether = false } });
cC7Sub2 = new TableCell(document) { CellFormat = { WrapText = true } };
cC7Sub2.Blocks.Add(new Paragraph(document, new Run(document, " " + "long content7 expecting wrap text2") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center, KeepLinesTogether = false } });
cC8 = new TableCell(document) { CellFormat = { WrapText = true } };
cC8.Blocks.Add(new Paragraph(document, new Run(document, "short content8") { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }) { ParagraphFormat = { Alignment = GemBox.Document.HorizontalAlignment.Center, KeepLinesTogether = false } });
rRow.Cells.Add(cC1);
rRow.Cells.Add(cC2);
rRow.Cells.Add(cC3);
rRow.Cells.Add(cC4);
rRow.Cells.Add(cC5);
rRow.Cells.Add(cC6);
rRow.Cells.Add(cC7Sub1);
rRow.Cells.Add(cC7Sub2);
rRow.Cells.Add(cC8);
tblOPosition.Rows.Add(rRow);
i++;
}
multiSection.Blocks.Add(tblOPosition);
Hi Henry,
This is the resulting Table that I got:
What issue do you have with it? What would you like to change here?
Regards,
Mario
I think I know what your problem is, it’s that “long content7 expecting wrap text1” does not fit into the cell. The reason why this occurs is because the table row has the exact height:
var rRow = new TableRow(document) { RowFormat = { Height = new TableRowHeight(15, TableRowHeightRule.Exact) } };
Try removing that formatting.
Also, try the following, I changed the table style definition so that you don’t have to set the desired formatting on each TableCell
, Paragraph
, and Run
element:
var document = new DocumentModel();
var tsTableMultiPostionOverview = new TableStyle("tsTableMultiPostionOverview")
{
CharacterFormat =
{
FontName = CONTENT_FONT_STYLE,
Size = 11
},
ParagraphFormat =
{
Alignment = HorizontalAlignment.Center,
SpaceAfter = 0
},
TableFormat =
{
PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage),
DefaultCellSpacing = 0,
AutomaticallyResizeToFitContents = false,
Alignment = HorizontalAlignment.Center
}
};
tsTableMultiPostionOverview.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.Single, new Color(0, 0, 0), 0.5);
tsTableMultiPostionOverview.ConditionalFormats[TableStyleFormatType.FirstRow].CellFormat = new TableCellFormat() { BackgroundColor = new Color(224, 224, 224) };
tsTableMultiPostionOverview.ConditionalFormats[TableStyleFormatType.FirstRow].CharacterFormat = new CharacterFormat() { Bold = true };
document.Styles.Add(tsTableMultiPostionOverview);
var tblOPosition = new Table(document);
tblOPosition.TableFormat.Style = tsTableMultiPostionOverview;
tblOPosition.TableFormat.StyleOptions |= TableStyleOptions.FirstRow;
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 5 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 5 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 8 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 10 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 20 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 6 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 16.5 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 16.5 });
tblOPosition.Columns.Add(new TableColumn() { PreferredWidth = 13 });
var headerRow1 = new TableRow(document);
var headerRow2 = new TableRow(document);
tblOPosition.Rows.Add(headerRow1);
tblOPosition.Rows.Add(headerRow2);
var cC1 = new TableCell(document, new Paragraph(document, " c1 header")) { RowSpan = 2 };
var cC2 = new TableCell(document, new Paragraph(document, " c2 header")) { RowSpan = 2 };
var cC3 = new TableCell(document, new Paragraph(document, " c3 header")) { RowSpan = 2 };
var cC4 = new TableCell(document, new Paragraph(document, " c4 header")) { RowSpan = 2 };
var cC5 = new TableCell(document, new Paragraph(document, " c5 header")) { RowSpan = 2 };
var cC6 = new TableCell(document, new Paragraph(document, " c6 header")) { RowSpan = 2 };
var cC7 = new TableCell(document, new Paragraph(document, " c7 header")) { ColumnSpan = 2 };
var cC8 = new TableCell(document, new Paragraph(document, " c8 header")) { RowSpan = 2 };
headerRow1.Cells.Add(cC1);
headerRow1.Cells.Add(cC2);
headerRow1.Cells.Add(cC3);
headerRow1.Cells.Add(cC4);
headerRow1.Cells.Add(cC5);
headerRow1.Cells.Add(cC6);
headerRow1.Cells.Add(cC7);
headerRow1.Cells.Add(cC8);
var cC7Sub1 = new TableCell(document, new Paragraph(document, " Sub1-C7")) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
var cC7Sub2 = new TableCell(document, new Paragraph(document, " Sub2-C7")) { CellFormat = { BackgroundColor = new Color(224, 224, 224) } };
headerRow2.Cells.Add(cC7Sub1);
headerRow2.Cells.Add(cC7Sub2);
int i = 1;
foreach (SRPositionVM position in srForm.SRPosition)
{
var rRow = new TableRow(document,
new TableCell(document, new Paragraph(document, " " + i)),
new TableCell(document, new Paragraph(document, "short content2")),
new TableCell(document, new Paragraph(document, "short content3")),
new TableCell(document, new Paragraph(document, "short content4")),
new TableCell(document, new Paragraph(document, "short content5")),
new TableCell(document, new Paragraph(document, "short content6")),
new TableCell(document, new Paragraph(document, "long content7 expecting wrap text1")),
new TableCell(document, new Paragraph(document, "long content7 expecting wrap text2")),
new TableCell(document, new Paragraph(document, "short content8")));
tblOPosition.Rows.Add(rRow);
i++;
}
multiSection.Blocks.Add(tblOPosition);
I hope this helps.
Regards,
Mario