Skip to content

Commit 2bf54d3

Browse files
committed
Bulleting and Numbering logic sorted
1 parent 75ae549 commit 2bf54d3

File tree

1 file changed

+115
-41
lines changed

1 file changed

+115
-41
lines changed

FileFormat.Words/OpenXML.Words.cs

Lines changed: 115 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private OwDocument()
3333
_mainPart.Document = new WP.Document();
3434
var tmp = new OT.DefaultTemplate();
3535
tmp.CreateMainDocumentPart(_mainPart);
36+
AddNumberingDefinitions(_pkgDocument);
3637
CreateProperties(_pkgDocument);
3738
}
3839
catch (Exception ex)
@@ -139,25 +140,27 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
139140
{
140141
SetIndentation(paragraphProperties, ffP.Indentation);
141142
}
142-
143-
if (ffP.IsNumbered)
144-
{
145-
var numberingProperties = new WP.NumberingProperties
146-
{
147-
NumberingId = new WP.NumberingId { Val = ffP.NumberingId },
148-
NumberingLevelReference = new WP.NumberingLevelReference { Val = ffP.NumberingLevel.Value }
149-
};
150-
paragraphProperties.Append(numberingProperties);
151-
}
152143

153-
if (ffP.IsBullet)
144+
if (ffP.IsNumbered || ffP.IsBullet)
154145
{
155-
var bulletProperties = new WP.NumberingProperties
146+
var numberingProperties = new WP.NumberingProperties();
147+
var numberingId = new WP.NumberingId();
148+
var numberingLevelReference = new WP.NumberingLevelReference();
149+
150+
if (ffP.IsBullet)
156151
{
157-
NumberingId = new WP.NumberingId { Val = 1 },
158-
NumberingLevelReference = new WP.NumberingLevelReference { Val = 0 }
159-
};
160-
paragraphProperties.Append(bulletProperties);
152+
// Assuming '1' is the ID for your bullet list definition in NumberingDefinitionsPart
153+
numberingId.Val = 1; // This value should match the ID of your bullet list definition
154+
numberingLevelReference.Val = ffP.NumberingLevel ?? 0; // Use the specified level or default to 0
155+
}
156+
else if (ffP.IsNumbered)
157+
{
158+
numberingId.Val = ffP.NumberingId <= 1 || ffP.NumberingId == null ? 2 : ffP.NumberingId; // This value should match the ID of your numbered list definition, assuming '2' as an example
159+
numberingLevelReference.Val = ffP.NumberingLevel ?? 0;
160+
}
161+
162+
numberingProperties.Append(numberingId, numberingLevelReference);
163+
paragraphProperties.Append(numberingProperties);
161164
}
162165
wpParagraph.Append(paragraphProperties);
163166
}
@@ -223,6 +226,48 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
223226
}
224227
}
225228

