Free Support Forum - aspose.com

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:
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-GB</w:LidThemeOther>
<w:LidThemeAsian>ZH-CN</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
<w:UseFELayout/>
</w:Compatibility>
<w:DoNotOptimizeForBrowser/>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Calibri","sans-serif"; mso-bidi-font-family:"Times New Roman";}

<![endif]–>

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:
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-GB</w:LidThemeOther>
<w:LidThemeAsian>ZH-CN</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
<w:UseFELayout/>
</w:Compatibility>
<w:DoNotOptimizeForBrowser/>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Calibri","sans-serif"; mso-bidi-font-family:"Times New Roman";}

<![endif]–>

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:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-autofit-a-table-to-page-width.html

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;

        <span style="color:blue;">if</span> (columnCount < 1) <span style="color:blue;">return</span>;

        <span style="color:blue;">double</span>[] columnWidth = <span style="color:blue;">new</span> <span style="color:blue;">double</span>[columnCount];


        <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i < columnCount; i++)
        {
            <span style="color:blue;">for</span> (<span style="color:blue;">int</span> j = 0; j < table.Rows.Count; j++)
            {
                <span style="color:blue;">double</span> cellWidth = table.Rows[j].Cells[i].GetText().Length;
                columnWidth[i] = <span style="color:#2b91af;">Math</span>.Max(columnWidth[i], cellWidth);
            }

        }

        <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i < table.Rows.Count; i++)
        {
            <span style="color:blue;">for</span> (<span style="color:blue;">int</span> j = 0; j < table.Rows[i].Cells.Count; j++)
            {
                table.Rows[i].Cells[j].CellFormat.Width = columnWidth[j];
            }
        }

    }</pre><br>

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)