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

Free Support Forum - aspose.com

Merge Cell is not working

When I am creating a document programmatically with aspose like below and then appending with another document the merge cells are not retained. Please see the below code reproduce. “SomePage.docx” can be any document.


private static void cellMergeTest() throws Exception {

Document firstDoc = new Document(new FileInputStream(new File(DATA_DIR+"SomePage.docx")));

new DocumentBuilder(firstDoc).write("This is the first page of the document");

Document tableDoc = new Document();

DocumentBuilder builder = new DocumentBuilder(tableDoc);

builder.insertCell();

builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);

builder.write("Text in merged cells.");


builder.insertCell();

// This cell is merged to the previous and should be empty.

builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);

builder.endRow();


builder.insertCell();

builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);

builder.write("Text in one cell.");


builder.insertCell();

builder.write("Text in another cell.");

builder.endRow();

builder.endTable();

tableDoc.save(OUT_DIR + "tableDoc.docx");


firstDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

firstDoc.appendDocument(tableDoc, ImportFormatMode.USE_DESTINATION_STYLES);

firstDoc.updatePageLayout();

firstDoc.updateTableLayout();

firstDoc.save(OUT_DIR+"mergedDoc.docx");

}

Hi Abhradeep,

Thanks for your inquiry. Please do not call Document.updateTableLayout method before saving the final document. You do not normally need to call this method as cell and table widths are maintained automatically. You can call this method before exporting to PDF (or any other fixed-page format), only in rare cases where you confirmed that tables appear incorrectly laid out in the output document. Calling this method might help to correct the output.

So if I am not calling this method then my table layout like Auto column width/ fixed column width are not working in pdf. What to do? and if call this method then my horizontal column span is not working.

Hi Abhradeep,

Thanks for your
inquiry. Document.UpdateTableLayout method updates widths of cells and tables in the document according to their preferred widths and content. You do not need to call this method if the tables appear correct in the output document.

As shared in my previous post, you can call this
method before exporting to PDF (or any other fixed-page format), only
in rare cases where you confirmed that tables appear incorrectly laid
out in the output document. Calling this method might help to correct
the output.

Hope this answers your query. Please let us know if you have any more queries.