Skip to content

Commit 6e0c295

Browse files
authored
Merge pull request #10 from Hanzalazia131/ParagraphAttributesFeatrue
Allignment Code pushed
2 parents 3628e07 + 8397f0c commit 6e0c295

File tree

2 files changed

+183
-6
lines changed

2 files changed

+183
-6
lines changed

FileFormat.Words/FileFormat.Words.IElements.cs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ 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+
}
23+
1624
/// <summary>
1725
/// Represents a paragraph element in a Word document.
1826
/// </summary>
@@ -38,13 +46,48 @@ public class Paragraph : IElement
3846
/// </summary>
3947
public string Style { get; set; }
4048

49+
/// <summary>
50+
/// Gets or Sets Alignment of the word paragraph
51+
/// </summary>
52+
public string Alignment { get; set; }
53+
54+
/// <summary>
55+
/// Gets or Sets Indentation of the word paragraph
56+
/// </summary>
57+
public Indentation Indentation { get; set; }
58+
59+
/// <summary>
60+
/// Gets or sets the numbering ID for the paragraph.
61+
/// </summary>
62+
public int? NumberingId { get; set; }
63+
64+
/// <summary>
65+
/// Gets or sets the numbering level for the paragraph.
66+
/// </summary>
67+
public int? NumberingLevel { get; set; }
68+
69+
/// <summary>
70+
/// Gets or sets whether the paragraph has bullet points.
71+
/// </summary>
72+
public bool IsBullet { get; set; }
73+
74+
/// <summary>
75+
/// Gets or sets whether the paragraph has numbering.
76+
/// </summary>
77+
public bool IsNumbered { get; set; }
78+
4179
/// <summary>
4280
/// Initializes a new instance of the <see cref="Paragraph"/> class.
4381
/// </summary>
4482
public Paragraph()
4583
{
4684
Runs = new List<Run>();
4785
Style = "Normal";
86+
Indentation = new Indentation();
87+
NumberingId = null;
88+
NumberingLevel = null;
89+
IsBullet = false;
90+
IsNumbered = false;
4891
UpdateText(); // Initialize the Text property
4992
}
5093

@@ -563,8 +606,4 @@ public ElementStyles()
563606
}
564607
}
565608

566-
}
567-
568-
569-
570-
609+
}

FileFormat.Words/OpenXML.Words.cs

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ 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+
Justify
30+
}
2431
private OwDocument()
2532
{
2633
lock (_lockObject)
@@ -130,6 +137,37 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
130137
var paragraphProperties = new WP.ParagraphProperties();
131138
var paragraphStyleId = new WP.ParagraphStyleId { Val = ffP.Style };
132139
paragraphProperties.Append(paragraphStyleId);
140+
141+
if (Enum.TryParse<ParagraphAlignment>(ffP.Alignment, true, out var alignmentEnum))
142+
{
143+
WP.JustificationValues justificationValue = MapAlignmentToJustification(alignmentEnum);
144+
paragraphProperties.Append(new WP.Justification { Val = justificationValue });
145+
}
146+
147+
if (ffP.Indentation != null)
148+
{
149+
SetIndentation(paragraphProperties, ffP.Indentation);
150+
}
151+
152+
if (ffP.IsNumbered)
153+
{
154+
var numberingProperties = new WP.NumberingProperties
155+
{
156+
NumberingId = new WP.NumberingId { Val = ffP.NumberingId },
157+
NumberingLevelReference = new WP.NumberingLevelReference { Val = ffP.NumberingLevel.Value }
158+
};
159+
paragraphProperties.Append(numberingProperties);
160+
}
161+
162+
if (ffP.IsBullet)
163+
{
164+
var bulletProperties = new WP.NumberingProperties
165+
{
166+
NumberingId = new WP.NumberingId { Val = 1 },
167+
NumberingLevelReference = new WP.NumberingLevelReference { Val = 0 }
168+
};
169+
paragraphProperties.Append(bulletProperties);
170+
}
133171
wpParagraph.Append(paragraphProperties);
134172
}
135173

@@ -194,6 +232,50 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
194232
}
195233
}
196234

