Table autofit doesn't work properly when using a paragraph

Recently we came across a problem with table, autofit and paragraphs in a cell

So if we add into a table cell a paragraph in which we inserted 3 different texts of different length, then the table cell column width will be the width of the first text. It should be the width of the largest text.

Attached you can find the reproducer and a sample document generated by this.

public static void ParagraphIssue()
{
    // ExStart:MarginsOrPadding
    // The path to the documents directory.
    var dataDir = “”;

    // Instntiate the Document object by calling its empty constructor
    var doc = new Document();
    var page = doc.Pages.Add();
    // Instantiate a table object
    var table = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToContent };
    var row1 = table.Rows.Add();
    var cell = row1.Cells.Add();
    var text1 = new TextFragment(“hello”);
    var text2 = new TextFragment(“goodbye”);
    var text3 = new TextFragment(“hamana”);
    // Row1.Cells.Add(“col3 with large text string to be placed inside cell”);
    cell.Paragraphs.Add(text1);
    cell.Paragraphs.Add(text2);
    cell.Paragraphs.Add(text3);

    row1.Cells.Add(“Really really really really long line”);
    row1.Cells.Add(“Another Really really long line”);
    page.Paragraphs.Add(table);

    //Create a Graph object
    GraphInfo borderGraph = new GraphInfo { LineWidth = 1 };
    //Set table border using customized BorderInfo object
    table.DefaultCellBorder = new BorderInfo(BorderSide.All, borderGraph);
    dataDir = dataDir + “ParagraphIssue_.pdf”;
    // Save the Pdf
    doc.Save(dataDir);
    public static void ParagraphIssue()
    {
        // ExStart:MarginsOrPadding
        // The path to the documents directory.
        var dataDir = “”;

        // Instntiate the Document object by calling its empty constructor
        var doc = new Document();
        var page = doc.Pages.Add();
        // Instantiate a table object
        var table = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToContent };
        var row1 = table.Rows.Add();
        var cell = row1.Cells.Add();
        var text1 = new TextFragment(“hello”);
        var text2 = new TextFragment(“goodbye”);
        var text3 = new TextFragment(“hamana”);
        cell.Paragraphs.Add(text1);
        cell.Paragraphs.Add(text2);
        cell.Paragraphs.Add(text3);

        row1.Cells.Add(“Really really really really long line”);
        row1.Cells.Add(“Another Really really long line”);
        page.Paragraphs.Add(table);

        //Create a Graph object
        GraphInfo borderGraph = new GraphInfo { LineWidth = 1 };
        //Set table border using customized BorderInfo object
        table.DefaultCellBorder = new BorderInfo(BorderSide.All, borderGraph);
        dataDir = dataDir + “ParagraphIssue_.pdf”;
        // Save the Pdf
        doc.Save(dataDir);
        // ExEnd:MarginsOrPadding
        Console.WriteLine("\nCell and table border width setup successfully.\nFile saved at " + dataDir);
    }
}

Hi Daniel,

Thanks for contacting support.

dfactset:

So if we add into a table cell a paragraph in which we inserted 3 different texts of different length, then the table cell column width will be the width of the first text. It should be the width of the largest text.

As you have set column adjustment to AutoFitToContent, so the cell width was automatically adjusting according to the content width. In order to get all column widths equal, you can set ColumnAdjustment property to AutoFitToWindow OR you can also set custom width to each column in the table by using ColumnWidths property of the table.

Please check following code snippet where I have set ColumnAdjustment to AutoFitToWindow and in the generated output which is attached, all cells have equal width.

var doc = new Document();
var page = doc.Pages.Add();
var table = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow };
var row1 = table.Rows.Add();
var cell = row1.Cells.Add();
var text1 = new TextFragment("hello");
var text2 = new TextFragment("goodbye");
var text3 = new TextFragment("hamana");
cell.Paragraphs.Add(text1);
cell.Paragraphs.Add(text2);
cell.Paragraphs.Add(text3);
row1.Cells.Add("Really really really really long line");
row1.Cells.Add("Another Really really long line");
page.Paragraphs.Add(table);
GraphInfo borderGraph = new GraphInfo { LineWidth = 1 };
table.DefaultCellBorder = new BorderInfo(BorderSide.All, borderGraph);
dataDir = dataDir + "ParagraphIssue_.pdf";
doc.Save(dataDir);

To set custom width, you can do like following.

```vb
var table = new Table { ColumnAdjustment = ColumnAdjustment.Customized };
table.ColumnWidths = "50 150 180";

An output with custom widths, is also attached for your reference. In case of any further assistance, please feel free to contact us.

Best Regards,

We are using ColumnAdjustment.Customized in some of the cases, but in the present case we don’t want to AutoFitToWindow, or use ColumnAdjustemnt.Customized, as we want the column widths to be adjusted to the content they have.

If the content of a column has new lines, we want that column width to be set to the width of the longest line in the column.

Hi Daniel,


Thanks for writing back.

I have tested the scenario again by setting column adjustment to AutoFitToContent and observed that the column width was not adjusting according to the largest text in it. Therefore, for the sake of correction, I have logged an issue as PDFNET-42739 in our issue tracking system. We will further investigate the reasons behind this issue and keep you posted on the status of its resolution. Please be patient and spare us little time.

We are sorry for the inconvenience.


Best Regards,

Can I get an update on it?

@dfactset

We are afraid that we could not resolve the issue due to other pending tasks in the queue. Nevertheless, your concerns have been recorded and we will surely inform you once the ticket is resolved. We apologize for the inconvenience.