Free Support Forum - aspose.com

Merging of table cells along with setting table.setAllowAutoFit() produces incorrect table format

Hi Team,

I’ve created a table of 2 rows, 4 columns.
If I merge the first 3 cells of row 1 and create 4 columns (non-merged) in the 2nd row and then set table.setAllowAutoFit(true), the cell merge in row 1 is not aligned with the second row.

Additionally, for first column in the 2nd row, cell width is set to some double value.

Could you let me know how to fix this?

Thanks,
Kumar

Hi Kumar,


Thanks for your inquiry. Could you please attach your Word document along with code snippet here for testing? I will investigate the issue on my side and provide you more information.

Hi Tahir,

The code is bit complex to find/pick a code snippet to replicate it standalone. However, I’ve attached the generated word documents.

Additionally, I’ve set column width to the 1st cell of 2nd row.

Attachments
With_table.setAllowAutoFit(true).doc : This document has issue. In the code table.setAllowAutoFit(true) is executed.
Without_table.setAllowAutoFit(true).doc : This document is good. In the code table.setAllowAutoFit(true) NOT is executed.

Could you explain how can table.setAllowAutoFit(true) cause the difference with respect to attached documents.

Thanks,
Kumar

Hi Kumar,


Please accept my apologies for your inconvenience. Please note that a table in MS Word is a set of independent rows. Each row has a set of cells independent on cells of other rows. So there is no logical “column” in a MS Word’s table.

It is hard to investigate the issue without code snippet or complete scenario detail. It would be great if you please share some more detail about your scenario along with code snippet. I will investigate the issue on my side and provide you more information.

Hi Tahir,

As I said before, I’m not able to replicate the issue with the standalone code. The following code looks similar to what I’ve in my complex code. This may help you understand the scenario. Check out my previous comments (with attachments) along with the current to understand the issue better.

import com.aspose.words.Cell;
import com.aspose.words.CellMerge;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.PreferredWidth;
import com.aspose.words.Table;

public class CellMergeDemo {

public static void main(String[] args) throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
mergeCellTest(builder, doc);
save(doc);
System.out.println(“done”);
}

private static void mergeCellTest(DocumentBuilder builder, Document doc)
throws Exception {

Table table = builder.startTable();
Cell cell = null;
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
builder.write(“Looooooooooooooooooooooooong Text in merged cells.”);

builder.insertCell();
// This cell is merged to the previous and should be empty.
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);

cell = builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);

cell = builder.insertCell();
cell.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.write(“Text in one cell.”);

builder.endRow();

// ######### was trying out combination as in our code
// table.setPreferredWidth( PreferredWidth.fromPercent( 100));
// table.setPreferredWidth(PreferredWidth.AUTO);
// table.autoFit( AutoFitBehavior.AUTO_FIT_TO_CONTENTS);

cell = builder.insertCell();
cell.getCellFormat().setWidth(50);
cell.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(50));
builder.write(“Text in one cell.”);

cell = builder.insertCell();
builder.write(“Text in one cell.”);

cell = builder.insertCell();
builder.write(“Text in one cell.”);

cell = builder.insertCell();
builder.write(“Text in one cell.”);
builder.endRow();

// ########### With or without the below line, the output is the same.
table.setAllowAutoFit(true);

}

private static void save(Document doc) throws Exception {
doc.save(“C:\test_old.doc”);
doc.save(“C:\test_old.pdf”);
}

}


Thanks,
Kumar

Hi Kumar,


Thanks for sharing the details. I am working over your query and will update you asap.

Hi Kumar,


Thanks for your patience.

Please note that a table in MS Word is a set of independent rows. Each row has a set of cells independent on cells of other rows. So there is no logical “column” in a MS Word’s table. “The 1st column” is something like “a set of the 1st cells of each row in a table”.

For example, it’s possible to have a table where the 1st row consists of two cells: 2cm and 1cm and the 2nd row consists of different two cells: 1cm and 2cm of width.

I suggest you, please set the size of each cell instead of merging columns as shown in following code snippet. Hope this helps you.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:green;mso-font-kerning:
0pt;mso-no-proof:yes”>//Create Document and DocumentBuilder<o:p></o:p>

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//Build table

builder.insertCell();

builder.write("first column");

//specify width of cell

builder.getCellFormat().setWidth(250);

builder.endRow();

//note that in the next row you should also specify width of each cell

//Build table

builder.insertCell();

builder.write("first column");

//specify width of cell

builder.getCellFormat().setWidth(50);

builder.insertCell();

builder.write("Second column");

builder.getCellFormat().setWidth(200);

builder.endRow();

builder.endTable();

doc.save(MyDir + "out.doc");

Thanks Tahir for the explanation.

I’ll try out the alternative (specify width) as you suggested and if I come up a standalone code for horizontal merge issue, I’ll post it.

Hi Kumar,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.