Skip to content

Commit 8cb6309

Browse files
committed
Allignment Code pushed
1 parent 56647b8 commit 8cb6309

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

FileFormat.Words/FileFormat.Words.IElements.cs

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

16+
1617
/// <summary>
1718
/// Represents a paragraph element in a Word document.
1819
/// </summary>
@@ -38,6 +39,11 @@ public class Paragraph : IElement
3839
/// </summary>
3940
public string Style { get; set; }
4041

42+
/// <summary>
43+
/// Gets or Sets Alignment of the word paragraph
44+
/// </summary>
45+
public string Alignment { get; set; }
46+
4147
/// <summary>
4248
/// Initializes a new instance of the <see cref="Paragraph"/> class.
4349
/// </summary>

FileFormat.Words/OpenXML.Words.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ internal class OwDocument
2121
private MemoryStream _ms;
2222
private PKG.MainDocumentPart _mainPart;
2323
private readonly object _lockObject = new object();
24+
public enum ParagraphAlignment
25+
{
26+
Left,
27+
Center,
28+
Right
29+
}
2430
private OwDocument()
2531
{
2632
lock (_lockObject)
@@ -130,6 +136,11 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
130136
var paragraphProperties = new WP.ParagraphProperties();
131137
var paragraphStyleId = new WP.ParagraphStyleId { Val = ffP.Style };
132138
paragraphProperties.Append(paragraphStyleId);
139+
if (Enum.TryParse<ParagraphAlignment>(ffP.Alignment, true, out var alignmentEnum))
140+
{
141+
WP.JustificationValues justificationValue = MapAlignmentToJustification(alignmentEnum);
142+
paragraphProperties.Append(new WP.Justification { Val = justificationValue });
143+
}
133144
wpParagraph.Append(paragraphProperties);
134145
}
135146

@@ -194,6 +205,21 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
194205
}
195206
}
196207

208+
private WP.JustificationValues MapAlignmentToJustification(ParagraphAlignment alignment)
209+
{
210+
switch (alignment)
211+
{
212+
case ParagraphAlignment.Left:
213+
return WP.JustificationValues.Left;
214+
case ParagraphAlignment.Center:
215+
return WP.JustificationValues.Center;
216+
case ParagraphAlignment.Right:
217+
return WP.JustificationValues.Right;
218+
default:
219+
return WP.JustificationValues.Left;
220+
}
221+
}
222+
197223
internal WP.Table CreateTable(FF.Table ffTable)
198224
{
199225
lock (_lockObject)
@@ -458,9 +484,13 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
458484
if (paraStyleId != null)
459485
{
460486
if (paraStyleId.Val != null) ffP.Style = paraStyleId.Val.Value;
461-
}
487+
}
488+
}
489+
var justificationElement = paraProps.Elements<WP.Justification>().FirstOrDefault();
490+
if (justificationElement != null)
491+
{
492+
ffP.Alignment = MapJustificationToAlignment(justificationElement.Val);
462493
}
463-
464494
var runs = wpPara.Elements<WP.Run>();
465495

466496
foreach (var wpR in runs)
@@ -473,7 +503,7 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
473503
{
474504
Text = wpR.InnerText,
475505
FontFamily = wpR.RunProperties?.RunFonts?.Ascii ?? null,
476-
FontSize = fontSize ?? 0, //int.Parse(wpR.RunProperties?.FontSize?.Val ?? null),
506+
FontSize = fontSize ?? 0,
477507
Color = wpR.RunProperties?.Color?.Val ?? null,
478508
Bold = (wpR.RunProperties != null && wpR.RunProperties.Bold != null),
479509
Italic = (wpR.RunProperties != null && wpR.RunProperties.Italic != null),
@@ -492,6 +522,22 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
492522
}
493523
}
494524

525+
private string MapJustificationToAlignment(WP.JustificationValues justificationValue)
526+
{
527+
switch (justificationValue)
528+
{
529+
case WP.JustificationValues.Left:
530+
return "Left";
531+
case WP.JustificationValues.Center:
532+
return "Center";
533+
case WP.JustificationValues.Right:
534+
return "Right";
535+
// Add more cases as needed
536+
default:
537+
return "Left";
538+
}
539+
}
540+
495541
internal FF.Image LoadImage(WP.Drawing drawing, int sequence)
496542
{
497543
lock (_lockObject)

0 commit comments

Comments
 (0)