Free Support Forum - aspose.com

Column - Row span

Hi, Please explain how column and row span works with Aspose. I tried cellformat.horizontalmerge and rowformat.verticalmerge but found some problems. Please see the attached picture and provide a sample code. In the picture, the table contains 5 rows and 5 columns and are denoted by numbers.

1. rowspan = 1, colspan = 1

2. rowspan = 1, colspan = 3

3. rowspan = 2, colspan = 1

4. rowspan = 1, colspan = 2

5. rowspan = 1, colspan = 1

6. rowspan = 1, colspan = 1

7. rowspan = 2, colspan = 2

8. rowspan = 1, colspan = 1

9. rowspan = 1, colspan = 1

10. rowspan = 3, colspan = 1

11. rowspan = 1, colspan = 1

12. rowspan = 1, colspan = 1

13. rowspan = 1, colspan = 1

14. rowspan = 1, colspan = 3

please build a table with this format. When i tried , merging is done but width is not added up when two columns are merged.

Also please set different rows different height and different columns different width. When i set the width of a cell after calling builder.InsertCell(), we get correct width, else we get the width of the last column of that row. How is cellformatting.width works?

thank you,

Ali

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. By design (by Microsoft Word design) rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width. So if you imagine first row with one wide cell and second row with two narrow cells, then looking at this document the cell in the first row will appear horizontally merged. But it is not a merged cell, it is just a single wide cell. Another perfectly valid scenario is when the first row has two cells. First cell hs CellMerge.First and second cell has CellMerge.Previous, in this case it is a merged cell. In both cases the visual appearance in MS Word is exactly the same. Both cases are valid.

Here is code that you can use to generate table:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//Define default row heigh

builder.RowFormat.Height = 50;

//Generate first row

builder.InsertCell();

builder.CellFormat.Width = 50;

//Insert text into the cell

builder.Write("1");

//Insert second cell

builder.InsertCell();

builder.CellFormat.Width = 150;

builder.Write("2");

builder.InsertCell();

builder.CellFormat.Width = 50;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("3");

//End first row

builder.EndRow();

//Generate second row

builder.InsertCell();

builder.CellFormat.Width = 100;

builder.Write("4");

//Insert second cell

builder.InsertCell();

builder.CellFormat.Width = 50;

builder.Write("5");

builder.InsertCell();

builder.Write("6");

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.EndRow();

//Generate third row

builder.InsertCell();

builder.CellFormat.Width = 100;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("7");

builder.InsertCell();

builder.CellFormat.Width = 50;

builder.Write("8");

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("10");

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write("11");

builder.EndRow();

//Generate forth row

builder.InsertCell();

builder.CellFormat.Width = 100;

builder.CellFormat.VerticalMerge = CellMerge.Previous;

//Insert second cell

builder.InsertCell();

builder.CellFormat.Width = 50;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write("9");

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write("12");

builder.EndRow();

//Generate fiveth row

builder.InsertCell();

builder.CellFormat.Width = 150;

builder.Write("14");

//Insert second cell

builder.InsertCell();

builder.CellFormat.Width = 50;

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.InsertCell();

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write("13");

builder.EndRow();

builder.EndTable();

doc.Save(@"Test279\out.doc");

Hope this helps.

Best regards.

Hi,

Thank you for the support. That works fine. But our business logic needs to create table first and then move to each cell, find out the span and merge it. I have done two sample codes. In the first one, it is perfect, but in the second one the cell with identification "6" has width more than what I have set. [Assuming that the width of the merged cell is not taken into account].

Please see the two code samples below.Please check out and tell me the reason.

1. Code1

****************

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.RowFormat.Height = 50;

builder.CellFormat.Width = 100;

for (int r = 0; r < 3; r++)

{

for (int c = 0; c < 3; c++)

{

builder.InsertCell();

}

builder.EndRow();

}

builder.EndTable();

builder.MoveToCell(0, 0, 0, 0);

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write("1");

