Table column width when exporting to PDF

Hi
Using Aspose.Words, I create a table. The first column is a name.
When saving the document as Word or HTML, the column automatically autofit the content.
When saving as a PDF, the columns’ width are not the same as I see them in Word.
How to fix that?

even setting “.CellFormat.WrapText = False” does not help

Hi
Thanks for your inquiry. I think that you can use AllowAutoFit and FitText properties. For example see the following code snippet.

builder.RowFormat.AllowAutoFit = false;
builder.CellFormat.FitText = true;
builder.CellFormat.Width = 50;
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 3; j++)
    {
        builder.InsertCell();
        builder.Write("test test test");
    }
    builder.EndRow();
}
builder.EndTable();

Hope this helps.
Best regards,

But what I want is to have the column large enough not to have the text wrapping on multiple lines.
Why is Word and HTML format are displayed correctly without having me to set the column width and not the PDF output?
Also, I don’t want to hardcode the width of the cell, I want it to automatically adjust to the longest text it contains.

Hello!
This is specific to PDF format. Please share the code and attach the sample document which you think is rendered incorrectly. We’ll take a look together with Aspose.Pdf team and make any suggestion to fit your needs.
Regards,

You will find attached a zip file containing the test project and the 3 output files (the pdf does not have correct autosized column width).

Hello!
Thank you for providing additional information. Now I see the matter of the issue well. We’ll consult and reply in this thread. In any case we couldn’t provide any enhancement fast.
Currently the only workaround is hardcoding widths and tuning them to text in cells. Also you can use a very simple empiric estimation of text width. Character width in average is about a half font size. So you can use the formula:
width = fontSize * charCount / 2
It’s very rough but usually works. You can slightly modify it if any text still doesn’t fit in its cell. Note that cell width should include some space for margins.
Regards,

That could work but … Can we set the width for a complete column at once? I have tried but now all my cells are misaligned.

There’s no way to specify width of the whole column. Columns can be split with merged cells or cells which are intentionally “misaligned”. You can use the following approach to set width traversing rows:

Table table = doc.FirstSection.Body.Tables[0];
foreach (Row row in table.Rows)
{
    row.Cells[0].CellFormat.Width = newWidth;
}

Hope this helps.
Best regards,

That is better but not ideal.
I have to loop through the table twice. The first time to find the longest string for each column and the second time to set them.
Anything from the PDF team?

I see you don’t like programming :wink: Why do you think coding two loops (really four, two are nested) is more difficult than one?
The algorithm is pretty fair. Create a floating point array with number of elements equal to number of columns. In the first pass traverse every cell in a row, get text and font from them, calculate width in points and compare with value in the appropriate array element. If it is greater then overwrite it. On the second pass set width to what you have in that array.
I haven’t got any feedback from Aspose.Pdf team and my boss yet. I suspect Aspose.Pdf could resist this improvement. This is evidently not urgent. Even if we start doing anything it could take considerable time. So I won’t promise, sorry.
Have a nice day,

I can use Cell.GetText to retreive the text value of a cell but what about the Font? Bold? Italic?
I am trying to do a generic method that will resize columns of a table and the Font in used in the cell is very important.
So far I have:

For Each row As Aspose.Words.Row In pTable.Rows
For Each objCell As Aspose.Words.Cell In row.Cells
Dim t As String = objCell.GetText
'Dim f as Font = objcell.???
Next
Next

Hi
I think that you can use the following code to achieve this.

foreach (Row myRow in myTable.Rows)
{
    foreach (Cell myCell in myRow.Cells)
    {
        double widht = 0;
        foreach (Run myRun in myCell.GetChildNodes(NodeType.Run, true))
        {
            widht += myRun.Text.Length * myRun.Font.Size / 2;
        }
        myCell.CellFormat.Width = widht;
    }
}

Hope this helps.
Best regards.

I didn’t know the Run class.
With your code, I was able to complete my generic method.
For those interested, here is the code:

Private Sub SetColumnWidth(ByVal pTable As Table)
Dim arrSize(pTable.Rows(0).Cells.Count - 1) As Single
Dim sngWidth As Single
For Each myRow As Aspose.Words.Row In pTable.Rows
Dim intCol As Integer = 0
For Each myCell As Aspose.Words.Cell In myRow.Cells
sngWidth = 0
For Each myRun As Run In myCell.GetChildNodes(NodeType.Run, True)
sngWidth += CSng(myRun.Text.Length * myRun.Font.Size * 0.6)
Next
arrSize(intCol) = Math.Max(sngWidth, arrSize(intCol))
intCol += 1
Next
Next
For Each row As Row In pTable.Rows
For intCol As Integer = 0 To row.Cells.Count - 1
row.Cells(intCol).CellFormat.Width = arrSize(intCol)
Next
Next
End Sub

Hello!
Probably I found what we really needed. Sorry if so late. My colleague and I didn’t know about this method Document.UpdateTableLayout.
Regards,

That works great. Thanks for sharing the info.