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

Free Support Forum - aspose.com

Cell merge from readed document

Hi support,

Please check the following code :

@Test

public void testCellProperties() throws Exception

{

// Create an empty document. It contains one empty paragraph.

Document doc = new Document(“C:\Temp\myDocument.doc”);

for (Section section : doc.getSections())

{

Body body = section.getBody();

if ((body != null) && (body.getChildNodes() != null))

{

Node[] nodes = body.getChildNodes().toArray();

handleNodeChildNodes(nodes, " ");

}

}

}

private void handleNodeChildNodes(Node[] nodeCollection, String offset)
{
for (Node node : nodeCollection)
{
System.out.println(offset + "node start " + node.toString());
if(node.getNodeType() == NodeType.CELL)
{
Cell cell = (Cell) node;
if(cell != null)
{
try
{
System.out.println(offset + (cell.getCellFormat().getHorizontalMerge() == CellMerge.FIRST));
System.out.println(offset + (cell.getCellFormat().getHorizontalMerge() == CellMerge.PREVIOUS));
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

if (node.isComposite())
{
CompositeNode compositeNode = (CompositeNode) node;
if (compositeNode != null)
{
Node[] childNodes = compositeNode.getChildNodes().toArray();
handleNodeChildNodes(childNodes, offset + " ");
}
}
else if(node.getNodeType() == NodeType.RUN)
{
Run run = (Run) node;
if(run != null)
{
System.out.println(offset + run.getText());
}
}

System.out.println(offset + "node end " + node.toString());
}
}

Please note that I get two cells from the first row, although when a merged group of cells must be written in a document, all the cells must be written. Note also that the merged properties have not the expected values.

Please advice,
Milan

Hi Milan,

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

Thanks for your inquiry. Here is what occurs. 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.

Best regards.

Hi Alexey,

I’ve got the point and noticed with a short VBA your affirmations.
But how can I find the information about merge properties through code ? If a have a table with two rows, two cells on the 1st row, and three on the 2nd row, how can I find which one from the cells in 1st row was merged and now occupies the same space as two cells from the row underneath ?
Note that if I write a table in a new document, and set horizontal merge values for a cell, and then if I try to get that value, I get it correctly, but if I read&parse a document with a table that has merged cells, I do not obtain valid information about merge values.

Thanks,
Milan

Hi Milan,

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

Thanks for your request. Actually, in this case you do not need to know the cell was merged or not. I think it is enough to know width of each cell and number of cells in each row.

Width=100

Width= 100+100 = 200

Width=100

Width=100

Width=100

Best regards,

Hi Alexey,

Yes, a decision about merged cells can be made by taken in consider the cell widths. Let’s suppose that your table has 100 rows : 99 are similar as the first one, and last one is like the 2nd one in your table. I can figure out that 2nd cell in 1st row is a “merged” cell, only when I find all the information about cells in all rows underneath, more exactly, after I find cells properties in the last row. If the row number 100 was missing, I would conclude that there is no cell with merge properties within the table. So this is not a very straightforward solution.

I’m building a table considering the table data I read. Forcing the cell width to have the values I get is not an option: I would have cells widths explicitly set in the document I create, comparing with the document I read, where no cell width is explicitly set.

So if there is a way of getting merge information for a cell, from the “Cell” object, it would be great.

Please advice,
Milan

Hi Milan,

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

Thank you for additional information. As I already mentioned earlier, this by MS Word design. Sometimes MS Word writes merged cells as actually merged cells, but sometimes it writes such cells as simply wide. So I cannot suggest you any simple way to detect whether cell is horizontally merged or not. Both scenarios in MS Word are correct.

For example, see the following code.

// Create document and DocumentBuilder.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

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

// Insert table with merged cells.

builder.getCellFormat().setWidth(100);

builder.insertCell();

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

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

builder.insertCell();

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

builder.endRow();

builder.insertCell();

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

builder.insertCell();

builder.endRow();

builder.endTable();

builder.writeln();

// Insert table with simply wide cell.

builder.insertCell();

builder.getCellFormat().setWidth(200);

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

builder.endRow();

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.insertCell();

builder.endRow();

builder.endTable();

// Save output document.

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

As you can see, both of tables look the same in MS Word, but in one case the first row contains two merged cells, and in other case only one simply wide cell.

Best regards.

Hi Alexey,

Is there any way I can find if the user has explicitly set the with for a certain cell ? The getWidth() method measures the cell and returns a value no matter if I set the width for that cell or not.

Thanks,
Milan

Hi Milan,

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

Thanks for your request. I suppose, what you are asking is “Preferred Cell Width” in MS Word. We will consider exposing this property in CellFormat.

Best regards.

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


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