builder.MoveToCell(0, 0, 1, 0);

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("a");

builder.MoveToCell(0, 1, 0, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write("b");

builder.MoveToCell(0, 1, 1, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.Write("b");

builder.MoveToCell(0, 0, 2, 0);

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.Width = 100;

builder.Write("2");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 1, 2, 0);

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.CellFormat.Width = 100;

builder.Write("5");

builder.MoveToCell(0, 2, 0, 0);

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write("3");

builder.MoveToCell(0, 2, 1, 0);

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.CellFormat.Width = 200;

builder.Write("4");

builder.MoveToCell(0, 2, 2, 0);

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write("c");

*****************************

2. Code2

*******************

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.RowFormat.Height = 50;

builder.CellFormat.Width = 100;

for (int r = 0; r < 3; r++)

{

for (int c = 0; c < 5; c++)

{

builder.InsertCell();

}

builder.EndRow();

}

builder.EndTable();

builder.MoveToCell(0, 0, 0, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("1");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 0, 1, 0);

builder.CellFormat.Width = 200;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("2");

builder.MoveToCell(0, 0, 2, 0);

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write(" ");

builder.MoveToCell(0, 1, 1, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write(" ");

builder.MoveToCell(0, 1, 2, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.Write(" ");

builder.MoveToCell(0, 2, 1, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write(" ");

builder.MoveToCell(0, 2, 2, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.Write(" ");

builder.MoveToCell(0, 0, 3, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("3");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 0, 4, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("4");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 1, 0, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("5");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 1, 1, 0);

builder.MoveToCell(0, 1, 2, 0);

builder.MoveToCell(0, 1, 3, 0);

builder.CellFormat.Width = 200;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("6");

builder.MoveToCell(0, 1, 4, 0);

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write(" ");

builder.MoveToCell(0, 2, 3, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write(" ");

builder.MoveToCell(0, 2, 4, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.Write(" ");

builder.MoveToCell(0, 2, 0, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("7");

builder.CellFormat.VerticalMerge = CellMerge.None;

***********************

Thank you,

Ali

Hi <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Regarding the second code snippet please see the following code:

builder.RowFormat.Height = 50;

builder.CellFormat.Width = 100;

for (int r = 0; r < 3; r++)

{

for (int c = 0; c < 5; c++)

{

builder.InsertCell();

}

builder.EndRow();

}

builder.EndTable();

builder.MoveToCell(0, 0, 0, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("1");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 0, 1, 0);

builder.CellFormat.Width = 200;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("2");

builder.MoveToCell(0, 0, 2, 0);

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write(" ");

builder.MoveToCell(0, 1, 1, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write(" ");

builder.MoveToCell(0, 1, 2, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.Write(" ");

builder.MoveToCell(0, 2, 1, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 200;

builder.Write(" ");

builder.MoveToCell(0, 2, 2, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.Write(" ");

builder.MoveToCell(0, 0, 3, 0);

builder.CellFormat.Width = 100; //Here you set width 100

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("3");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 0, 4, 0);

builder.CellFormat.Width = 100; //And here you set width 100

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("4");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 1, 0, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("5");

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.MoveToCell(0, 1, 1, 0);

builder.MoveToCell(0, 1, 2, 0);

builder.MoveToCell(0, 1, 3, 0);

builder.CellFormat.Width = 100; //So i think here should be width 100

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Write("6");

builder.MoveToCell(0, 1, 4, 0);

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.CellFormat.Width = 100; //And here

builder.Write(" ");

builder.MoveToCell(0, 2, 3, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.CellFormat.Width = 100; //And here

builder.Write(" ");

builder.MoveToCell(0, 2, 4, 0);

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.CellFormat.Width = 100; //And here

builder.Write(" ");

builder.MoveToCell(0, 2, 0, 0);

builder.CellFormat.Width = 100;

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("7");

builder.CellFormat.VerticalMerge = CellMerge.None;

Hope this helps.

Best regards.