Merge Table Cells Range in Word document | Sort Text by Alphabetical or Numerical Order | Apply Bullet Points C# .NET

I tried the same but it does’t work well for me…In my case there is one table with 5 rows, each row is having cells…with some cell having text and some are blank…so i firstly shifting the cell values to previous if there is blank value…and after that i applied your logic for merging 3 column cell values to single column but it doesn’t work and shows only one cell with text.

What I need to do is,

  1. Select range of cells of the table.
  2. Merge selected cells.
  3. Apply bullet-points and sort ascending.
  4. Split cells into 3 Column and 1 Row.

Appreciate Help.

Thanks…!!

@Pankaj31,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for .NET 20.8 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words for .NET.

Aspose.Words for .NET 13.1.0.0 generated output DOCX file showing the undesired behavi.pdf (132.5 KB)
Simplified input Word document.pdf (132.5 KB)
Your expected DOCX file showing the desired output.pdf (126.6 KB)

Code Logic Used Currently.pdf (81.1 KB)

I attached the code logic used …also pasting here,


using System;
using System.Linq;
using Aspose.Words;
using NUnit.Framework;
using Aspose.Words.Tables;
using System.Collections;
using System.Drawing;

namespace EditDocument
{
    public class Program
    {
        public static void Main()
        {
            try
            {
                var license = new License();
                license.SetLicense("Aspose.Words.lic");

                Document doc = new Document("File Path Location");
                DocumentBuilder builder = new DocumentBuilder(doc);

                TableCollection tables1 = doc.FirstSection.Body.Tables;

                Table table;
                for (int i = 0; i < tables1.Count; i++)
                {
                    if (i == 4 || i == 5)
                    {
                        table = (Table)doc.GetChild(NodeType.Table, i, true);
                        TableCellMerge(table, i);
                    }
                }
                doc.Save(@"File Location");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Generated =>" + ex.ToString());
            }
            finally
            {
                Console.WriteLine("Cell Data Switched Successfully");
            }
        }
          
        private static void TableCellMerge(Table table, int tblNum)
        {
            RowCollection rows = table.Rows;
            Assert.AreEqual(rows, rows.ToArray());
            Assert.AreNotSame(rows, rows.ToArray());

            for (int j = 1; j < rows.Count; j++)
            {
                Console.WriteLine($"\tStart of Row {j}");

                CellCollection cells = rows[j].Cells;

                Assert.AreEqual(cells, cells.ToArray());
                Assert.AreNotSame(cells, cells.ToArray());
                string cellText = string.Empty;

                for (int k = 0; k < cells.Count; k++)
                {
                    if (j == 1 || j == 2 || j == 3)
                    {
                        if (k == 1 && j == 2 && tblNum == 5)
                        {
                            cellText = rows[j].Cells[k + 1].ToString(SaveFormat.Text).Trim();
                        }
                        else
                        {
                            cellText = rows[j].Cells[k].ToString(SaveFormat.Text).Trim();
                        }
                    }
                    if (j == 4 || j == 5)
                        cellText = string.Empty;

                    if (string.IsNullOrEmpty(cellText) && j == 1)
                    {
                        if (rows[j + 1] != null)
                            cellText = rows[j + 1].Cells[k].ToString(SaveFormat.Text).Trim();
                        if (string.IsNullOrEmpty(cellText))
                        {
                            if (rows[j + 2] != null)
                                cellText = rows[j + 2].Cells[k].ToString(SaveFormat.Text).Trim();
                        }
                    }
                    else if (string.IsNullOrEmpty(cellText) && j == 2)
                    {
                        if (k == 0)
                        {
                            if (rows[j + 3] != null)
                                cellText = rows[j + 3].Cells[k].ToString(SaveFormat.Text).Trim();
                        }
                        else if ((k == 1 || k == 2) && tblNum != 5)
                        {
                            if (rows[j + 1] != null)
                                cellText = rows[j + 1].Cells[k].ToString(SaveFormat.Text).Trim();
                        }
                    }

                    else if (j == 3)
                    {
                        if ((k == 0 || k == 1 || k == 2) && tblNum != 5)
                        {
                            if (rows[j + 1] != null)
                                cellText = rows[j + 1].Cells[k].ToString(SaveFormat.Text).Trim();
                        }
                    }

                    else if (string.IsNullOrEmpty(cellText) && j == 4)
                    {
                        if (k == 1 || k == 2)
                        {
                            if (rows[j + 1] != null)
                                cellText = rows[j + 1].Cells[k].ToString(SaveFormat.Text).Trim();
                        }
                    }

                    else if (string.IsNullOrEmpty(cellText) && j == 5)
                    {
                        if (k == 0 || k == 1 || k == 2)
                            cellText = "";
                    }
                    ReplaceCellText(rows[j].Cells[k], cellText);
                    Console.WriteLine($"\t\tContents of Cell:{k} = \"{cellText}\"");
                    cellText = string.Empty;
                }
                Console.WriteLine($"\tEnd of Row {j}");
            }
        }

