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

Free Support Forum - aspose.com

Automatically Resize Columns in Table during Converting DOCX to PDF

Dear Aspose Support

I have a single page Word Document. When i save it as PDF with Aspose, it adds another empty page.
While debugging, i saw that the Document instance has a PageCount of 2 instead of 1.

This is the Word file: AdditionalPage.zip (7.2 KB)

This is the code ive used:

Document document = new Document(@"C:\TEMP\AdditionalPage.docx");
//document.PageCount is 2 for some reason
using (var fs = new FileStream($@"C:\TEMP\AdditionalPage.pdf", FileMode.Create))
{
    document.Save(fs, SaveFormat.Pdf);
}

Aspose.Words version: 19.3

@emptyveee,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-19641. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@emptyveee,

Regarding WORDSNET-19641, it is to update you that the issue occurs because an incorrect Table Grid data saved in the document is not currently re-calculated by Aspose.Words correctly.

The source document is generated by Aspose.Words. There is a large table on page 1 that has incorrect column widths in Aspose.Words layout. Because of incorrect column widths, some paragraph take more lines in Aspose.Words layout, and some contents flow to page 2. The issue is to be addressed by table grid calculation algorithm being implemented per WORDSNET-832. Currently the algorithm does not support tables with cells having vertical text direction.

We are postponing the implementation of WORDSNET-19641 until the problematic table is supported. In the meantime, while you are waiting for a fix, you may please re-save the document via MS Word as a workaround. Hope, this helps.

Is there a mistake on my side of generating the table that causes these incorrect column sizes?
Could you give me a clue as to how the error might arise? I could then try fixing my table generation code.

@emptyveee,

Please open your ‘’ document with MS Word and then re-save (Save As) to DOCX format. This will fix the table structure. And when you process the following re-saved document with Aspose.Words, the PDF output will be correct. Hope, this helps.

Rest assured, we will inform you via this thread as soon as this issue will be resolved in future. We apologize for any inconvenience.

@emptyveee,

There does not seem to be a mistake in your code. The problem is that Aspose.Words currently relies on table column width data normally stored in the document by MS Word. For generated tables, these data are missing. We are working on reproducing MS Word column width calculation from table/cell properties, but it is not an easy task because MS Word algorithm is not public and there are too many factors affecting the calculation. Column width re-calculation for auto-fit tables with vertical text direction is not yet supported.

You may try the following workaround ideas if you are willing to modify your code:

  • Set Table.AllowAutoFit to false and set CellFormat.PreferredWidth explicitly. This way the column widths will not depend on cell contents. Aspose.Words column width calculation for the generated table should match MS Word. The width for the cells will not change dynamically in MS Word when adding/removing contents however. Also cell preferred widths need to be set explicitly in your code. They may be set as a percent of the table width.
  • Generate the table without the first row that has vertical text direction. Call Document.UpdatePageLayout() so that Aspose.Words updates the generated cell widths. Add the first row with vertical text after that. The column width calculated earlier should remain unchanged. The approach may require some experimenting. The below snippet manipulates the document attached to this issue and produces a satisfactory layout: attachment: 19641wa.pdf (20.4 KB).

Code:

Body section = doc.FirstSection.Body;
Table table = section.Tables[0];

// Split the table.
Table tmpTable = new Table(doc);
Paragraph dummy = new Paragraph(doc);
section.InsertBefore(tmpTable, table);
section.InsertAfter(dummy, tmpTable);
tmpTable.AppendChild(table.FirstRow);

// Update the layout model, it will update column widths for the supported tables.
doc.UpdatePageLayout();

// Move the first row back.
table.PrependChild(tmpTable.FirstRow);
tmpTable.Remove();
dummy.Remove();

// Update page layout after document model manipulation.
doc.UpdatePageLayout();
doc.Save("19641wa.pdf");
2 Likes