Free Support Forum - aspose.com

Word table with more than 63 columns

Hi Team,


I’m using AsposeWordsForJava 15.6.

When I generate a table with more than 63 columns, here are different results.

Scenario 1: Table with one row and 64 cells
Table is corrupted

Scenario 2: Table with one row and 64 cells
Footer with Page Number field
Table is corrupted

Scenario 3: Table with one row and 64 cells
Footer with Page Number field
Update fields
Table is good

I know that MS word doesn’t allow more than 63 cells in a row. But could you explain the above behavior.

I’ve attached the code and the documents for the above scenarios.

Thanks,
Kumar

import java.util.UUID;

import com.aspose.words.BuildVersionInfo;
import com.aspose.words.Cell;
import com.aspose.words.CellMerge;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.Node;

public class TestTableCorruptionMP
{
public static void main(String[] args) throws Exception
{
TestAsposeUtils.setupLicense(TestAsposeUtils.LICENSE_FILE_PATH);

System.out.println(“Create document with no update file field no footer field”);
createDocument(false, false, “noUpdateField_noFooterField”);

System.out.println();
System.out.println(“Create document with no update field and footer field”);
createDocument(false, true, “noUpdateField_yesFooterField”);

System.out.println();
System.out.println(“Create document with update field and footer field”);
createDocument(true, true, “yesUpdateField_yesFooterField”);

System.out.println(“done”);
}

private static void createDocument(boolean updateFields, boolean writeFooterPageNumber, String filename)
throws Exception
{
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

Node curNodeInDocument = docBuilder.getCurrentNode();
if (curNodeInDocument == null)
{
curNodeInDocument = docBuilder.getCurrentParagraph();
}

if (writeFooterPageNumber)
{
// write footer
docBuilder.moveTo(doc.getLastSection().getBody().getLastParagraph());
docBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);

docBuilder.insertField(“PAGE”, “”); //$NON-NLS-1$ //$NON-NLS-2$

if (curNodeInDocument != null)
{
docBuilder.moveTo(curNodeInDocument);
curNodeInDocument = null;
}
else
{
docBuilder.moveTo(doc.getLastSection().getBody().getLastParagraph());
}
}

buildTable(docBuilder);

if (updateFields)
{
doc.updateFields();
}
String baseFolder = System.getProperty(“java.io.tmpdir”);
String unique = UUID.randomUUID().toString().replaceAll(" ", “");
String docpath = baseFolder + "test” + unique + “" + filename + "” + BuildVersionInfo.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$
doc.save(docpath + “.doc”); //$NON-NLS-1$
System.out.println(docpath + “.doc”);
}

private static void buildTable(DocumentBuilder docBuilder) throws Exception
{
docBuilder.insertCell();
docBuilder.write(“Test 64 cells”);
mergeCell(docBuilder, “column”, 63);

docBuilder.insertCell();
mergeCell(docBuilder, “column”, 1);
docBuilder.endRow();

docBuilder.endTable();
}

private static void mergeCell(DocumentBuilder docBuilder, String type, int span) throws Exception
{
for (int i = 0; i < span; i++)
{
if (“column”.equals(type))
{
if (i == 0)
{
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
docBuilder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
}
else
{
Cell cell = docBuilder.insertCell();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
docBuilder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
}
}
}
}
}

Hi Kumar,


Thanks for your inquiry. Please note that the cell count per row must not exceed 63 i.e. document can have problems when opening with Microsoft Word. However, saving to DOCX format shouldn’t cause this problem. You can use UpdatePageLayout method before saving to DOC format.

UpdatePageLayout method needs to build layout of the document and for this it needs to layout tables and for this “merged cells” in these tables must be normalized. Aspose.Words converts horizontally merged cells into one normal cell during this normalization process. That is why Aspose.Words always writes “w:gridSpan” attribute in document.xml against normalized cells. Also, please note that the Document.Save method doesn’t apply normalization when saving to DOC and RTF formats. It means, instead of DOC, if you save your document to DOCX format (without calling UpdatePageLayout), you can still be able to open this document with Microsoft Word. I hope, this helps.

Best regards,

Hi Awais,


I understand that the cell count per row should not exceed 63. However, Aspose APIs behaves differently as I mentioned in the scenarios above. Please let me know why is that.

We’ve bigger issue with updatePageLayout and updateTableLayout - http://www.aspose.com/community/forums/thread/518748.aspx

Thanks,
Kumar

Additionally, updatePageLayout is a time-consuming operation and hence extra time would be incurred.

Hi Kumar,


Thanks for your inquiry. I think, in this case, you should save in DOCX format (without calling UpdatePageLayout) instead of DOC. If we can help you with anything else, please feel free to ask.

Best regards,

Hi Awais,


If I’m not able to create more than 63 cells from MS Word, how is Aspose API creating it? for both doc and docx format…

Thanks,
Kumar

Hi Kumar,


Thanks for your inquiry. The restriction is imposed by MS Word; however, with OpenOffice Writer (I checked version 4.0.1), I was able to insert more than 63 cells in a Row.

Best regards,

If this restriction is imposed by MS Word, then how does it open a word (doc and docx) document with more than 63 cells? Is it a hacky solution from Aspose?

Hi Kumar,


Thanks for your inquiry. Well, if you insert more than 63 cells in document using OpenOffice Writer and save it to ODT format, after that MS Word 2013 can open this ODT without any problem yet it does not allow insertion of more than 63 cells in a new Table in the same ODT. So, this restriction seems to be applied by MS Word only.

Best regards,