Hi Jan,
Thanks for considering Aspose.
1. Yes the basis of your code is correct, there were only a few changes needed to achieve the correct output. These changes stop the bottom line dissapearing and merge the cell vertically. I have made the changes to your code below.
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder();
docBuilder.setDocument(doc);
docBuilder.getRowFormat().setAllowAutoFit(false);
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().setWidth(50);
docBuilder.startTable();
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.endRow();
docBuilder.getCellFormat().setWidth(75);
docBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.GRAY);
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.getCellFormat().getShading().clearFormatting();
docBuilder.endRow();
docBuilder.insertCell();
docBuilder.write("Merged Cell");
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
docBuilder.insertCell();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
docBuilder.endRow();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
docBuilder.endRow();
docBuilder.insertCell();
docBuilder.insertCell();
docBuilder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
docBuilder.endRow();
docBuilder.getCellFormat().setVerticalMerge(CellMerge.NONE);
docBuilder.endTable();
2. Currently there is no API method to achieve this functionality in one line of code. We will look into implementing this in the future and will keep you informed of any developments (through a message on this thread when this feature is avaliable).
In the mean time it is easy to achieve this functionality manually using the Aspose.Words API. I have put together a quick implementation below which takes a table passed to the method and fits it to the page width, while keeping the cell sizes of each row in proportion. Please find the method below.
public static void fitTableToPageWidth(Table table) throws Exception
{
//Get the page settings from the section where this table occurs, as each section can have different page settings.
Section section = (Section)table.getAncestor(NodeType.SECTION);
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
//First run simply gets the table size (the widest row). This is used to calculate the ratio below instead of just each row length
//as the last cell in one row could be shorter than the last cell in the other row. This will preserve these different sizes when fitting.
double tableWidth = 0;
for(Row row : table.getRows())
{
double rowWidth = 0;
for(Cell cell : row.getCells())
{
rowWidth += cell.getCellFormat().getWidth();
}
//If this row is larger than previous set this width as the longest row.
if(rowWidth > tableWidth)
tableWidth = rowWidth;
}
//Calculate the width of the page
double pageWidth = section.getPageSetup().getPageWidth() - (section.getPageSetup().getLeftMargin() + section.getPageSetup().getRightMargin());
//In the second run set each cell in the row proportionally to the width of the page
for(Row row : table.getRows())
{
for(Cell cell : row.getCells())
{
//Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
double cellRatio = cell.getCellFormat().getWidth() / tableWidth;
cell.getCellFormat().setWidth(cellRatio * pageWidth);
}
}
}
To use this method you can simply change one of the lines in your main code to get the table object and then pass it to the method above:
Table table = docBuilder.startTable();
// Rest of code here
fitTableToPageWidth(table);
If you have any other queries please feel free to ask.
Thanks,