Skip to content

Commit

Permalink
Merge pull request #30 from EvotecIT/TablesImprovment
Browse files Browse the repository at this point in the history
Improve tables and comments
  • Loading branch information
PrzemyslawKlys authored Jul 31, 2022
2 parents 08a5320 + 10d62dd commit 72a241b
Show file tree
Hide file tree
Showing 21 changed files with 1,308 additions and 103 deletions.
3 changes: 3 additions & 0 deletions OfficeIMO.Examples/OfficeIMO.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<None Update="Templates\ListsCreatedByWord.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\TableExamples.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion OfficeIMO.Examples/OfficeIMO.Examples.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=word_005Cheadersandfooters/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=word_005Chyperlinks/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=word_005Clists/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=word_005Cloaddocuments/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=word_005Cloaddocuments/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=word_005Ctables/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
5 changes: 4 additions & 1 deletion OfficeIMO.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void Main(string[] args) {
//BasicDocument.Example_BasicWord(folderPath, false);
//BasicDocument.Example_BasicWord2(folderPath, false);

AdvancedDocument.Example_AdvancedWord(folderPath, true);
//AdvancedDocument.Example_AdvancedWord(folderPath, true);

//BasicDocument.Example_BasicDocument(folderPath, true);
//BasicDocument.Example_BasicDocumentSaveAs1(folderPath, true);
Expand All @@ -48,6 +48,9 @@ static void Main(string[] args) {
//Tables.Example_BasicTables1(folderPath, false);
//Tables.Example_BasicTablesLoad1(folderPath, false);
//Tables.Example_BasicTablesLoad2(templatesPath, folderPath, true);
Tables.Example_BasicTablesLoad3(templatesPath, false);
Tables.Example_TablesWidthAndAlignment(folderPath, true);

//Tables.Example_AllTables(folderPath, false);
//Tables.Example_Tables(folderPath, false);
//Tables.Example_TableBorders(folderPath, true);
Expand Down
Binary file added OfficeIMO.Examples/Templates/TableExamples.docx
Binary file not shown.
87 changes: 87 additions & 0 deletions OfficeIMO.Examples/Word/Tables/Tables.Create5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using Color = SixLabors.ImageSharp.Color;

namespace OfficeIMO.Examples.Word {
internal static partial class Tables {
internal static void Example_TablesWidthAndAlignment(string folderPath, bool openWord) {
Console.WriteLine("[*] Creating standard document with width and alignment");
string filePath = System.IO.Path.Combine(folderPath, "Document with Table Alignment.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
var paragraph = document.AddParagraph("Lets add table with some alignment ");
paragraph.ParagraphAlignment = JustificationValues.Center;
paragraph.Bold = true;
paragraph.Underline = UnderlineValues.DotDash;

WordTable wordTable = document.AddTable(4, 4, WordTableStyle.GridTable1LightAccent1);
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[3].Cells[0].Paragraphs[0].Text = "Test 4";

wordTable.FirstRow.FirstCell.Paragraphs[0].AddComment("Adam Kłys", "AK", "Test comment for paragraph within a Table");

wordTable.FirstRow.FirstCell.ShadingFillColor = Color.Blue;
wordTable.Rows[1].FirstCell.ShadingFillColor = Color.Red;

wordTable.LastRow.FirstCell.ShadingPattern = ShadingPatternValues.Percent20;


wordTable.AddComment("Przemysław Kłys", "PK", "This is a table, and we just added comment to a whole table");

wordTable.LastRow.LastCell.Paragraphs[0].Text = "Last Cell";

wordTable.WidthType = TableWidthUnitValues.Pct;
wordTable.Width = 3000;
wordTable.Alignment = TableRowAlignmentValues.Center;

wordTable.Title = "This is title";
wordTable.Description = "Description of table";


var paragraph1 = document.AddParagraph("Lets add another table showing text wrapping around");

WordTable wordTable1 = document.AddTable(4, 4, WordTableStyle.GridTable1LightAccent1);
wordTable1.Rows[0].Cells[0].Paragraphs[0].Text = "Test 1";
wordTable1.Rows[1].Cells[0].Paragraphs[0].Text = "Test 2";
wordTable1.Rows[2].Cells[0].Paragraphs[0].Text = "Test 3";
wordTable1.Rows[3].Cells[0].Paragraphs[0].Text = "Test 4";

wordTable1.WidthType = TableWidthUnitValues.Pct;
wordTable1.Width = 3000;

wordTable1.AllowTextWrap = true;

var paragraph2 = document.AddParagraph("This paragraph should continue but next to to the table");

document.AddParagraph();
document.AddParagraph();

var paragraph3 = document.AddParagraph("Lets add another table showing AutoFit");

WordTable wordTable2 = document.AddTable(4, 4, WordTableStyle.GridTable1LightAccent1);
wordTable2.Rows[0].Cells[0].Paragraphs[0].Text = "Test 1";
wordTable2.Rows[1].Cells[0].Paragraphs[0].Text = "Test 2";
wordTable2.Rows[2].Cells[0].Paragraphs[0].Text = "Test 3";
wordTable2.Rows[3].Cells[0].Paragraphs[0].Text = "Test 4";

wordTable2.ColumnWidth = new List<int>() { 1716, 3817, 300, 3000 };
wordTable2.RowHeight = new List<int>() { 1000, 300, 500, 200 };

// add a cell to 3rd row
WordTableCell cell = new WordTableCell(document, wordTable2, wordTable2.Rows[2]);
cell.Paragraphs[0].Text = "This cell is outside a bit";
cell.TextDirection = TextDirectionValues.TopToBottomLeftToRightRotated;

wordTable2.LayoutType = TableLayoutValues.Fixed;

document.Save(openWord);
}
}
}
}
52 changes: 52 additions & 0 deletions OfficeIMO.Examples/Word/Tables/Tables.Load3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;

namespace OfficeIMO.Examples.Word {
internal static partial class Tables {
internal static void Example_BasicTablesLoad3(string folderPath, bool openWord) {
Console.WriteLine("[*] Loading standard document with multiple tables");
string filePath = System.IO.Path.Combine(folderPath, "TableExamples.docx");
using (WordDocument document = WordDocument.Load(filePath)) {


Console.WriteLine("Tables count: " + document.Tables.Count);

for (int i = 0; i < document.Tables.Count; i++) {
Console.WriteLine("===");
Console.WriteLine("Tables " + i + " style: " + document.Tables[i].Style);
Console.WriteLine("Tables " + i + " width type: " + document.Tables[i].WidthType);
Console.WriteLine("Tables " + i + " alignment: " + document.Tables[i].Alignment);
Console.WriteLine("Tables " + i + " width: " + document.Tables[i].Width);
}
//var paragraph = document.AddParagraph("Basic paragraph - Page 4");
//paragraph.ParagraphAlignment = JustificationValues.Center;

//WordTable wordTable = document.AddTable(3, 4, WordTableStyle.GridTable1LightAccent5);
//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 = document.AddTable(3, 4, WordTableStyle.GridTable1LightAccent6);
//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 = document.AddTable(3, 4, WordTableStyle.GridTable1LightAccent3);
//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 wordTableFromEarlier = document.Tables[0];
//wordTableFromEarlier.Rows[1].Cells[1].Paragraphs[0].Text = "Middle table";

document.Save(false);
}
}

}
}
Binary file added OfficeIMO.Tests/Documents/TableExamples.docx
Binary file not shown.
3 changes: 2 additions & 1 deletion OfficeIMO.Tests/Excel.BasicDocuments.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using OfficeIMO.Excel;
using Xunit;

Expand Down
3 changes: 3 additions & 0 deletions OfficeIMO.Tests/OfficeIMO.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<None Update="Documents\sample3.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Documents\TableExamples.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Documents\TestDocument365.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
190 changes: 190 additions & 0 deletions OfficeIMO.Tests/Word.Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,195 @@ public void Test_CreatingWordDocumentWithTablesWithSections() {
document.Save();
}
}

[Fact]
public void Test_CreatingWordDocumentWithTablesAndOptions() {
string filePath = Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesAndOptions.docx");
using (WordDocument document = WordDocument.Create(filePath)) {

WordTable wordTable = document.AddTable(4, 4, WordTableStyle.GridTable1LightAccent1);
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[3].Cells[0].Paragraphs[0].Text = "Test 4";

wordTable.FirstRow.FirstCell.Paragraphs[0].AddComment("Adam Kłys", "AK", "Test comment for paragraph within a Table");

Assert.True(wordTable.FirstRow.FirstCell.ShadingFillColor == null);

wordTable.FirstRow.FirstCell.ShadingFillColor = Color.Blue;

//Assert.True(wordTable.FirstRow.FirstCell.Paragraphs[0].Comments.Count == 1);
Assert.True(wordTable.FirstRow.FirstCell.ShadingFillColor == Color.Blue);


wordTable.Rows[1].FirstCell.ShadingFillColor = Color.Red;

Assert.True(wordTable.Rows[1].FirstCell.ShadingFillColor == Color.Red);

Assert.True(wordTable.LastRow.FirstCell.ShadingPattern == null);

wordTable.LastRow.FirstCell.ShadingPattern = ShadingPatternValues.Percent20;

Assert.True(wordTable.LastRow.FirstCell.ShadingPattern == ShadingPatternValues.Percent20);

document.Save(false);
}

using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesAndOptions.docx"))) {

var wordTable = document.Tables[0];

Assert.True(wordTable.Rows[1].FirstCell.ShadingFillColor == Color.Red);
Assert.True(wordTable.LastRow.FirstCell.ShadingPattern == ShadingPatternValues.Percent20);

wordTable.Rows[1].FirstCell.ShadingFillColorHex = "#0000FF";

Assert.True(wordTable.Rows[1].FirstCell.ShadingFillColor == Color.Blue);
Assert.True(wordTable.Rows[1].FirstCell.ShadingFillColorHex == "0000FF");
document.Save();
}

using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesAndOptions.docx"))) {

Assert.True(document.Tables[0].Rows[1].FirstCell.ShadingFillColor == Color.Blue);
Assert.True(document.Tables[0].Rows[1].FirstCell.ShadingFillColorHex == "0000FF");

document.Save();
}
}


