How to format table content

Hi All,

How to format content inside a table, so that the text will NOT wrap together.

Below (every word uses one line, even it shows otherwise on this web page) has to format:

Title My title
Undertaken By undertaken by cUY
Date Undertaken 07 April 2011
Reported to Audit RTA
Reported to Committee RTC
Audit Comment My Audit Comment

like this:

Title My title
Undertaken By undertaken by cUY
Date Undertaken 07 April 2011
Reported to Audit RTA
Reported to Committee RTC
Audit Comment My Audit Comment

Thanks in advance!!

Hi

Thanks for your request. But both tables look the same. Could you please attach sample documents that will demonstrate the problem and provide sample code that will allow to reproduce it?

Best regards,

Please find the docoment and source code in the attachment.

Please keep me informed on the solution!

Thanks in advance!

Regards

Steve

Hi Steve,

Thanks for your request. I think, in your case, you can try using code like the following:

protected void CreateTable(Dictionary<string, string> rows, DocumentBuilder builder)
{
    // Since there should be only two columns in our table,
    // we can calculate width of column ad pageWidth/2.
    PageSetup ps = builder.PageSetup;
    double columnWidth = (ps.PageWidth - ps.LeftMargin - ps.RightMargin) / 2;
    SetContentFormat(builder);
    builder.StartTable();
    builder.RowFormat.AllowBreakAcrossPages = false;
    // Set default width of table cell.
    builder.CellFormat.Width = columnWidth;
    foreach (KeyValuePair<string, string> row in rows)
    {
        builder.InsertCell();
        builder.Write(row.Key);
        builder.InsertCell();
        builder.Write(row.Value);
        builder.EndRow();
    }
    builder.EndTable();
}

protected void SetContentFormat(DocumentBuilder builder)
{
    builder.Font.Size = 11;
    builder.Font.Bold = false;
    builder.Font.Name = "Arial";
    builder.Font.Color = System.Drawing.Color.Black;
}

Hope this helps.

Best regards,

Hi,

Thank you for your advice. The previous solution is fine. But I want to make column width just fit the content length of that column, with no extra space. The previous solution will create extra space if the content is short.

Any idea would be very much appreciated!

Regards

Steve

Hi Steve,

Thanks for your request. What you are asking for is “Auto-fit to Content” option in MS Word. Unfortunately, currently there is no way to set this option using Aspose.Words. We plan to support this in the one of future releases. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

At the moment, you can only specify fixed width of each cell in the table.

Best regards,

Is there code sample like this:

https://docs.aspose.com/words/net/working-with-tables/

but I want to to fit the column width to cell content length.

Both render the document and convert document to doc, docx, pdf, rtf.

Hi Steve,

Thanks for your request. But as I mentioned earlier there is no way to achieve this at the moment. Of course, you can try measuring text in cells and calculate width of columns, but such technique will require more efforts from your side.

Best regards,

Given the current bug with Aspose Word .NET, are you aware of any sample code available that you can provide?

Hi

Thanks for your request. This is not a bug – this is a non-implemented feature. This feature will be available in 2-3 months.

You can create such code yourself, for example, you can assume that width of character in table cell equals a half of font size. Using such formula, you can approximately calculate width of text inside cell. Knowing width of text, you can change width of cell to fit content.

Best regards,

How to get the width of text within a cell (table.Rows[j].Cells[i].GetText().Length)? Below is the code, which does NOT work:

protected static void FitColumnWidth(Table table)
{
    int columnCount = 0;
    if (table.Rows.Count < 1) return;
    columnCount = table.Rows[0].Cells.Count;
    if (columnCount < 1) return;
    double[] columnWidth = new double[columnCount];
    for (int i = 0; i < columnCount; i++)
    {
        for (int j = 0; j < table.Rows.Count; j++)
        {
            double cellWidth = table.Rows[j].Cells[i].GetText().Length;
            columnWidth[i] = Math.Max(columnWidth[i], cellWidth);
        }
    }
    for (int i = 0; i < table.Rows.Count; i++)
    {
        for (int j = 0; j < table.Rows[i].Cells.Count; j++)
        {
            table.Rows[i].Cells[j].CellFormat.Width = columnWidth[j];
        }
    }
}

Hi Steve,

Thanks for your request. As I already mentioned in my previous thread you can approximately calculate width of each character as half of font size. Also, you can try using System.Drawing to measure text. For instance, the following code approximately calculate width of text inside cell:

/// 
/// Method calculate an aproximate width of content inside cell.
/// 
private double CalculateWidhtOfCellContent(Cell cell)
{
    double width = 0;
    foreach (Paragraph paragraph in cell.Paragraphs)
    {
        double intermWidth = 0;
        foreach (Run run in paragraph.Runs)
        {
            using (Bitmap bmp = new Bitmap(1, 1))
            {
                bmp.SetResolution(96, 96);
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    using (System.Drawing.Font f = new System.Drawing.Font(run.Font.Name, (float)run.Font.Size))
                    {
                        SizeF textSize = g.MeasureString(run.Text, f);
                        intermWidth += textSize.Width;
                    }
                }
            }
        }
        if (intermWidth > width)
            width = intermWidth;
    }
    return width;
}

Hope this code could be useful for you.

Best regards,

Thank you for your advice!

I encountered an incosistent issue regarding cell length. I have a table. When it is at one location, it is cell content is no wrapped, while it is wrapped at another location.

Hi Steve,

Thanks for your request. But could you please attach your sample code and documents and explain how we can reproduce the problem? We will check the issue and provide you more information.

Best regards,

I will do later!

Thank you Steve, I will wait for your inputs.

The issues you have found earlier (filed as WORDSNET-2044) have been fixed in this .NET update and in this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(50)