diff --git a/OfficeIMO.Tests/Word.Tables.cs b/OfficeIMO.Tests/Word.Tables.cs index c14c25d9..e6e90ee7 100644 --- a/OfficeIMO.Tests/Word.Tables.cs +++ b/OfficeIMO.Tests/Word.Tables.cs @@ -859,5 +859,64 @@ public void Test_CreatingWordDocumentWithTablesWithReplace() { } + [Fact] + public void Test_CreatingWordDocumentWithTablesClearParagraphs() { + string filePath = Path.Combine(_directoryWithFiles, "CreatedDocumentWithTables.docx"); + using (WordDocument document = WordDocument.Create(filePath)) { + Assert.True(document.Paragraphs.Count == 0, "Number of paragraphs during creation is wrong. Current: " + document.Paragraphs.Count); + Assert.True(document.Tables.Count == 0, "Tables count matches"); + Assert.True(document.Lists.Count == 0, "List count matches"); + + var paragraph = document.AddParagraph("Basic paragraph - Page 4"); + paragraph.ParagraphAlignment = JustificationValues.Center; + + WordTable wordTable = document.AddTable(3, 4); + wordTable.Rows[0].Cells[0].Paragraphs[0].Text = "Test 1"; + wordTable.Rows[1].Cells[0].Paragraphs[0].Text = "Test 2"; + wordTable.Rows[2].Cells[0].Paragraphs[0].Text = "Test 3"; + + wordTable.Rows[0].Cells[1].Paragraphs[0].Text = "Test Row 0 Cell 1"; + + Assert.True(document.Tables.Count == 1); + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs.Count == 1); + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs.Count == 1); + + // add 2 more texts to the same cell as new paragraphs + wordTable.Rows[0].Cells[0].Paragraphs[0].AddParagraph("New"); + wordTable.Rows[0].Cells[0].Paragraphs[1].AddParagraph("New more"); + + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs.Count == 3); + + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].Text == "Test 1"); + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[1].Text == "New"); + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[2].Text == "New more"); + + // replace existing paragraphs with single one + wordTable.Rows[0].Cells[0].AddParagraph("New paragraph, delete rest", true); + + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs.Count == 1); + Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].Text == "New paragraph, delete rest"); + + // lets try to add new paragraph to the same cell, using WordParagraph + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs.Count == 1); + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[0].Text == "Test Row 0 Cell 1"); + + WordParagraph paragraph1 = new WordParagraph { + Text = "Paragraph added separately as WordParagraph", + Bold = true, + Italic = true + }; + + wordTable.Rows[0].Cells[1].AddParagraph(paragraph1); + + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs.Count == 2); + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[0].Text == "Test Row 0 Cell 1"); + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[1].Text == "Paragraph added separately as WordParagraph"); + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[1].Bold == true); + Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[1].Italic == true); + + document.Save(false); + } + } } } diff --git a/OfficeIMO.Word/WordTableCell.cs b/OfficeIMO.Word/WordTableCell.cs index 96e48cdd..473f2302 100644 --- a/OfficeIMO.Word/WordTableCell.cs +++ b/OfficeIMO.Word/WordTableCell.cs @@ -99,9 +99,24 @@ public string ShadingFillColorHex { /// /// Add paragraph to the table cell /// - /// - /// - public WordParagraph AddParagraph(WordParagraph paragraph = null) { + /// The paragraph to add to this cell, if + /// this is not passed then a new empty paragraph with settings from + /// the previous paragraph will be added. + /// If value is not passed or false then add + /// the given paragraph into the cell. If set to true then clear + /// every existing paragraph before adding the new paragraph. + /// + /// A reference to the added paragraph. + public WordParagraph AddParagraph(WordParagraph paragraph = null, bool removeExistingParagraphs = false) { + // Considering between implementing a reset that clears all paragraphs or + // a deletePrevious that will replace the last paragraph. + // NOTE: Raise this during PR. + if (removeExistingParagraphs) { + var paragraphs = _tableCell.ChildElements.OfType().ToList(); + foreach (var wordParagraph in paragraphs) { + wordParagraph.Remove(); + } + } if (paragraph == null) { paragraph = new WordParagraph(this._document); } @@ -113,9 +128,10 @@ public WordParagraph AddParagraph(WordParagraph paragraph = null) { /// Add paragraph to the table cell with text /// /// + /// /// - public WordParagraph AddParagraph(string text) { - return AddParagraph().SetText(text); + public WordParagraph AddParagraph(string text, bool removeExistingParagraphs = false) { + return AddParagraph(paragraph: null, removeExistingParagraphs).SetText(text); } /// @@ -339,7 +355,7 @@ public void MergeHorizontally(int cellsCount, bool copyParagraphs = false) { } /// - /// Splits (unmerge) cells that were merged + /// Splits (unmerge) cells that were merged /// /// public void SplitHorizontally(int cellsCount) { @@ -359,7 +375,7 @@ public void SplitHorizontally(int cellsCount) { } /// - /// Merges two or more cells together vertically + /// Merges two or more cells together vertically /// /// ///