We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table Cell Merge

Hi,
Please see my desired table format:

Task Number Title Training Location Sust Tng Freq Sust Tng SL
Skill Level 1

And here is the code:

tasksTblBuilder.startTable();
tasksTblBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
tasksTblBuilder.getCellFormat().getBorders().setLineWidth(1);
tasksTblBuilder.setBold(true);
tasksTblBuilder.insertCell();
tasksTblBuilder.write("Task Number");
tasksTblBuilder.insertCell();
tasksTblBuilder.write("Title");
tasksTblBuilder.insertCell();
tasksTblBuilder.write("Training Location");
tasksTblBuilder.insertCell();
tasksTblBuilder.write("Sust Tng Freq");
tasksTblBuilder.insertCell();
tasksTblBuilder.write("Sust Tng Sl");
tasksTblBuilder.setBold(false);
tasksTblBuilder.endRow();

for (Stpskilllevel skillLevel:stp.getStpskilllevels())
{
    tasksTblBuilder.setBold(true);
    tasksTblBuilder.insertCell();
    tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
    tasksTblBuilder.insertCell();
    tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
    tasksTblBuilder.write("Skill Level 1");
    tasksTblBuilder.insertCell();
    tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
    tasksTblBuilder.insertCell();
    tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);

    tasksTblBuilder.insertCell();
    tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
    tasksTblBuilder.setBold(false);
    tasksTblBuilder.endRow();
}
tasksTblBuilder.endTable();

The table is displayed correctly, but the text “Skill Level 1” doesn’t get displayed. Can you please help?

Hi
Thanks for your request. You should insert text into the first of merged cells. Please see the following code snippet.

tasksTblBuilder.setBold(true);
tasksTblBuilder.insertCell();
tasksTblBuilder.write("Skill Level 1");
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.setBold(false);
tasksTblBuilder.endRow();

Hope this helps.
Best regards.

Thank you, Alexey. Actually, I knew I was able to get the text displayed when I move the text to the first column and merge columns in order. I was, though, hoping to align the text in the center when I merge the whole columns. It looks like that I have to create a paragraph to align the text in the center after the columns are merged. Am I right? CellFormat has the vertical alignment method, but not horizontal alignment. So, creating a paragraph to align the text in center and add the node to the cell should be the only option. Please confirm this if I am correct.
Thanks!
Michelle

Hi
Thanks for your request. Yes you are right. You should use ParagraphFormat.Alignment. See the following code:

tasksTblBuilder.setBold(true);
tasksTblBuilder.insertCell();
tasksTblBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
tasksTblBuilder.write("Skill Level 1");
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.insertCell();
tasksTblBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
tasksTblBuilder.setBold(false);
tasksTblBuilder.endRow();

Hope this helps.
Best regards.

Thank you so much, Alexey, for your confirmation.