Skip to content

Commit

Permalink
Add SetColumnJustification(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuyafujisaki committed Jun 25, 2017
1 parent ae2b1e1 commit 1c09746
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion OpenXmlWordHelper.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.6
VisualStudioVersion = 15.0.26430.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenXmlWordHelper", "OpenXmlWordHelper\OpenXmlWordHelper.csproj", "{7EEA76A5-EB47-42AF-A082-F23A086EBE03}"
EndProject
Expand Down
23 changes: 23 additions & 0 deletions OpenXmlWordHelper/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,28 @@ public static Paragraph CreateNumberingParagraph(int numberId, int levelIndex, s
}
}
}

// Colunn index is zero-based.
public static void SetColumnJustification(MainDocumentPart mdp, int columnIndex, JustificationValues jv)
{
foreach (var tr in mdp.Document.Body.GetFirstChild<Table>().Elements<TableRow>())
{
// If columnIndex = 0, "tr.Elements<TableCell>().ToList()[columnIndex]" can be simplified to "tr.GetFirstChild<TableCell>()".
var p = tr.Elements<TableCell>().ToList()[columnIndex].GetFirstChild<Paragraph>();

if (p.ParagraphProperties == null)
{
p.PrependChild(new ParagraphProperties());
}

if (p.ParagraphProperties.Justification != null)
{
// Ensure that all Justifications are removed though there shouldn't be more than one in theory.
p.ParagraphProperties.RemoveAllChildren<Justification>();
}

p.ParagraphProperties.Justification = new Justification { Val = jv };
}
}
}
}
40 changes: 32 additions & 8 deletions Test1/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Test1
[TestClass]
public class UnitTest1
{
[ClassInitialize]
public static void Initialize(TestContext context)
{
Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}

static void TestRunner(Action<MainDocumentPart> f)
{
var path = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fffffff") + ".docx");
Expand All @@ -29,14 +35,24 @@ static void TestRunner(Action<MainDocumentPart> f)
Process.Start(path);
}

static void TestRunner(string path, Action<MainDocumentPart> f)
{
using (var wd = WordprocessingDocument.Open(path, true))
{
f(wd.MainDocumentPart);
}

Process.Start(path);
}

[TestMethod]
public static void TestAddParagraphPropertiesDefault() => TestRunner(Api.AddParagraphPropertiesDefault);
public void TestAddParagraphPropertiesDefault() => TestRunner(Api.AddParagraphPropertiesDefault);

[TestMethod]
public static void TestCreateWordWithParagraphPropertiesDefault() => TestRunner(Api.ClearHeaderFooter);
public void TestCreateWordWithParagraphPropertiesDefault() => TestRunner(Api.ClearHeaderFooter);

[TestMethod]
public static void TestCreateParagraphWithText()
public void TestCreateParagraphWithText()
{
TestRunner(mdp =>
{
Expand All @@ -46,7 +62,7 @@ public static void TestCreateParagraphWithText()
}

[TestMethod]
public static void TestCreateNumberingParagraphs()
public void TestCreateNumberingParagraphs()
{
TestRunner(mdp =>
{
Expand All @@ -70,7 +86,7 @@ public static void TestCreateNumberingParagraphs()
}

[TestMethod]
public static void TestMergeDocuments()
public void TestMergeDocuments()
{
const string path1 = "Source1.docx";
const string path2 = "Source2.docx";
Expand All @@ -81,7 +97,7 @@ public static void TestMergeDocuments()
}

[TestMethod]
public static void TestMergeDocumentsToNewFile()
public void TestMergeDocumentsToNewFile()
{
const string path1 = "Source1.docx";
const string path2 = "Source2.docx";
Expand All @@ -93,13 +109,21 @@ public static void TestMergeDocumentsToNewFile()
}

[TestMethod]
public static void TestProtectWord()
public void TestProtectWord()
{
const string path = "Sample.docx";
const string path = @"Sample.docx";

Api.ProtectWord(path, "dummy");

Process.Start(path);
}

[TestMethod]
public void TestSetColumnJustification()
{
const string path = @"Sample.docx";

TestRunner(path, mdp => Api.SetColumnJustification(mdp, 2, JustificationValues.Right));
}
}
}
8 changes: 4 additions & 4 deletions Test1/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DocumentFormat.OpenXml" version="2.7.2" targetFramework="net462" />
<package id="MSTest.TestFramework" version="1.1.11" targetFramework="net462" />
<package id="System.IO.FileSystem.Primitives" version="4.0.1" targetFramework="net462" />
<package id="System.IO.Packaging" version="4.0.0" targetFramework="net462" />
<package id="DocumentFormat.OpenXml" version="2.7.2" />
<package id="MSTest.TestFramework" version="1.1.11"/>
<package id="System.IO.FileSystem.Primitives" version="4.0.1" />
<package id="System.IO.Packaging" version="4.0.0"/>
</packages>

0 comments on commit 1c09746

Please sign in to comment.