235+
private void SetIndentation(WP.ParagraphProperties paragraphProperties, FF.Indentation ffIndentation)
236+
{
237+
var indentation = new WP.Indentation();
238+
239+
if (ffIndentation.Left > 0)
240+
{
241+
indentation.Left = (ffIndentation.Left * 1440).ToString();
242+
}
243+
244+
if (ffIndentation.Right > 0)
245+
{
246+
indentation.Right = (ffIndentation.Right * 1440).ToString();
247+
}
248+
249+
if (ffIndentation.FirstLine > 0)
250+
{
251+
indentation.FirstLine = (ffIndentation.FirstLine * 1440).ToString();
252+
}
253+
254+
if (ffIndentation.Hanging > 0)
255+
{
256+
indentation.Hanging = (ffIndentation.Hanging * 1440).ToString();
257+
}
258+
259+
paragraphProperties.Append(indentation);
260+
}
261+
262+
private WP.JustificationValues MapAlignmentToJustification(ParagraphAlignment alignment)
263+
{
264+
switch (alignment)
265+
{
266+
case ParagraphAlignment.Left:
267+
return WP.JustificationValues.Left;
268+
case ParagraphAlignment.Center:
269+
return WP.JustificationValues.Center;
270+
case ParagraphAlignment.Right:
271+
return WP.JustificationValues.Right;
272+
case ParagraphAlignment.Justify:
273+
return WP.JustificationValues.Both;
274+
default:
275+
return WP.JustificationValues.Left;
276+
}
277+
}
278+
197279
internal WP.Table CreateTable(FF.Table ffTable)
198280
{
199281
lock (_lockObject)
@@ -458,7 +540,41 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
458540
if (paraStyleId != null)
459541
{
460542
if (paraStyleId.Val != null) ffP.Style = paraStyleId.Val.Value;
543+
544+
if (IsBulletStyle(paraStyleId.Val.Value))
545+
{
546+
ffP.IsBullet = true;
547+
}
461548
}
549+
var numberingProperties = paraProps.Elements<WP.NumberingProperties>().FirstOrDefault();
550+
if (numberingProperties != null)
551+
{
552+
ffP.IsNumbered = true;
553+
ffP.NumberingId = numberingProperties.NumberingId?.Val ?? 0;
554+
ffP.NumberingLevel = numberingProperties.NumberingLevelReference?.Val ?? 0;
555+
}
556+
}
557+
var justificationElement = paraProps.Elements<WP.Justification>().FirstOrDefault();
558+
if (justificationElement != null)
559+
{
560+
ffP.Alignment = MapJustificationToAlignment(justificationElement.Val);
561+
}
562+
var Indentation = paraProps.Elements<WP.Indentation>().FirstOrDefault();
563+
if (Indentation.Left != null)
564+
{
565+
ffP.Indentation.Left = int.Parse(Indentation.Left);
566+
}
567+
if (Indentation.Right != null)
568+
{
569+
ffP.Indentation.Right = int.Parse(Indentation.Right);
570+
}
571+
if (Indentation.Hanging != null)
572+
{
573+
ffP.Indentation.Hanging = int.Parse(Indentation.Hanging);
574+
}
575+
if (Indentation.FirstLine != null)
576+
{
577+
ffP.Indentation.FirstLine = int.Parse(Indentation.FirstLine);
462578
}
463579

464580
var runs = wpPara.Elements<WP.Run>();
@@ -473,7 +589,7 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
473589
{
474590
Text = wpR.InnerText,
475591
FontFamily = wpR.RunProperties?.RunFonts?.Ascii ?? null,
476-
FontSize = fontSize ?? 0, //int.Parse(wpR.RunProperties?.FontSize?.Val ?? null),
592+
FontSize = fontSize ?? 0,
477593
Color = wpR.RunProperties?.Color?.Val ?? null,
478594
Bold = (wpR.RunProperties != null && wpR.RunProperties.Bold != null),
479595
Italic = (wpR.RunProperties != null && wpR.RunProperties.Italic != null),
@@ -492,6 +608,28 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
492608
}
493609
}
494610

611+
private bool IsBulletStyle(string styleId)
612+
{
613+
return styleId == "BulletStyle";
614+
}
615+
616+
private string MapJustificationToAlignment(WP.JustificationValues justificationValue)
617+
{
618+
switch (justificationValue)
619+
{
620+
case WP.JustificationValues.Left:
621+
return "Left";
622+
case WP.JustificationValues.Center:
623+
return "Center";
624+
case WP.JustificationValues.Right:
625+
return "Right";
626+
case WP.JustificationValues.Both:
627+
return "Justify";
628+
default:
629+
return "Left";
630+
}
631+
}
632+
495633
internal FF.Image LoadImage(WP.Drawing drawing, int sequence)
496634
{
497635
lock (_lockObject)

0 commit comments

Comments
 (0)