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

Free Support Forum - aspose.com

Wrap the long URL in table cell using C#

Dear Ladies and Gentlemen,

I have a little Problem with URL wrapping in aspose.words.

I create a word document with aspose.words. Then I’d like to add a table with some text to the word document. This text can contains urls. Unfortunaly the URL isn’t wrapped automatically. So the right part of the created pdf file is cut off. How can I enable url wrapping?

Here is the sample code:



Dim doc As New Aspose.Words.DocumentBuilder
doc.StartTable()

doc.InsertCell()
doc.Write(“ThisIsAVeryVeryLongWord”)

doc.InsertCell()
doc.Write(“Here is the URL: http://example.org?param1=this+is+a+param&param2=moreparam&param3=1234567”)

doc.InsertCell()
doc.Write(“ThisIsAVeryVeryLongWord”)

doc.EndRow()
doc.EndTable()

doc.Document.Save(“C:\wegdamit\test.pdf”)

Sincere regards,

Björn

								<br><br>

Hi Björn,

Thanks for your query. You are trying to add text without space like (ThisIsAVeryVeryLongWord). If you do the same by using MS Word, the table’s width will be greater then page width and the last cell come out from the page.

For your kind reference, please visit following links. Please let me know, If you have any further queries.

Specifying Table and Cell Widths
CellFormat.WrapText Property

Thank you for your answer.

tahir.manzoor:

Thanks for your query. You are trying to add text without space like (ThisIsAVeryVeryLongWord). If you do the same by using MS Word, the table’s width will be greater then page width and the last cell come out from the page.

In my tests MS Word is going to wrap the url.

tahir.manzoor:

For your kind reference, please visit following links. Please let me know, If you have any further queries.

I have fixed the problem. Setting the following paramters solves the problem:
table.AllowAutoFit = False
table.PreferredWidth = PreferredWidth.FromPercent(100)

Hi Björn,

I am really thankful to you for correcting me. We always admire the positive feedback and active participation from our customers.

I am also facing the same issue in aspose word where url is not getting wrapped. Tried with AutoFitTowindow, AutoFitToContent with allow autofit to false as well as true though url column is getting wrapped it is impacting other columns as in below screenshot

Here is snippet of code.

  Document doc = new Document();
            int counter = 0;
            Table table = new Table(doc);
            doc.FirstSection.Body.AppendChild(table);
            Row rowHeader = GetNewRow(doc);
            table.AppendChild(rowHeader);
            Style style = GetTableRowHeaderStyle(doc);
            style.Font.Size = 8;
            Style styleRow = GetTableRowStyle(doc);
            styleRow.Font.Size = 8;
            string[] headers = new string[] { "ID", "Name", "Value", "Language" };
            int[] cellWidths = new int[] { 300, 150, 400, 150 };
            BuildRowHeader(doc, rowHeader, headers, style, cellWidths);

            foreach (var languagesepecific in alllanguagelinklibrarydetails)
            {
                languagesepecific.Value.ForEach((linklib) =>
                {
                    counter++;
                    Row row = GetNewRow(doc);
                    table.AppendChild(row);
                    Color cellColor = Color.FromArgb(221, 219, 213);
                    if (counter % 2 != 0)
                    {
                        cellColor = Color.White;
                    }

                    doc = BuildTableCell(doc, row, Convert.ToString(linklib.code), cellColor, 0, styleRow, false);
                    doc = BuildTableCell(doc, row, linklib.name, cellColor, 0, styleRow, false);
                    doc = BuildTableCell(doc, row, linklib.value, cellColor, 0, styleRow, false);
                    doc = BuildTableCell(doc, row, languagesepecific.Key, cellColor, 0, styleRow, false);
                });

            }
            **table.AutoFit(AutoFitBehavior.AutoFitToWindow);**
            table.SetBorders(LineStyle.Single, 1.0, Color.Gray);
    indent preformatted text by 4 spaces

 private static Document BuildTableCell(Document doc, Row tableRow, string cellText, Color cellBgColor,
            int borderW`Preformatted text`idth, Style cellStyle, bool wrapText = false, double cellWidth = 0)
        {
            var cell = new Cell(doc);
            cell.CellFormat.Shading.BackgroundPatternColor = cellBgColor;
            cell.CellFormat.Borders.LineWidth = borderWidth;
            cell.CellFormat.WrapText = wrapText;
            cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
            cell.CellFormat.LeftPadding = 5;

            if (cellWidth != 0)
                cell.CellFormat.Width = cellWidth;
            tableRow.AppendChild(cell);
            var para = new Paragraph(doc);
            para.ParagraphFormat.Style = cellStyle;
            tableRow.LastCell.AppendChild(para);
            var htmlDocument = new HtmlAgilityPack.HtmlDocument();
            htmlDocument.LoadHtml(cellText);
            tableRow.LastCell.FirstParagraph.AppendChild(new Run(doc, htmlDocument.DocumentNode.InnerText));
            return doc;
        }

table.png (10.1 KB)

@Anil.Vebilisetty The question is answered in another your thread:
https://forum.aspose.com/t/unable-to-wrap-long-url-in-aspose-word-cell-c-wrapping-url-column-is-impacting-other-columns/252601