[Fact]
public void Test_CreatingWordDocumentWithTablesAndMoreOptions() {
string filePath = Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesAndMoreOptions.docx");
using (WordDocument document = WordDocument.Create(filePath)) {

var wordTable1 = document.AddTable(4, 4, WordTableStyle.GridTable1LightAccent1);
wordTable1.Rows[0].Cells[0].Paragraphs[0].Text = "Test 1";
wordTable1.Rows[1].Cells[0].Paragraphs[0].Text = "Test 2";
wordTable1.Rows[2].Cells[0].Paragraphs[0].Text = "Test 3";
wordTable1.Rows[3].Cells[0].Paragraphs[0].Text = "Test 4";

Assert.True(wordTable1.Alignment == null);

wordTable1.Alignment = TableRowAlignmentValues.Center;

Assert.True(wordTable1.Alignment == TableRowAlignmentValues.Center);

wordTable1.WidthType = TableWidthUnitValues.Pct;
wordTable1.Width = 3000;

wordTable1.Title = "This is a title of the table";
wordTable1.Description = "This is a table showing some features";

Assert.True(wordTable1.Description == "This is a table showing some features");
Assert.True(wordTable1.Title == "This is a title of the table");

Assert.True(wordTable1.AllowTextWrap == false);
Assert.True(wordTable1.Position.VerticalAnchor == null);

wordTable1.AllowTextWrap = true;

Assert.True(wordTable1.AllowTextWrap == true);
Assert.True(wordTable1.Position.VerticalAnchor == VerticalAnchorValues.Text);

Assert.True(wordTable1.AllowOverlap == false);


Assert.True(wordTable1.Position.TableOverlap == null);

wordTable1.AllowOverlap = true;

Assert.True(wordTable1.AllowOverlap == true);
Assert.True(wordTable1.Position.TableOverlap == TableOverlapValues.Overlap);

document.Save(false);
}

using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesAndMoreOptions.docx"))) {
var wordTable1 = document.Tables[0];

// add a cell to 3rd row
WordTableCell cell = new WordTableCell(document, wordTable1, wordTable1.Rows[2]);
cell.Paragraphs[0].Text = "This cell is outside a bit";
cell.TextDirection = TextDirectionValues.TopToBottomLeftToRightRotated;

Assert.True(cell.TextDirection == TextDirectionValues.TopToBottomLeftToRightRotated);
Assert.True(cell.Paragraphs[0].Text == "This cell is outside a bit");

Assert.True(wordTable1.Rows[1].Cells.Count == 4);
Assert.True(wordTable1.Rows[2].Cells.Count == 5);
Assert.True(wordTable1.Rows[1].CellsCount == 4);
Assert.True(wordTable1.Rows[2].Cells[4].Paragraphs[0].Text == "This cell is outside a bit");
Assert.True(wordTable1.Rows[2].Cells[4].TextDirection == TextDirectionValues.TopToBottomLeftToRightRotated);

Assert.True(wordTable1.Alignment == TableRowAlignmentValues.Center);

Assert.True(wordTable1.AllowTextWrap == true);
Assert.True(wordTable1.Position.VerticalAnchor == VerticalAnchorValues.Text);

Assert.True(wordTable1.AllowOverlap == true);
Assert.True(wordTable1.Position.TableOverlap == TableOverlapValues.Overlap);

Assert.True(wordTable1.Description == "This is a table showing some features");
Assert.True(wordTable1.Title == "This is a title of the table");

Assert.True(wordTable1.Position.RightFromText == null);
Assert.True(wordTable1.Position.LeftFromText == null);
Assert.True(wordTable1.Position.TablePositionXAlignment == null);
Assert.True(wordTable1.Position.TablePositionY == null);
Assert.True(wordTable1.Position.HorizontalAnchor == null);


wordTable1.Position.LeftFromText = 100;

wordTable1.Position.RightFromText = 180;


wordTable1.Position.TopFromText = 50;

wordTable1.Position.BottomFromText = 130;

wordTable1.Position.TablePositionXAlignment = HorizontalAlignmentValues.Left;

wordTable1.Position.HorizontalAnchor = HorizontalAnchorValues.Margin;

wordTable1.Position.TablePositionY = 1;

document.Save();
}