        private static void ReplaceCellText(Cell cell, string cellText)
        {
            Node node = cell.LastParagraph;
            DocumentBuilder b = new DocumentBuilder((Document)cell.Document);
            b.MoveTo(cell.LastParagraph);
            b.Writeln("");
            b.ListFormat.ApplyBulletDefault();
            b.Write(cellText);

            while (node != null)
            {
                Node nextNode = node.PreviousSibling;                
                node.Remove();
                if (string.IsNullOrEmpty(cellText))
                    b.CurrentParagraph.ListFormat.RemoveNumbers();
                node = nextNode;
            }
        }
    }
}

@Pankaj31,

You have only provided the PDF files. Please also ZIP and attach the actual simplified input Word document (and other documents) in DOCX format here for further testing.

Aspose.Words for .NET 13.1.0.0 generated output DOCX file showing the undesired behavi.zip (32.3 KB)
Simplified input Word document.zip (36.9 KB)
Your expected DOCX file showing the desired output.zip (39.3 KB)

Attached files in zip format…you can extract and see what format i need.

Appreciate your help…its urgent for me to finish this work.

SBC_CustomizationScreens.zip (394.8 KB)

The attached document explains what i need to achieve here…Only thing is that i needed in c# or vb.net code.

@Pankaj31,

We are working on your query and will get back to you soon.

Thanks for your help…!!

Please let me know if you able to create the solution regarding my query.

Appreciate your help again…!!

@Pankaj31,

Please refer to the following section of documentation:

And try running the following code that will select range of cells, merge those cells and copy bulleted paragraphs.

Document doc = new Document("C:\\Temp\\Simplified input Word document\\Simplified input Word document.docx");

Table table = doc.FirstSection.Body.Tables[4];
Table cloneTable = (Table)table.Clone(true);

for (int i = 1; i < table.Rows.Count; i++)
{
    for (int j = 0; j < table.Rows[i].Count; j++)
    {
        table.Rows[i].Cells[j].RemoveAllChildren();
        if (j == 0)
            table.Rows[i].Cells[j].CellFormat.HorizontalMerge = CellMerge.First;
        else
            table.Rows[i].Cells[j].CellFormat.HorizontalMerge = CellMerge.Previous;
    }
}

for (int i = 1; i < table.Rows.Count; i++)
{
    if (i == 0)
        table.Rows[i].Cells[0].CellFormat.VerticalMerge = CellMerge.First;
    else
        table.Rows[i].Cells[0].CellFormat.VerticalMerge = CellMerge.Previous;
}


foreach (Paragraph para in cloneTable.GetChildNodes(NodeType.Paragraph, true))
    if (!string.IsNullOrEmpty(para.ToString(SaveFormat.Text).Trim()))
        table.Rows[1].Cells[0].AppendChild(para.Clone(true));

doc.Save(@"C:\Temp\Simplified input Word document\20.8.docx");

We have also logged following new feature request in our issue tracking system:

  • WORDSNET-20908: Provide API to Sort or Arrange current Selection by Alphabetical or Numerical Order

We will further look into the details of this problem and will keep you updated on the status of linked issue. We apologize for your inconvenience.

Thanks for your response.

Could you please let me know the complete solution as per my requirement mentioned in the document which i sent you earlier.

Appreciate your help…!!

@Pankaj31,

I am afraid, Aspose.Words currently does not support Paragraph text sorting by Alphabetical or Numerical Order. WORDSNET-20908 is currently pending for analysis and is in the queue. We will inform you via this forum thread as soon as Sorting will get supported in future or any more updates may be available.

Hi,
Do you have any update on WORDSNET-20908.

Thanks
Pankaj Singh

@Pankaj31,

Unfortunately, there is no further news about it. This issue is currently pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved or any further updates about your requirement are available. We apologize for any inconvenience.

@Pankaj31,

Regarding WORDSNET-20908, we have completed the work on this issue and concluded to close this issue with “Won’t fix” status i.e. we won’t be able to include any fix of this issue in Aspose.Words API. However, please check following analysis details and code:

Source word document has several tables (see WORDSNET-20908 Analysis 01.png (32.3 KB)) and you want to achieve something like this (see WORDSNET-20908 Analysis 02.png (15.7 KB)). Attached code implements these steps:

  1. Sort the paragraphs and save them to the collection.
  2. Delete unnecessary lines.
  3. Set borders.
  4. Remove paragraphs from cells.
  5. Add the sorted paragraphs from the collection to the cells.

And the output file (please see Test20908_out.docx) generated by this code contains something like this (see WORDSNET-20908 Analysis 03.png (13.9 KB)).