Converting DOCX to PDF. Table width distortion

Hi!

The file “test.docx” was edited in a JS editor and saved as “test (after JS editor).docx”.
Then, the file “test (after JS editor).docx” was converted to “test.pdf” using Aspose.Words.
In “test.pdf”, the table width changed, as shown in the screenshot.

It seems the JS editor is behaving incorrectly, but it’s not critical since the file “test (after JS editor).docx” displays correctly in MS Word without distortions. Is it possible to “fix” the document upon loading the file using Aspose.Words?

Project.zip (122.8 KB)

@LogOS2

Why the table width changes only after conversion

When a DOCX file is edited by a third‑party JavaScript editor the internal table layout information can be left in a state that Word can still render correctly (Word “re‑calculates” the layout on the fly).
Aspose.Words, however, uses the layout data stored in the package as‑is. If that data is incomplete or contradictory, the PDF renderer will fall back to its own layout engine, which may produce a different column width.

The good news is that Aspose.Words can re‑calculate the table layout for you before the document is saved to PDF.


Quick fix: Re‑calculate the table layout

// C# example
using Aspose.Words;
using Aspose.Words.Saving;

Document doc = new Document(@"test (after JS editor).docx");

// Force Aspose.Words to rebuild the table layout information
doc.UpdateTableLayout();               // <-- this is the key line

// Optional: you can also force the modern layout algorithm
doc.LayoutOptions.TableLayout = TableLayoutType.Autofit;

// Save as PDF
PdfSaveOptions saveOptions = new PdfSaveOptions();
doc.Save(@"test.pdf", saveOptions);
// Java example
import com.aspose.words.*;

Document doc = new Document("test (after JS editor).docx");

// Re‑calculate the layout of all tables
doc.updateTableLayout();                // <-- key method

// (Optional) use the modern table layout algorithm
doc.getLayoutOptions().setTableLayout(com.aspose.words.TableLayoutType.AUTOFIT);

// Save to PDF
PdfSaveOptions pdfOpts = new PdfSaveOptions();
doc.save("test.pdf", pdfOpts);

What UpdateTableLayout() does

  • Parses the table grid definitions and column widths stored in the DOCX.
  • If some columns are missing explicit widths, Aspose.Words will auto‑fit them according to the content (the same behaviour Word shows when you open the file).
  • The method updates the internal layout model, so the subsequent PDF rendering uses the corrected widths.

When the simple call is not enough

If you still notice a mismatch, try the following additional tweaks:

Setting Description Example
doc.LayoutOptions.TableLayout = TableLayoutType.Fixed Forces the fixed‑width layout (keeps the exact widths defined in the document). doc.getLayoutOptions().setTableLayout(TableLayoutType.FIXED);
doc.LayoutOptions.UseWord2003TableLayout = true Uses the legacy Word 2003 algorithm, which can be more tolerant to malformed tables. doc.getLayoutOptions().setUseWord2003TableLayout(true);
doc.CompatibilityOptions.OptimizeForMsWord = true Enables a set of compatibility options that make the renderer behave like Microsoft Word. doc.getCompatibilityOptions().optimizeForMsWord();
PdfSaveOptionsOptimizeOutput Reduces the chances of PDF‑specific changes blowing up the layout. PdfSaveOptions opts = new PdfSaveOptions(); opts.setOptimizeOutput(true);

How to verify the fix

  1. Load the DOCX with the above code.
  2. Call UpdateTableLayout() (or the extra options you need).
  3. Save to PDF and open the resulting file.
  4. Compare the PDF table widths with the original Word view.

If the PDF now matches the Word rendering, the problem is solved. If you still see a discrepancy, please:

  • Attach the exact DOCX that exhibits the issue (the one you posted in the zip is fine – we can reproduce it).
  • Specify the Aspose.Words version you are using.
  • Indicate the target .NET/Java version (e.g., .NET 6, Java 17).

With that information we can dig deeper and suggest the precise combination of layout options.


References


TL;DR

Document doc = new Document("test (after JS editor).docx");
doc.UpdateTableLayout();   // re‑calculates table widths
doc.Save("test.pdf", new PdfSaveOptions());

Running the code above (or the equivalent Java snippet) should eliminate the table‑width distortion you see after conversion. If the problem persists, let us know the points listed under How to verify the fix and we’ll help you further.

Thank you. :robot:It works.

@LogOS2 Through UpdatTableLayout works in your case, this method is deprecated. UpdateTableLayout method was the first attempt of table grid calculation algorithm implementation. Currently another implementation is used internally by Aspose.Words, so normally there is no need to call additional methods to calculate table layout. So please use UpdatTableLayout carefully. We will look what is wrong with table rendering.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28616

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.