How to remove a linefeed from the end of a table cell

Posted by: david.sheppard on 12 September 2023, 10:04 pm EST

    • Post Options:
    • Link

    Posted 12 September 2023, 10:04 pm EST

    When I make tables and put items in the cells, I always have line feeds chr(10) as the last character of text in the cell. This leaves a blank line after each row in the table.

    How can I remove the last character in a table cell?

    Attached is a word document with an example.

    TableWithLineFeeds.zip

  • Posted 13 September 2023, 6:13 pm EST

    Hi,

    You can delete the extra empty Paragraphs from each cell to achieve your requirement.

    GcWordDocument doc = new GcWordDocument();
    doc.Load("TableWithLineFeeds.DOCX");
    var table = doc.Body.Tables[0];
    for(int i=0; i <table.Rows.Count;i++)
    {
        var row = table.Rows[i];
        for(int j=0;j<row.Cells.Count;j++)
        {
            var cell = row.Cells[j];
            foreach(var par in cell.Children)
            {
                if (par is Paragraph)
                    if ((par as Paragraph).Children.Count() == 0)
                        par.Delete();
            }        
        }
    }
    doc.Save("TableWithoutLineFeeds.DOCX");

    Please refer the attached sample for the same: WordRemoveLineFeed.zip

    Best Regards,

    Nitin

  • Posted 13 September 2023, 11:14 pm EST

    Thank you. The .children and children.count properties are what I was missing. This solution works and makes logical sense.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels