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

Free Support Forum - aspose.com

Table layout gets corrupted when "PAGE" field inserted using Java

When a merge field like “PAGE” is inserted somewhere, table layouts get corrupted.
I just added two lines of code to an example from your site to reproduce this.
Please find the resulting document attached.
How can this be worked around?

public void testMergeCells() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

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

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

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.insertField(“PAGE”); // destroys table layout
doc.updateTableLayout(); // doesn’t help

doc.save(“c:/tmp/aspose/TestMergeCells.doc”);
}

Hi Jen,

Thanks for your query. I have tested the scenario and have managed to reproduce the same problem at my end. I have logged this issue in our issue tracking system and you will be updated via this forum thread once this issue is resolved. If you want to add page number in header, please see the following code snippet.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

builder.write("Header");

builder.insertField("PAGE");

builder.write(" of ");

builder.insertField("NUMPAGES");

builder.moveToSection(0);

builder.insertCell();

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

builder.insertCell();

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

builder.endRow();

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.moveToDocumentEnd();

//builder.insertField("NUMPAGES"); 

doc.updateTableLayout(); 

doc.save("D:\\out.doc");

Hi Jens,

Thanks for your reqeust. I would like to clarify a bit regarding horizontally merged cells. 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 has 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 simple code, which demonstrates the described things.

// Create empty document and DocumentBuilder object.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Configure DocumentBuilder

builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);

builder.getCellFormat().getBorders().setColor(Color.BLACK);

// Build table, with simply wide cells.

// First row will contains simply wide cell and in MS Word it will look like merged.

builder.insertCell();

builder.getCellFormat().setWidth(200);

builder.write("This is simply wide cell");

builder.endRow();

// Insert the second row

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.endRow();

builder.endTable();

// Insert few paragraphs between table.

builder.writeln();

builder.writeln();

// Build table, with merged cells.

// First row will contains merged cells.

builder.insertCell();

builder.getCellFormat().setWidth(100);

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

builder.write("This is merged cells");

builder.insertCell();

builder.getCellFormat().setWidth(100);

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

builder.endRow();

// Insert the second row

builder.insertCell();

builder.getCellFormat().setWidth(100);

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

builder.insertCell();

builder.getCellFormat().setWidth(100);

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

builder.endRow();

builder.endTable();

// Save output document

doc.save("C:\\Temp\\out.doc");

However, in your case you call updateTableLayout. UpdateTableLayout usually gives good result. But sometimes with complex tables it can give incorrect result, i.e. table layout can be broken. This is because algorithm of UpdateTableLayout is not the same as MS Word uses to layout tables. So in your case, I would recommend you to use simply wide cells instead of merged cells.

Best regards,

In addition, your code should look like this:

public void testMergeCells() throws Exception {

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertCell();

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

builder.insertCell();

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

builder.endRow();

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);

// Fiels must be inserted inside the cell

builder.insertField("PAGE");

builder.endRow();

doc.save("c:/tmp/aspose/TestMergeCells.doc");

}

Best regards,

The issues you have found earlier (filed as WORDSNET-5857) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)