Free Support Forum - aspose.com

Table is cut off

Hi Team,


I’m using Aspose.WordsForJava 15.6.

I generate a table with horizontal merge, setting width etc, the table is cut off on the right part of the page.

However, if I use a blank new document (empty.doc) from MS Word 2010 as a stylesheet, the table is not cut off.

Does Aspose use any default stylesheet? If yes, what version of the MS Word document is it? Is there any what to mention to use latest version as default stylesheet?

// Document doc = new Document(“c:\empty.doc”); // table is good
Document doc = new Document(); // table is cut off

Thanks,
Kumar

public class TestTableCutOff
{

public static void main(String[] args) throws Exception
{
TestAsposeUtils.setupLicense(TestAsposeUtils.LICENSE_FILE_PATH);
// Document doc = new
// Document(“D:\ks\work\RPE_2012\Escalations\34607-dng-table-issue\cutoff-table\empty.doc”);
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
buildTable(docBuilder);
save(doc);
}

private static void buildTable(DocumentBuilder docBuilder) throws Exception
{
Table table = docBuilder.startTable();

// row 1
Cell cell = docBuilder.insertCell();
docBuilder.write("[COMPANY LOGO]");
mergeCell(docBuilder, “column”, 16, convertCellWidth(“305px”));
docBuilder.moveTo(cell.getFirstParagraph());

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 16, convertCellWidth(“333px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write("[PO TEXT + PO Number + [Change]");
setCellWidth(cell, convertCellWidth(“333px”));

cell = docBuilder.insertCell();

docBuilder.endRow();

// row 2
cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 16, convertCellWidth(“305px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Proveedor:”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 12, convertCellWidth(“168px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Destinatario de factura:”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 4, convertCellWidth(“165px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Destinatario de mercaderia”);

cell = docBuilder.insertCell();

docBuilder.endRow();

// row 3
cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 9, convertCellWidth(“240px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Fecha de orden de compra: 09.04.2013”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 16, convertCellWidth(“197px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Incoterm: FBO”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 7, convertCellWidth(“201px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Comprador: Mario”);

cell = docBuilder.insertCell();

docBuilder.endRow();

// row 4
cell = docBuilder.insertCell();
docBuilder.write(“10”);
setCellWidth(cell, convertCellWidth(“48px”));

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“41px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“C”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“122px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10004545”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 6, convertCellWidth(“70px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 5, convertCellWidth(“41px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“EA”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“64px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10,00 [Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“47px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“1000”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 6, convertCellWidth(“76px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10.000,00[Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 2, convertCellWidth(“128px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“12.300,00[Curr]”);

cell = docBuilder.insertCell();
docBuilder.write("");

docBuilder.endRow();

// row 5
cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 32, convertCellWidth(“638px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write("");

cell = docBuilder.insertCell();
docBuilder.write("");

docBuilder.endRow();

// row 6
cell = docBuilder.insertCell();
docBuilder.write(“10”);
setCellWidth(cell, convertCellWidth(“48px”));

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 2, convertCellWidth(“41px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“C”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 5, convertCellWidth(“122px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10004545”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 6, convertCellWidth(“70px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 5, convertCellWidth(“41px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“EA”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“64px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10,00 [Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 5, convertCellWidth(“47px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“1000”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 4, convertCellWidth(“76px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10.000,00[Curr]”);

cell = docBuilder.insertCell();
docBuilder.write(“12.300,00[Curr]”);
setCellWidth(cell, convertCellWidth(“128px”));

cell = docBuilder.insertCell();
docBuilder.write("");
docBuilder.endRow();

// row 7
cell = docBuilder.insertCell();
docBuilder.write(“10”);
setCellWidth(cell, convertCellWidth(“48px”));

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“41px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“C”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“122px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10004545”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 6, convertCellWidth(“70px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 5, convertCellWidth(“41px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“EA”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“64px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10,00 [Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 3, convertCellWidth(“47px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“1000”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 6, convertCellWidth(“76px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10.000,00[Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 2, convertCellWidth(“128px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“12.300,00[Curr]”);

cell = docBuilder.insertCell();
docBuilder.write("");
docBuilder.endRow();

// row 8
cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 22, convertCellWidth(“421px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10.000,00[Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 10, convertCellWidth(“217px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“12.300,00[Curr]”);

cell = docBuilder.insertCell();
docBuilder.write("");
docBuilder.endRow();

// row 9
cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 6, convertCellWidth(“153px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“10.000,00[Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 11, convertCellWidth(“158px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“12.300,00[Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 9, convertCellWidth(“141px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“12.300,00[Curr]”);

cell = docBuilder.insertCell();
mergeCell(docBuilder, “column”, 7, convertCellWidth(“187px”));
docBuilder.moveTo(cell.getFirstParagraph());
docBuilder.write(“Revision Number”);
docBuilder.endRow();

table.setPreferredWidth(PreferredWidth.AUTO);
table.setAllowAutoFit(true);
docBuilder.endTable();
}

private static void mergeCell(DocumentBuilder docBuilder, String type, int span, double width) throws Exception
{
Cell mergeCell = null;
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
{
mergeCell = docBuilder.insertCell();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
docBuilder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
}
}

if (“row”.equals(type))
{
if (i == 0)
{
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
docBuilder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
}
else
{
Cell cell = docBuilder.insertCell();
docBuilder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
docBuilder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
setCellWidth(cell, width);
}
}
}
setCellWidth(mergeCell, width);
}

private static void setCellWidth(Cell cell, double width)
{
cell.getCellFormat().setWidth(width);
cell.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(width));
}

private static double convertCellWidth(String value)
{
value = value.replace(“px”, “”); //$NON-NLS-1$//$NON-NLS-2$
return Double.parseDouble(value);
}

public static void save(Document doc) throws Exception
{
String baseFolder = System.getProperty(“java.io.tmpdir”);
String unique = UUID.randomUUID().toString().replaceAll(" ", “");
String docpath = baseFolder + "test” + unique + “_” + BuildVersionInfo.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$
doc.save(docpath + “.doc”); //$NON-NLS-1$
System.out.println(docpath + “.doc”);
}
}

The issue is not specific to any merge. You could replicate it with much simplified code like below.

private static void buildTable(DocumentBuilder docBuilder) throws Exception
{
Table table = docBuilder.startTable();
docBuilder.insertCell();
docBuilder.write("loooooooooooooooooooooonword");
docBuilder.insertCell();
docBuilder.write("loooooooooooooooooooooonword");
docBuilder.insertCell();
docBuilder.write("loooooooooooooooooooooonword");
docBuilder.insertCell();
docBuilder.write("loooooooooooooooooooooonword");
docBuilder.insertCell();
docBuilder.write("loooooooooooooooooooooonword");
docBuilder.endRow();

table.setPreferredWidth(PreferredWidth.AUTO);
table.setAllowAutoFit(true);
docBuilder.endTable();
}
We found a legacy option called "Allow tables to extend into margins" enabled in old word documents. All the new documents had it disabled. Disabling it resulted in a proper output with all the tables fitting the page.

The property is available through File->options->Advanced->Compatibility options ( despite it's location this is a property for the document).

I think Aspose still uses the old stylesheet options as default.

Is there any way to set the above property through Aspose API?
doc.getCompatibilityOptions().setXXX

Thanks,
Kumar

Hi Kumar,

You can use the following code to save as new document format and disable this option by default.

OoxmlSaveOptions options= new OoxmlSaveOptions();

options.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);

doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010);<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

doc.save(“Out.docx”,options);

Please feel free to contact us in case you have further comments or questions.

Best Regards,

Hi Muhammad,


The issue is for doc format and not docx. Is there a similar API for doc format?

Thanks,
Kumar

Hi Kumar,

Same API is available for DOC format also. You can use this API without additional save options e.g.

doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010);

doc.save("Out.doc");

Best Regards,

Hi Muhammad,


Thanks.

What is the below for? and would this be applicable only for docx?

OoxmlSaveOptions options= new OoxmlSaveOptions();

options.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);


Additionally, is the below applicable for PDF as well or only for Word?
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010);

Thanks,
Kumar

Hi Kumar,

OoxmlSaveOptions can be used when saving to DOCX, DOCM, DOTX, DOTM or FlatOpc formats. As far as getCompatibilityOptions().optimizeFor() method is concerned, yes it can be used for PDF also as this is part of Document object.

Best Regards,