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;
}
}
}
}