Skip to content

Commit 5af745b

Browse files
committed
Indentation Code Pushed
1 parent 07afb42 commit 5af745b

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

FileFormat.Words/FileFormat.Words.IElements.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public interface IElement
1313
int ElementId { get; }
1414
}
1515

16+
public class Indentation
17+
{
18+
public double Left { get; set; }
19+
public double Right { get; set; }
20+
public double FirstLine { get; set; }
21+
public double Hanging { get; set; }
22+
}
1623

1724
/// <summary>
1825
/// Represents a paragraph element in a Word document.
@@ -44,13 +51,19 @@ public class Paragraph : IElement
4451
/// </summary>
4552
public string Alignment { get; set; }
4653

54+
/// <summary>
55+
/// Gets or Sets Indentation of the word paragraph
56+
/// </summary>
57+
public Indentation Indentation { get; set; }
58+
4759
/// <summary>
4860
/// Initializes a new instance of the <see cref="Paragraph"/> class.
4961
/// </summary>
5062
public Paragraph()
5163
{
5264
Runs = new List<Run>();
5365
Style = "Normal";
66+
Indentation = new Indentation();
5467
UpdateText(); // Initialize the Text property
5568
}
5669

@@ -569,8 +582,4 @@ public ElementStyles()
569582
}
570583
}
571584

572-
}
573-
574-
575-
576-
585+
}

FileFormat.Words/OpenXML.Words.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,14 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
138138
var paragraphStyleId = new WP.ParagraphStyleId { Val = ffP.Style };
139139
paragraphProperties.Append(paragraphStyleId);
140140
if (Enum.TryParse<ParagraphAlignment>(ffP.Alignment, true, out var alignmentEnum))
141-
{
141+
{
142142
WP.JustificationValues justificationValue = MapAlignmentToJustification(alignmentEnum);
143143
paragraphProperties.Append(new WP.Justification { Val = justificationValue });
144144
}
145+
if (ffP.Indentation != null)
146+
{
147+
SetIndentation(paragraphProperties, ffP.Indentation);
148+
}
145149
wpParagraph.Append(paragraphProperties);
146150
}
147151

@@ -206,6 +210,33 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
206210
}
207211
}
208212

213+
private void SetIndentation(WP.ParagraphProperties paragraphProperties, FF.Indentation ffIndentation)
214+
{
215+
var indentation = new WP.Indentation();
216+
217+
if (ffIndentation.Left > 0)
218+
{
219+
indentation.Left = (ffIndentation.Left * 1440).ToString();
220+
}
221+
222+
if (ffIndentation.Right > 0)
223+
{
224+
indentation.Right = (ffIndentation.Right * 1440).ToString();
225+
}
226+
227+
if (ffIndentation.FirstLine > 0)
228+
{
229+
indentation.FirstLine = (ffIndentation.FirstLine * 1440).ToString();
230+
}
231+
232+
if (ffIndentation.Hanging > 0)
233+
{
234+
indentation.Hanging = (ffIndentation.FirstLine * 1440).ToString();
235+
}
236+
237+
paragraphProperties.Append(indentation);
238+
}
239+
209240
private WP.JustificationValues MapAlignmentToJustification(ParagraphAlignment alignment)
210241
{
211242
switch (alignment)
@@ -494,6 +525,14 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
494525
{
495526
ffP.Alignment = MapJustificationToAlignment(justificationElement.Val);
496527
}
528+
var Indentation = paraProps.Elements<WP.Indentation>().FirstOrDefault();
529+
if (Indentation != null)
530+
{
531+
ffP.Indentation.Left = int.Parse(Indentation.Left);
532+
ffP.Indentation.Right = int.Parse(Indentation.Right);
533+
ffP.Indentation.Hanging = int.Parse(Indentation.Hanging);
534+
ffP.Indentation.FirstLine = int.Parse(Indentation.FirstLine);
535+
}
497536
var runs = wpPara.Elements<WP.Run>();
498537

499538
foreach (var wpR in runs)
@@ -534,9 +573,9 @@ private string MapJustificationToAlignment(WP.JustificationValues justificationV
534573
case WP.JustificationValues.Center:
535574
return "Center";
536575
case WP.JustificationValues.Right:
537-
return "Right";
538-
case WP.JustificationValues.Both
539-
return "Justify"
576+
return "Right";
577+
case WP.JustificationValues.Both:
578+
return "Justify";
540579
default:
541580
return "Left";
542581
}

0 commit comments

Comments
 (0)