Skip to content

Commit

Permalink
Add FirstLineOrHangingChars
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsuya Fujisaki committed Oct 3, 2017
1 parent 7a83653 commit f9479d7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 50 deletions.
22 changes: 5 additions & 17 deletions OpenXmlWordHelper/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public static class Equation

}

public static Paragraph CreateParagraphWithText(string s) => new Paragraph(new Run(new Text(s)));

public static void ProtectWord(string path, string password)
{
string CreateSalt()
Expand Down Expand Up @@ -165,9 +163,9 @@ public static AbstractNum CreateAbstractNum(int abstractNumId)

var an = new AbstractNum { AbstractNumberId = abstractNumId };

an.Append(LevelFactory.CreateLevel(NumberFormatValues.Decimal, 720, 360));
an.Append(LevelFactory.CreateLevel(NumberFormatValues.Decimal, 720 * 2, 360));
an.Append(LevelFactory.CreateLevel(NumberFormatValues.Decimal, 720 * 3, 360));
an.AppendChild(LevelFactory.CreateLevel(NumberFormatValues.Decimal, 720, 360));
an.AppendChild(LevelFactory.CreateLevel(NumberFormatValues.Decimal, 720 * 2, 360));
an.AppendChild(LevelFactory.CreateLevel(NumberFormatValues.Decimal, 720 * 3, 360));

return an;
}
Expand Down Expand Up @@ -235,18 +233,8 @@ public static void SetColumnJustification(Table table, int columnIndex, Justific

var p = tc.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 };
OpenXmlElementHelper.SetChildIfNotExists<ParagraphProperties>(p);
OpenXmlElementHelper.SetChild(p.GetFirstChild<ParagraphProperties>(), new Justification { Val = jv });
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions OpenXmlWordHelper/OpenXmlElementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace OpenXmlWordHelper
{
static class OpenXmlElementHelper
public static class OpenXmlElementHelper
{
internal static bool HasChild<T>(OpenXmlElement parent) where T : OpenXmlElement => parent.GetFirstChild<T>() != null;
public static bool HasChild<T>(OpenXmlElement parent) where T : OpenXmlElement => parent.GetFirstChild<T>() != null;

internal static void SetChild<T>(OpenXmlElement parent, T child) where T : OpenXmlElement
public static void SetChild<T>(OpenXmlElement parent, T child) where T : OpenXmlElement
{
parent.RemoveAllChildren<T>();
parent.AppendChild(child);
}

internal static void AppendChildIfNotExists<T>(OpenXmlElement parent) where T : OpenXmlElement, new()
public static void SetChildIfNotExists<T>(OpenXmlElement parent) where T : OpenXmlElement, new()
{
if (!HasChild<T>(parent))
{
Expand Down
71 changes: 50 additions & 21 deletions OpenXmlWordHelper/ParagraphPropertiesHelper.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Collections.Generic;
using System.ComponentModel;
using JustificationValues = DocumentFormat.OpenXml.Wordprocessing.JustificationValues;
using ParagraphProperties = DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties;

namespace OpenXmlWordHelper
{
static class ParagraphPropertiesHelper
public enum FirstLineOrHanging
{
internal static void SetParagraphProperties(ParagraphProperties p, int? leftChars, int? firstLineChars, int? hangingChars, int? spaceBetweenLines, JustificationValues? jv)
FirstLine,
Hanging
}

public class FirstLineOrHangingChars
{
internal readonly FirstLineOrHanging Floh;
internal readonly int Chars;

public FirstLineOrHangingChars(FirstLineOrHanging floh, int chars)
{
this.Floh = floh;
this.Chars = chars;
}
}

public static class ParagraphPropertiesHelper
{
public static void SetParagraphProperties(ParagraphProperties p, int? leftChars, FirstLineOrHangingChars firstLineOrhangingChars, int? spaceBetweenLines, JustificationValues? jv)
{
var oxes = new List<OpenXmlElement>();

if (leftChars != null || firstLineChars != null || hangingChars != null)
if (leftChars != null || firstLineOrhangingChars != null)
{
var indent = new Indentation();

Expand All @@ -21,14 +40,19 @@ internal static void SetParagraphProperties(ParagraphProperties p, int? leftChar
indent.LeftChars = leftChars.Value;
}

if (firstLineChars != null)
if (firstLineOrhangingChars != null)
{
indent.FirstLineChars = firstLineChars.Value;
}

if (hangingChars != null)
{
indent.HangingChars = hangingChars.Value;
switch (firstLineOrhangingChars.Floh)
{
case FirstLineOrHanging.FirstLine:
indent.FirstLineChars = firstLineOrhangingChars.Chars;
break;
case FirstLineOrHanging.Hanging:
indent.HangingChars = firstLineOrhangingChars.Chars;
break;
default:
throw new InvalidEnumArgumentException(firstLineOrhangingChars.Floh.ToString());
}
}

oxes.Add(indent);
Expand All @@ -47,7 +71,7 @@ internal static void SetParagraphProperties(ParagraphProperties p, int? leftChar
OpenXmlElementHelper.SetChild(p, new ParagraphProperties(oxes));
}

internal static void SetIndent(ParagraphProperties pp, int? leftChars, int? firstLineChars, int? hangingChars)
public static void SetIndent(ParagraphProperties pp, int? leftChars, FirstLineOrHangingChars firstLineOrhangingChars)
{
var indent = new Indentation();

Expand All @@ -56,27 +80,32 @@ internal static void SetIndent(ParagraphProperties pp, int? leftChars, int? firs
indent.LeftChars = leftChars.Value;
}

if (firstLineChars != null)
{
indent.FirstLineChars = firstLineChars.Value;
}

if (hangingChars != null)
if (firstLineOrhangingChars != null)
{
indent.HangingChars = hangingChars.Value;
switch (firstLineOrhangingChars.Floh)
{
case FirstLineOrHanging.FirstLine:
indent.FirstLineChars = firstLineOrhangingChars.Chars;
break;
case FirstLineOrHanging.Hanging:
indent.HangingChars = firstLineOrhangingChars.Chars;
break;
default:
throw new InvalidEnumArgumentException(firstLineOrhangingChars.Floh.ToString());
}
}

OpenXmlElementHelper.SetChild(pp, indent);
}

internal static void SetSpacinggBetweenLines(ParagraphProperties pp, int space)
public static void SetSpacinggBetweenLines(ParagraphProperties pp, int space)
{
OpenXmlElementHelper.SetChild(pp, new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Auto, Line = space.ToString() });
}

internal static void SetJustification(ParagraphProperties pp, JustificationValues jv)
public static void SetJustification(ParagraphProperties pp, JustificationValues jv)
{
OpenXmlElementHelper.SetChild(pp, new Justification { Val = jv });
}
}
}
}
21 changes: 13 additions & 8 deletions UnitTestProject1/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,23 @@ static void TestRunner(string path, Action<MainDocumentPart> f)
}

[TestMethod]
public void TestAddParagraphPropertiesDefault() => TestRunner(Api.AddParagraphPropertiesDefault);
public void TestCreateParagraphProperties() =>
TestRunner(mdp =>
{
var pp = new ParagraphProperties();
ParagraphPropertiesHelper.SetParagraphProperties(pp, 10, new FirstLineOrHangingChars(FirstLineOrHanging.FirstLine, 10), 1000, JustificationValues.Both);
var p = new Paragraph();
OpenXmlElementHelper.SetChild(p, pp);
OpenXmlElementHelper.SetChild(p, new Run(new Text("Hello world!")));
OpenXmlElementHelper.SetChild(mdp.Document.Body, p);
});


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

[TestMethod]
public void TestCreateParagraphWithText() =>
TestRunner(mdp =>
{
mdp.Document.Body.AppendChild(Api.CreateParagraphWithText("Hello"));
mdp.Document.Body.AppendChild(Api.CreateParagraphWithText("Word"));
});
public void TestCreateWordWithParagraphPropertiesDefault() => TestRunner(Api.ClearHeaderFooter);

[TestMethod]
public void TestCreateNumberingParagraphs()
Expand Down

0 comments on commit f9479d7

Please sign in to comment.