Skip to content

Commit 287632d

Browse files
author
Fahad Adeel
committed
Table Cell Merged Feature is added
1 parent c6a20dc commit 287632d

9 files changed

Lines changed: 950 additions & 214 deletions

File tree

FileFormat.Words-Tests/UnitTest1.cs

Lines changed: 130 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace FileFormat_Tests
2222
public class TestDocumentClass
2323
{
2424

25-
private static string testDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/";
26-
private static string processedDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/ProcessedDocs/";
25+
private static string testDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/";
26+
private static string processedDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/ProcessedDocs/";
2727
private static string testDoc = "UbuntuSoftwareCenter";
2828
/// <summary>
2929
/// Test #1 Create empty WordprocessingML document and save to disk
@@ -95,8 +95,8 @@ public void TestGetBuiltInDocumentProperties()
9595
[TestClass]
9696
public class ParagraphTests
9797
{
98-
private static string testDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/";
99-
private static string processedDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/ProcessedDocs/";
98+
private static string testDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/";
99+
private static string processedDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/ProcessedDocs/";
100100
private static string testDoc = "UbuntuSoftwareCenter";
101101

102102
[TestMethod]
@@ -253,8 +253,8 @@ public void GetRuns_ReturnsCorrectFormatting()
253253
public class RunTests
254254
{
255255

256-
private static string testDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/";
257-
private static string processedDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/ProcessedDocs/";
256+
private static string testDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/";
257+
private static string processedDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/ProcessedDocs/";
258258
private static string testDoc = "UbuntuSoftwareCenter";
259259
[TestMethod]
260260
public void TestRunText()
@@ -323,18 +323,16 @@ public void TestRunIsItalic()
323323
[TestClass]
324324
public class TestsTableClass
325325
{
326-
private const string TestFile = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/table.docx";
326+
private const string TestFile = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/Docs.docx";
327327

328328
[TestMethod]
329329
public void TestCreateTable()
330330
{
331331
var doc = new Document();
332-
var body = new Body(doc);
333-
Table table = new Table();
334-
TableRow tableRow = new TableRow();
335-
TableRow tableRow2 = new TableRow();
332+
Body body = new Body(doc);
336333

337-
TableProperties tableProperties = new TableProperties();
334+
// ********** this code block creates a table into a word document ********** //
335+
Table table = new Table();
338336

339337
TopBorder topBorder = new TopBorder();
340338
topBorder.basicBlackSquares_border(20);
@@ -355,91 +353,162 @@ public void TestCreateTable()
355353
insideHorizontalBorder.basicBlackSquares_border(20);
356354

357355
TableBorders tableBorders = new TableBorders();
358-
359356
tableBorders.AppendTopBorder(topBorder);
360357
tableBorders.AppendBottomBorder(bottomBorder);
361358
tableBorders.AppendRightBorder(rightBorder);
362359
tableBorders.AppendLeftBorder(leftBorder);
363360
tableBorders.AppendInsideVerticalBorder(insideVerticalBorder);
364361
tableBorders.AppendInsideHorizontalBorder(insideHorizontalBorder);
365362

366-
tableProperties.Append(tableBorders);
363+
// specify its border style of the table
364+
TableProperties tblProp = new TableProperties();
365+
366+
tblProp.Append(tableBorders);
367367

368368
TableJustification tableJustification = new TableJustification();
369-
tableJustification.AlignRight();
370-
tableProperties.Append(tableJustification);
371-
table.AppendChild(tableProperties);
369+
tableJustification.AlignLeft();
370+
// set the position of the table to Right.
371+
tblProp.Append(tableJustification);
372+
373+
// create two table rows
374+
TableRow tableRow = new TableRow();
375+
TableRow tableRow2 = new TableRow();
372376

377+
// create table cell
373378
TableCell tableCell = new TableCell();
379+
Paragraph para = new Paragraph();
380+
Run run = new Run();
374381

375-
TableCellWidth tableCellWidth = new TableCellWidth("1400");
376-
TableCellProperties tableCellProperties = new TableCellProperties();
377-
tableCellProperties.Append(tableCellWidth);
378-
tableCell.Append(tableCellProperties);
382+
// set the header of the first column
383+
table.TableHeaders("Name");
384+
run.Text = "Mustafa";
385+
para.AppendChild(run);
386+
tableCell.Append(para);
379387

380-
TableCell tableCell2 = new TableCell();
381-
TableCellWidth tableCellWidth2 = new TableCellWidth("900");
382-
TableCellProperties tableCellProperties2 = new TableCellProperties();
383-
tableCellProperties2.Append(tableCellWidth2);
384-
tableCell2.Append(tableCellProperties2);
385-
386-
table.TableHeaders("name");
387-
table.TableHeaders("age");
388-
Paragraph paragraph = new Paragraph();
389-
Run run = new Run();
390-
run.Text = "abdul";
391-
paragraph.AppendChild(run);
392-
tableCell.Append(paragraph);
388+
// create table properties
389+
TableCellProperties tblCellProps = new TableCellProperties();
390+
391+
// set the width of table cell
392+
tblCellProps.Append(new TableCellWidth("2400"));
393+
tableCell.Append(tblCellProps);
393394

394-
Paragraph paragraph2 = new Paragraph();
395+
TableCell tableCell2 = new TableCell();
396+
Paragraph para2 = new Paragraph();
395397
Run run2 = new Run();
396-
run2.Text = "10";
397-
paragraph2.AppendChild(run2);
398-
tableCell2.Append(paragraph2);
398+
399+
// set the header of the second column
400+
table.TableHeaders("Nationality");
401+
run2.Text = "Pakistani";
402+
para2.AppendChild(run2);
403+
tableCell2.Append(para2);
404+
405+
TableCellProperties tblCellProps2 = new TableCellProperties();
406+
407+
VerticalMerge verticalMerge = new VerticalMerge();
408+
verticalMerge.MergeRestart = true;
409+
tblCellProps.Append(verticalMerge);
410+
411+
HorizontalMerge horizontalMerge = new HorizontalMerge();
412+
horizontalMerge.MergeRestart = true;
413+
tblCellProps2.Append(horizontalMerge);
414+
415+
tblCellProps2.Append(new TableCellWidth("1400"));
416+
tableCell2.Append(tblCellProps2);
399417

400418
TableCell tableCell3 = new TableCell();
401-
Paragraph paragraph3 = new Paragraph();
419+
Paragraph para3 = new Paragraph();
402420
Run run3 = new Run();
403-
run3.Text = "mustafa";
404-
paragraph3.AppendChild(run3);
405-
tableCell3.Append(paragraph3);
406-
407-
TableCell tableCell4 = new TableCell();
408-
Paragraph paragraph4 = new Paragraph();
409-
Run run4 = new Run();
410-
run4.Text = "30";
411-
paragraph4.AppendChild(run4);
412-
tableCell4.Append(paragraph4);
421+
table.TableHeaders("Age");
422+
run3.Text = "30";
423+
para3.AppendChild(run3);
424+
tableCell3.Append(para3);
425+
426+
HorizontalMerge horizontalMerge1 = new HorizontalMerge();
427+
horizontalMerge1.MergeContinue = true;
428+
TableCellProperties tblCellProps3 = new TableCellProperties();
429+
tblCellProps3.Append(new TableCellWidth("1400"));
430+
tblCellProps3.Append(horizontalMerge1);
431+
tableCell3.Append(tblCellProps3);
413432

414433
tableRow.Append(tableCell);
415434
tableRow.Append(tableCell2);
435+
tableRow.Append(tableCell3);
436+
437+
// create table cell
438+
TableCell _tableCell = new TableCell();
439+
Paragraph _para = new Paragraph();
440+
Run _run = new Run();
441+
442+
_run.Text = "sultan";
443+
_para.AppendChild(_run);
444+
_tableCell.Append(_para);
445+
446+
TableCellProperties tblCellProps1_ = new TableCellProperties();
447+
448+
VerticalMerge verticalMerge2 = new VerticalMerge();
449+
verticalMerge2.MergeContinue = true;
450+
tblCellProps1_.Append(verticalMerge2);
451+
452+
tblCellProps1_.Append(new TableCellWidth("2400"));
453+
_tableCell.Append(tblCellProps1_);
454+
416455

417-
tableRow2.Append(tableCell3);
418-
tableRow2.Append(tableCell4);
456+
TableCell _tableCell2 = new TableCell();
457+
Paragraph _para2 = new Paragraph();
458+
Run _run2 = new Run();
459+
460+
_run2.Text = "British";
461+
_para2.AppendChild(_run2);
462+
_tableCell2.Append(_para2);
463+
464+
TableCellProperties tblCellProps2_ = new TableCellProperties();
465+
tblCellProps2_.Append(new TableCellWidth("1400"));
466+
_tableCell2.Append(tblCellProps2_);
467+
468+
TableCell _tableCell3 = new TableCell();
469+
Paragraph _para3 = new Paragraph();
470+
Run _run3 = new Run();
471+
472+
_run3.Text = "2";
473+
_para3.AppendChild(_run3);
474+
_tableCell3.Append(_para3);
475+
476+
TableCellProperties tblCellProps3_ = new TableCellProperties();
477+
tblCellProps3_.Append(new TableCellWidth("1400"));
478+
_tableCell3.Append(tblCellProps3_);
479+
480+
tableRow2.Append(_tableCell);
481+
tableRow2.Append(_tableCell2);
482+
tableRow2.Append(_tableCell3);
483+
484+
table.AppendChild(tblProp);
419485
table.Append(tableRow);
420486
table.Append(tableRow2);
487+
421488
body.AppendChild(table);
422-
int count = body.FindTableByText("name");
489+
490+
491+
int count = body.FindTableByText("Name");
423492
Assert.AreEqual(count, 1);
424493
foreach (TableRow row in body.FindTableRow(0, 1))
425494
{
426-
Assert.AreEqual(int.Parse(row.NumberOfCell), 2);
495+
Assert.AreEqual(int.Parse(row.NumberOfCell), 3);
427496
}
428497
foreach (TableCell cell in body.FindTableCell(0, 0, 0))
429498
{
430-
Assert.AreEqual(cell.CellWidth, "1400");
431-
Assert.AreEqual(cell.Text, "name");
499+
Assert.AreEqual(cell.CellWidth, "2400");
500+
Assert.AreEqual(cell.Text, "Name");
432501
}
433502

434503
Assert.AreEqual(body.getDocumentTables.Count(), 1);
435504
foreach (FileFormat.Words.Table.Table props in body.getDocumentTables)
436505
{
437506
Assert.AreEqual(int.Parse(props.NumberOfRows), 3);
438-
Assert.AreEqual(int.Parse(props.NumberOfColumns), 2);
439-
Assert.AreEqual(int.Parse(props.NumberOfCells), 6);
507+
Assert.AreEqual(int.Parse(props.NumberOfColumns), 3);
508+
Assert.AreEqual(int.Parse(props.NumberOfCells), 9);
440509
Assert.AreEqual(props.TableBorder, "basicBlackSquares");
441-
Assert.AreEqual(props.CellWidth, "1400");
442-
Assert.AreEqual(props.TablePosition, "right");
510+
Assert.AreEqual(props.CellWidth, "2400");
511+
Assert.AreEqual(props.TablePosition, "left");
443512

444513
}
445514
Assert.AreEqual(table.ChangeTextInCell(TestFile, 0, 0, 0, "changed"), "Cell updated successfully");
@@ -453,8 +522,8 @@ public void TestCreateTable()
453522
[TestClass]
454523
public class ImageTests
455524
{
456-
private static string testDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/";
457-
private static string processedDir = "/Users/fahadadeelqazi/github/Aspose/FileFormat.Words-for-.NET/TestDocs/ProcessedDocs/";
525+
private static string testDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/";
526+
private static string processedDir = "/Users/Mustafa/Projects/FileFormat.Words/TestDocs/ProcessedDocs/";
458527
private static string testDoc = "UbuntuSoftwareCenter";
459528
[TestMethod]
460529
public void TestAddImageToDoc()

FileFormat.Words/Body.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,55 @@
77

88
namespace FileFormat.Words
99
{
10+
/// <summary>
11+
/// The Body class represents the main body of the Word document.
12+
/// </summary>
1013
public class Body : Document
1114
{
15+
/// <value>
16+
/// An object of the Parent Body class.
17+
/// </value>
1218
protected internal DocumentFormat.OpenXml.Wordprocessing.Body wordDocumentBody;
1319

20+
/// <summary>
21+
/// Instantiate a new instance of the Body class.
22+
/// </summary>
23+
/// <param name="doc">An object of the Document class.</param>
1424
public Body(Document doc)
1525
{
1626
this.wordDocumentBody = doc.wordDocument.MainDocumentPart.Document.Body;
1727
}
1828

29+
/// <summary>
30+
/// This method adds a Paragraph to a Word document.
31+
/// </summary>
32+
/// <param name="para">An object of the Paragragh class.</param>
1933
public void AppendChild(Paragraph para)
2034
{
2135
this.wordDocumentBody.AppendChild(para.wordDocumentParagraph);
2236
}
37+
38+
/// <summary>
39+
/// This method adds a Table to a Word document.
40+
/// </summary>
41+
/// <param name="tab">An object of the Table class.</param>
2342
public void AppendChild(Table.Table tab)
2443
{
2544
this.wordDocumentBody.Append(tab.table);
2645
}
46+
47+
/// <summary>
48+
/// This method adds a new line to a Word document.
49+
/// </summary>
2750
public void LineBreak()
2851
{
2952
this.wordDocumentBody.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new Text(" "))));
3053
}
54+
55+
/// <summary>
56+
/// This method returns a list of Paragraphs from a Word document.
57+
/// </summary>
58+
/// <returns>A list of Paragraphs.</returns>
3159
public List<Paragraph> GetParagraphs()
3260
{
3361
var paragraphs = this.wordDocumentBody.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
@@ -51,6 +79,13 @@ public List<Paragraph> GetParagraphs()
5179
}
5280
return lst;
5381
}
82+
83+
/// <summary>
84+
/// This property returns a list of Tables from a Word document.
85+
/// </summary>
86+
/// <value>
87+
/// A list of type Tables.
88+
/// </value>
5489
public List<Table.Table> getDocumentTables
5590
{
5691

@@ -85,6 +120,14 @@ public List<Table.Table> getDocumentTables
85120
return ls;
86121
}
87122
}
123+
124+
/// <summary>
125+
/// This method returns a specific cell of a row along with cell props and Text.
126+
/// </summary>
127+
/// <param name="tableIndex">An integer value represents the table index. </param>
128+
/// <param name="tableRow">An integer value represents row index.</param>
129+
/// <param name="tableCell">An integer value represents a cell index.</param>
130+
/// <returns>A list of cell objects.</returns>
88131
public List<Table.TableCell> FindTableCell(int tableIndex, int tableRow, int tableCell)
89132
{
90133

@@ -125,6 +168,13 @@ public List<Table.Table> getDocumentTables
125168
ls.Add(tableCell1);
126169
return ls;
127170
}
171+
172+
/// <summary>
173+
/// This method returns a specific row and cell count from a particular table of an existing document.
174+
/// </summary>
175+
/// <param name="tableindex">An integer value represents a table index.</param>
176+
/// <param name="tableRowIndex">An integer value represents the row index.</param>
177+
/// <returns>A list of row objects.</returns>
128178
public List<Table.TableRow> FindTableRow(int tableindex, int tableRowIndex)
129179
{
130180
Table.Table tab = new Table.Table();
@@ -151,6 +201,11 @@ public List<Table.Table> getDocumentTables
151201
ls.Add(tableRow1);
152202
return ls;
153203
}
204+
/// <summary>
205+
/// This method returns a table count that contains a specific text value.
206+
/// </summary>
207+
/// <param name="text">Any string value.</param>
208+
/// <returns>An integer value.</returns>
154209
public int FindTableByText(string text)
155210
{
156211
List<DocumentFormat.OpenXml.Wordprocessing.Table> table = wordDocumentBody.Elements<DocumentFormat.OpenXml.Wordprocessing.Table>().ToList();

0 commit comments

Comments
 (0)