229+
internal void AddNumberingDefinitions(PKG.WordprocessingDocument pkgDocument)
230+
{
231+
PKG.NumberingDefinitionsPart numberingPart = pkgDocument.MainDocumentPart.NumberingDefinitionsPart;
232+
if (numberingPart == null)
233+
{
234+
numberingPart = pkgDocument.MainDocumentPart.AddNewPart<PKG.NumberingDefinitionsPart>();
235+
}
236+
237+
WP.Numbering numbering = new WP.Numbering();
238+
239+
WP.AbstractNum abstractNumBulleted = new WP.AbstractNum() { AbstractNumberId = 1 };
240+
abstractNumBulleted.Append(new WP.Level(
241+
new WP.NumberingFormat() { Val = WP.NumberFormatValues.Bullet },
242+
new WP.LevelText() { Val = "•" },
243+
new WP.LevelJustification() { Val = WP.LevelJustificationValues.Left }
244+
)
245+
{ LevelIndex = 0 });
246+
247+
WP.AbstractNum abstractNumNumbered = new WP.AbstractNum() { AbstractNumberId = 2 };
248+
abstractNumNumbered.Append(new WP.Level(
249+
new WP.StartNumberingValue() { Val = 1 },
250+
new WP.NumberingFormat() { Val = WP.NumberFormatValues.Decimal },
251+
new WP.LevelText() { Val = "%1." },
252+
new WP.LevelJustification() { Val = WP.LevelJustificationValues.Left }
253+
)
254+
{ LevelIndex = 0 });
255+
256+
numbering.Append(abstractNumBulleted);
257+
numbering.Append(abstractNumNumbered);
258+
259+
WP.NumberingInstance numInstanceBulleted = new WP.NumberingInstance() { NumberID = 1 };
260+
numInstanceBulleted.Append(new WP.AbstractNumId() { Val = abstractNumBulleted.AbstractNumberId });
261+
262+
WP.NumberingInstance numInstanceNumbered = new WP.NumberingInstance() { NumberID = 2 };
263+
numInstanceNumbered.Append(new WP.AbstractNumId() { Val = abstractNumNumbered.AbstractNumberId });
264+
265+
numbering.Append(numInstanceBulleted);
266+
numbering.Append(numInstanceNumbered);
267+
268+
numberingPart.Numbering = numbering;
269+
}
270+
226271
private void SetIndentation(WP.ParagraphProperties paragraphProperties, FF.Indentation ffIndentation)
227272
{
228273
var indentation = new WP.Indentation();
@@ -531,43 +576,72 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id)
531576
if (paraStyleId != null)
532577
{
533578
if (paraStyleId.Val != null) ffP.Style = paraStyleId.Val.Value;
579+
}
580+
}
581+
var numberingProperties = wpPara.ParagraphProperties?.NumberingProperties;
582+
if (numberingProperties != null)
583+
{
584+
var numIdVal = numberingProperties.NumberingId?.Val;
585+
var levelVal = numberingProperties.NumberingLevelReference?.Val;
534586

535-
if (IsBulletStyle(paraStyleId.Val.Value))
587+
// Check if there's a valid numbering id and level reference
588+
if (numIdVal.HasValue && levelVal.HasValue)
589+
{
590+
// Get the numbering part from the document
591+
var numberingPart = _pkgDocument.MainDocumentPart.NumberingDefinitionsPart;
592+
if (numberingPart != null)
536593
{
537-
ffP.IsBullet = true;
594+
// Look for the AbstractNum that matches the numIdVal
595+
var abstractNum = numberingPart.Numbering.Elements<WP.AbstractNum>()
596+
.FirstOrDefault(a => a.AbstractNumberId.Value == numIdVal.Value);
597+
598+
if (abstractNum != null)
599+
{
600+
// Get the level corresponding to the levelVal
601+
var level = abstractNum.Elements<WP.Level>()
602+
.FirstOrDefault(l => l.LevelIndex.Value == levelVal.Value);
603+
604+
// If the level's numbering format is bullet, set IsBullet to true
605+
if (level != null && level.NumberingFormat != null && level.NumberingFormat.Val.Value == WP.NumberFormatValues.Bullet)
606+
{
607+
ffP.IsBullet = true;
608+
ffP.NumberingLevel = levelVal.Value;
609+
}
610+
// If the level's numbering format is not bullet, it's a numbered list
611+
else if (level != null)
612+
{
613+
ffP.IsNumbered = true;
614+
ffP.NumberingLevel = levelVal.Value;
615+
ffP.NumberingId = numIdVal.Value;
616+
}
617+
}
538618
}
539619
}
540-
var numberingProperties = paraProps.Elements<WP.NumberingProperties>().FirstOrDefault();
541-
if (numberingProperties != null)
542-
{
543-
ffP.IsNumbered = true;
544-
ffP.NumberingId = numberingProperties.NumberingId?.Val ?? 0;
545-
ffP.NumberingLevel = numberingProperties.NumberingLevelReference?.Val ?? 0;
546-
}
547620
}
548621
var justificationElement = paraProps.Elements<WP.Justification>().FirstOrDefault();
549622
if (justificationElement != null)
550623
{
551624
ffP.Alignment = MapJustificationToAlignment(justificationElement.Val);
552625
}
553626
var Indentation = paraProps.Elements<WP.Indentation>().FirstOrDefault();
554-
if (Indentation.Left != null)
555-
{
556-
ffP.Indentation.Left = int.Parse(Indentation.Left);
557-
}
558-
if (Indentation.Right != null)
559-
{
560-
ffP.Indentation.Right = int.Parse(Indentation.Right);
561-
}
562-
if (Indentation.Hanging != null)
563-
{
564-
ffP.Indentation.Hanging = int.Parse(Indentation.Hanging);
565-
}
566-
if (Indentation.FirstLine != null)
567-
{
568-
ffP.Indentation.FirstLine = int.Parse(Indentation.FirstLine);
627+
if (Indentation != null) {
628+
if (Indentation.Left != null)
629+
{
630+
ffP.Indentation.Left = int.Parse(Indentation.Left);
631+
}
632+
if (Indentation.Right != null)
633+
{
634+
ffP.Indentation.Right = int.Parse(Indentation.Right);
635+
}
636+
if (Indentation.Hanging != null)
637+
{
638+
ffP.Indentation.Hanging = int.Parse(Indentation.Hanging);
639+
}
640+
if (Indentation.FirstLine != null)
641+
{
642+
ffP.Indentation.FirstLine = int.Parse(Indentation.FirstLine);
643+
}
569644
}
570-
571645
var runs = wpPara.Elements<WP.Run>();
572646

573647
foreach (var wpR in runs)

0 commit comments

Comments
 (0)