using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesAndMoreOptions.docx"))) {
var wordTable1 = document.Tables[0];

Assert.True(wordTable1.Alignment == TableRowAlignmentValues.Center);

Assert.True(wordTable1.AllowTextWrap == true);
Assert.True(wordTable1.Position.VerticalAnchor == VerticalAnchorValues.Text);

Assert.True(wordTable1.AllowOverlap == true);
Assert.True(wordTable1.Position.TableOverlap == TableOverlapValues.Overlap);

Assert.True(wordTable1.Description == "This is a table showing some features");
Assert.True(wordTable1.Title == "This is a title of the table");

Assert.True(wordTable1.Position.RightFromText == 180);
Assert.True(wordTable1.Position.LeftFromText == 100);
Assert.True(wordTable1.Position.TopFromText == 50);
Assert.True(wordTable1.Position.BottomFromText == 130);
Assert.True(wordTable1.Position.TablePositionXAlignment == HorizontalAlignmentValues.Left);
Assert.True(wordTable1.Position.TablePositionY == 1);
Assert.True(wordTable1.Position.HorizontalAnchor == HorizontalAnchorValues.Margin);

Assert.True(wordTable1.Rows[1].Cells.Count == 4);
Assert.True(wordTable1.Rows[2].Cells.Count == 5);
Assert.True(wordTable1.Rows[1].CellsCount == 4);
Assert.True(wordTable1.Rows[2].Cells[4].Paragraphs[0].Text == "This cell is outside a bit");
Assert.True(wordTable1.Rows[2].Cells[4].TextDirection == TextDirectionValues.TopToBottomLeftToRightRotated);

}
}
}
}
Loading

0 comments on commit 72a241b

Please sign in to comment.