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

Free Support Forum - aspose.com

Table's column width is different when document is saved to DOCX and PDF using Java

Hello,

I have noticed differences when saving as DOCX and PDF. The column widths differ in the output formats. The column widths should fit to the content, this does not work in PDF. The bug occurs when cells are merged.
Tested with Aspose.Words for Java 18.7 and 20.3.

Steps to Reproduce:
Using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Font font = builder.getFont();
font.setSize(9);
font.setName(“Arial”);

builder.insertCell();
builder.write(“Part Number”);

builder.insertCell();
builder.write(“Description”);

builder.insertCell();
builder.write(“Qty”);

builder.insertCell();
builder.write(“Unit List Price”);

builder.insertCell();
builder.write(“Unit Contract Price”);

builder.insertCell();
builder.write(“Unit\nDiscounted\nPrice”);

builder.insertCell();
builder.write(“Extended Discounted Price”);

builder.endRow();

builder.insertCell();
builder.write("");

// Whenever a cell is merged, the bug occurs
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("");

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

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

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

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

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

// the following has no effect
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);

doc.save(".\test.docx");
doc.save(".\test.pdf");

For example, the text in the first column is wrapped in the PDF.

Maybe the same problem as in (I cannot open the example to verify it):

Best regards
Ingo

@gutzeit

Please call Document.updateTableLayout method as shown below to get the desired output.

doc.save(MyDir + "test.docx");
doc.updateTableLayout();
doc.save(MyDir + "test.pdf");

Hello @tahir.manzoor,

thanks for the quick reply. Basically your solution works.
We modify Word documents, sometimes also tables are modified.
If the method is only called before saving the document, simple documents (contain only one table) are displayed correctly as PDF. If there are several tables that are modified, it does not always work (unfortunately I have not yet managed to create a simple example). Then the update must be called after each table modification. Is this a known behavior?
The updateTableLayout() method probably includes all tables contained in the document, this could cause performance problems. Is it possible to update only the modified table?

Best regards
Ingo

@gutzeit

You do not normally need to call Document.UpdateTableLayout 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.

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output file that shows the undesired behavior.
  • Please attach the expected output file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hello @tahir.manzoor,

I managed to find the reason for the unexpected behavior and have found a solution for me.
I have discovered that in some cases updating the tables does not work if the fields are updated before.

The following works in any cases:

document.updateTableLayout();
document.updateFields();

Switching the lines does not work:

document.updateFields();
document.updateTableLayout();

If I have time I will try to create an example for further investigation.
For me the problem is solved. Thanks a lot for your support.

Best regards
Ingo Gutzeit

@gutzeit

It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.