Update Table width when saving to PDF

Hello Aspose-Team,

we have a problem with table widths in the following scenario:

  1. Load a DOCX document which contains a table with relative preferred size, e.g. 100%, and columns with different preferred sizes.
  2. Change the margins of the document, such that the effective width of the document becomes larger.
  3. Save the document to PDF.

In the resulting PDF, the table has not a width of 100% of the document, but it is roughly as wide as it should have been with the old margin.

The problem does not occur, when we first save the document as a DOCX and then load that DOCX and save it to PDF. But that is a waste of resources, and it would be better if there was a function or SaveOption for that.

@orgavision

It sounds like you’re encountering an issue with table widths not adjusting correctly when saving a DOCX document to PDF after changing the document margins. This can happen because the table’s width is calculated based on the original margins when the document is loaded.

Cause: The issue arises when the table’s width does not automatically update to reflect the new margins set in the document.

Solution: To resolve this, you can follow these steps:

  1. Update Table Width: After changing the margins, explicitly set the table width to 100% before saving the document to PDF. This ensures that the table recalculates its width based on the new margins.

  2. Use SaveOptions: When saving the document to PDF, consider using the PdfSaveOptions class to specify any additional settings that might help with layout adjustments.

Here’s a minimal code example to illustrate how you might implement this:

Document doc = new Document("input.docx");

// Change margins
doc.FirstSection.PageSetup.LeftMargin = 50;
doc.FirstSection.PageSetup.RightMargin = 50;

// Find the table and set its width
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.PreferredWidth = PreferredWidth.FromPercent(100);
}

// Save to PDF
doc.Save("output.pdf", SaveFormat.Pdf);

Next Steps: Implement the above code in your project and test it to see if the table width adjusts correctly when saving to PDF. If you continue to experience issues, please provide additional details about your setup or any error messages you encounter.

This approach should help ensure that the table width reflects the new document margins when saving to PDF. If you have any further questions or need additional assistance, feel free to ask!

@orgavision Could you please attach your sample input and problematic output documents along with simple code that will allow us to reproduce the problem? We will check the issue and provide you more information.

I have to correct myself: There are cases, where it does not help to save the document as DOCX before saving it as PDF.

An example input is the document:
DEV-33461-tables.docx (18.4 KB)

We load that document, and replace header, footer and all margins with the ones from this document:
DEV-33461_template-landscape.docx (17.1 KB)

The expected output would be (appoximately; the cell widths might not be exactly correct in my manual fix):
DEV-33461-expected.pdf (39.1 KB)

But instead, we get this:
junit1143669280821853786.tmp.pdf (39.1 KB)

The second table has the correct width, but the first table has not updated its width to the changed dimensions of the page.

@orgavision
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-28563

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.