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

Free Support Forum - aspose.com

How to merge generated table cells?

We have buy your Aspose.Words components, but We found a lot of limits comparing with Ms word Automation. Now We we want to merge two genereated cells, do you have some methods? We don't want the DocumentBuilder's CellFormat way because it works only on generating.
This message was posted using Aspose.Live 2 Forum

Hi Andy,

Thanks for your inquiry.

You can learn about setting CellMerge in the API documentation here. Could you please specify how exactly you are working with your cells if you do not want to do this through DocumentBuilder. Attaching your code and template document may help as well.

Also could you clarify what features that are in Automation that are not in Aspose.Words. Prehaps you have overlooked them in Aspose.Words. We will be glad to give you some assitance.

Thanks,

thanks for your response. In word automation, the cell object provide a Merge method which can merge to a certain cell like the following:

table.Cell(1, 2).Merge(table.Cell(2, 2));

this method is very useful, because we can merge any cell after the table generated. In our code when using word automation, we can provide a general method to insert a datatable to document. such as : Helper.InsertTable(DataTable dt) which return a Aspose.Words.Tables.Table object. Thru this returned table we can merge the cells we want. but now, We must merge cells on building the table.

thanks.

Hi Andy,

Thanks for this additional information.

Merging existing table cells in Aspose.Words is easily possible although it is different to the way automation is set out. We will look into providing a simplier way to do this in the API sometime in the future. I have linked this issue in our database and we will keep you informed of any developments.

In the mean time I have made a implementation which will achieve this in the same way that automation handles it. Please see the code below.

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

Document doc = new Document("Document.docx");

// Retrieve the first table in the body of the first section.

Table table = doc.FirstSection.Body.Tables[0];

// Merge all the cells between position (C1, R1) and (C3, R2) into one.

MergeCells(table.FirstRow.FirstCell, table.Rows[1].Cells[2]);

// Save the document.

doc.Save("Document Out.docx");

///

/// Merges the cells between two cells horizontally and vertically. Can span over multiple rows.

///

/// The cell where to start merging other cells together

/// The cell where to stop the merging of other cells

public static void MergeCells(Cell startCell, Cell endCell)

{

Table startParentTable = startCell.ParentRow.ParentTable;

Table endParentTable = startCell.ParentRow.ParentTable;

Row startParentRow = startCell.ParentRow;

Row endParentRow = endCell.ParentRow;

if (startCell == null)

throw new ArgumentException("The start cell cannot be null.");

if (endCell == null)

throw new ArgumentException("The end cell cannot be null.");

if (!startParentTable.Equals(endParentTable))

throw new ArgumentException("Both cells must belong to the same table.");

Point startCellPos = new Point(startParentRow.IndexOf(startCell), startParentTable.IndexOf(startParentRow));

Point endCellPos = new Point(endParentRow.IndexOf(endCell), startParentTable.IndexOf(endParentRow));

if(startCellPos.X > endCellPos.X)

{

int tempPos = startCellPos.X;

startCellPos.X = endCellPos.X;

endCellPos.X = tempPos;

}

if (startCellPos.Y > endCellPos.Y)

{

int tempPos = startCellPos.Y;

startCellPos.Y = endCellPos.Y;

endCellPos.Y = tempPos;

}

foreach (Row row in startParentTable.Rows)

{

foreach (Cell cell in row.Cells)

{

Point currentPos = new Point(row.IndexOf(cell), startParentTable.IndexOf(row));

if (BetweenPoints(currentPos, startCellPos, endCellPos))

{

if (startCellPos.X != endCellPos.X)

{

if (currentPos.X == startCellPos.X)

cell.CellFormat.HorizontalMerge = CellMerge.First;

else

cell.CellFormat.HorizontalMerge = CellMerge.Previous;

}

if (startCellPos.Y != endCellPos.Y)

{

if (currentPos.Y == startCellPos.Y)

cell.CellFormat.VerticalMerge = CellMerge.First;

else

cell.CellFormat.VerticalMerge = CellMerge.Previous;

}

}

}

}

}

///

/// Returns true if the point defined by currentPos is inside the start and end points

///

/// The starting position

/// The ending position

/// The current position

///

public static bool BetweenPoints(Point currentPos, Point startCellPos, Point endCellPos)

{

return (currentPos.X >= startCellPos.X && currentPos.X <= endCellPos.X && currentPos.Y >= startCellPos.Y && currentPos.Y <= endCellPos.Y);

}

Depending on the version of .NET you are using, you may want improve calling to the method by making it an extension method (so it can be called like table.FirstRow.FirstCell.Merge(table.FirstRow.Cells[3]) instead).

If you have any further queries, please feel free to ask.

Thanks,

really appreciated, It works well. thanks.

Thanks a lot for taking the time to write this, it did just what I needed. I am using the Java version of the API and had to do a quick translation. The version I came up with is pasted below for anyone else who needs to do this in Java.

Thanks,
Alex



/
* Merges the cells between two cells horizontally and vertically. Can span
* over multiple rows
*/
public static void MergeCells(Cell startCell, Cell endCell) {

if (startCell == null) {
System.out.println(“Start cell is null”);
throw new IllegalArgumentException(“The start cell cannot be null.”);
}

if (endCell == null) {
throw new IllegalArgumentException(“The end cell cannot be null.”);
}

Table startParentTable = startCell.getParentRow().getParentTable();

Table endParentTable = startCell.getParentRow().getParentTable();

Row startParentRow = startCell.getParentRow();

Row endParentRow = endCell.getParentRow();

if (!startParentTable.equals(endParentTable)) {
throw new IllegalArgumentException(“Both cells must belong to the same table.”);
}

Point startCellPos = new Point(startParentRow.indexOf(startCell), startParentTable.indexOf(startParentRow));
Point endCellPos = new Point(endParentRow.indexOf(endCell), startParentTable.indexOf(endParentRow));

if (startCellPos.getX() > endCellPos.getX()) {
double tempPos = startCellPos.getX();
startCellPos.setLocation(endCellPos.getX(), startCellPos.getY());
endCellPos.setLocation(tempPos, endCellPos.getY());
}

if (startCellPos.getY() > endCellPos.getY()) {
double tempPos = startCellPos.getY();
startCellPos.setLocation(startCellPos.getX(), endCellPos.getY());
endCellPos.setLocation(endCellPos.getX(), tempPos);
}

for (Row row : startParentTable.getRows()) {

for (Cell cell : row.getCells()) {
Point currentPos = new Point(row.indexOf(cell), startParentTable.indexOf(row));

if (BetweenPoints(currentPos, startCellPos, endCellPos)) {

if (startCellPos.getX() != endCellPos.getX()) {

if (currentPos.getX() == startCellPos.getX()) {
cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
} else {
cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
}

if (startCellPos.getY() != endCellPos.getY()) {

if (currentPos.getY() == startCellPos.getY()) {
cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
} else {
cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
}
}
}
}
}

}

/

* Returns true if the point defined by currentPos is inside the start and
* end points
*/
public static boolean BetweenPoints(Point currentPos, Point startCellPos, Point endCellPos) {
return (currentPos.getX() >= startCellPos.getX() && currentPos.getX() <= endCellPos.getX()
&& currentPos.getY() >= startCellPos.getY() && currentPos.getY() <= endCellPos.getY());
}

Hi Alex,

Thanks for posting your translation here for others to use. It's great that you found the code snippet useful.

Thanks,

A post was split to a new topic: Merge selected range of table cells in Word document using C# - apply bullet-points