Skip to content

Commit 3de9e32

Browse files
committed
Create and Load OpenXML shapes
Closes #31 Closes #32
1 parent 5f1b470 commit 3de9e32

File tree

4 files changed

+358
-7
lines changed

4 files changed

+358
-7
lines changed

FileFormat.Words.IElements.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,92 @@ public class Image : IElement
467467
/// </summary>
468468
public int Width { get; set; }
469469
}
470+
471+
/// <summary>
472+
/// Represents a shape element in a Word document.
473+
/// </summary>
474+
public class Shape : IElement
475+
{
476+
/// <summary>
477+
/// Gets the unique identifier of the shape.
478+
/// </summary>
479+
public int ElementId { get; internal set; }
480+
481+
/// <summary>
482+
/// Gets or sets the x position of the shape.
483+
/// </summary>
484+
public int X { get; set; }
485+
486+
/// <summary>
487+
/// Gets or sets the y position of the shape.
488+
/// </summary>
489+
public int Y { get; set; }
490+
491+
/// <summary>
492+
/// Gets or sets the height of the shape.
493+
/// </summary>
494+
public int Height { get; set; }
495+
496+
/// <summary>
497+
/// Gets or sets the width of the shape.
498+
/// </summary>
499+
public int Width { get; set; }
500+
501+
/// <summary>
502+
/// Gets or sets the type of the shape.
503+
/// </summary>
504+
public ShapeType Type { get; set; }
505+
506+
/// <summary>
507+
/// Initializes a new instance of the <see cref="Shape"/> class.
508+
/// </summary>
509+
public Shape()
510+
{
511+
X = 100;
512+
Y = 100;
513+
Width = 200;
514+
Height = 200;
515+
Type = ShapeType.Ellipse;
516+
}
517+
518+
/// <summary>
519+
/// Initializes a new instance of the <see cref="Shape"/> class with specified values.
520+
/// </summary>
521+
/// <param name="x">x position of the shape.</param>
522+
/// <param name="y">y position of the shape.</param>
523+
/// <param name="width">Width of the shape.</param>
524+
/// <param name="height">Height of the shape.</param>
525+
public Shape(int x,int y,int width,int height,ShapeType shapeType)
526+
{
527+
X = x;
528+
Y = y;
529+
Width = width;
530+
Height = height;
531+
Type = shapeType;
532+
}
533+
}
534+
535+
/// <summary>
536+
/// Specifies the type of a shape within the word document.
537+
/// </summary>
538+
public enum ShapeType
539+
{
540+
/// <summary>
541+
/// Ellipse or Oval shape.
542+
/// </summary>
543+
Ellipse,
544+
545+
/// <summary>
546+
/// Diamond shape.
547+
/// </summary>
548+
Diamond,
549+
550+
/// <summary>
551+
/// Hexagone shape.
552+
/// </summary>
553+
Hexagone
554+
}
555+
470556
/// <summary>
471557
/// Represents a table element in a Word document.
472558
/// </summary>

FileFormat.Words.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ public int InsertBefore(IElement newElement, IElement element)
398398
case Image image:
399399
image.ElementId = newElementId;
400400
break;
401+
case Shape shape:
402+
shape.ElementId = newElementId;
403+
break;
401404
}
402405

403406
try
@@ -456,6 +459,9 @@ public int InsertBefore(IElement newElement, int elementId)
456459
case Image image:
457460
image.ElementId = newElementId;
458461
break;
462+
case Shape shape:
463+
shape.ElementId = newElementId;
464+
break;
459465
}
460466

461467
try
@@ -496,6 +502,9 @@ internal int Append(IElement newElement)
496502
case Image image:
497503
image.ElementId = newElementId;
498504
break;
505+
case Shape shape:
506+
shape.ElementId = newElementId;
507+
break;
499508
}
500509

501510
var originalCount = _lstStructure.Count;
@@ -576,6 +585,9 @@ public int InsertAfter(IElement newElement, IElement element)
576585
case Image image:
577586
image.ElementId = newElementId;
578587
break;
588+
case Shape shape:
589+
shape.ElementId = newElementId;
590+
break;
579591
}
580592

581593
try
@@ -633,6 +645,9 @@ public int InsertAfter(IElement newElement, int elementId)
633645
case Image image:
634646
image.ElementId = newElementId;
635647
break;
648+
case Shape shape:
649+
shape.ElementId = newElementId;
650+
break;
636651
}
637652

638653
try
@@ -912,6 +927,10 @@ public class Body
912927
/// </summary>
913928
public List<Image> Images { get; internal set; }
914929
/// <summary>
930+
/// Gets the list of shapes in the body.
931+
/// </summary>
932+
public List<Shape> Shapes { get; internal set; }
933+
/// <summary>
915934
/// Gets the list of sections in the body.
916935
/// </summary>
917936
public List<Section> Sections { get; internal set; }
@@ -927,6 +946,7 @@ public Body(Document doc)
927946
Paragraphs = new List<Paragraph>();
928947
Tables = new List<Table>();
929948
Images = new List<Image>();
949+
Shapes = new List<Shape>();
930950
Sections = new List<Section>();
931951
foreach (var element in doc.GetElements())
932952
{
@@ -945,6 +965,11 @@ public Body(Document doc)
945965
Images.Add((Image)element);
946966
}
947967

968+
if (element is Shape)
969+
{
970+
Shapes.Add((Shape)element);
971+
}
972+
948973
if (element is Section section)
949974
{
950975
Sections.Add(section);

OpenXML.Words.Data.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,18 @@ internal void Insert(FF.IElement newElement, int position, Document doc)
8787
elements.ElementAt(position).InsertBeforeSelf(wpImage);
8888
break;
8989

90+
case FF.Shape ffShape:
91+
var wpShape = _ooxmlDoc.CreateShape(ffShape);
92+
elements.ElementAt(position).InsertBeforeSelf(wpShape);
93+
break;
9094
}
9195

9296
}
9397
catch (Exception ex)
9498
{
9599
staticDoc.MainDocumentPart.Document.Body.RemoveAllChildren();
96100
staticDoc.MainDocumentPart.Document.Body.Append(originalElements);
97-
var errorMessage = ConstructMessage(ex, "Remove OOXML Element(s)");
101+
var errorMessage = ConstructMessage(ex, "Insert OOXML Element(s)");
98102
throw new FileFormatException(errorMessage, ex);
99103
}
100104
}
@@ -137,6 +141,10 @@ internal void Update(FF.IElement newElement, int position, Document doc)
137141
var wpImage = _ooxmlDoc.CreateImage(ffImage, staticDoc.MainDocumentPart);
138142
enumerable1.ElementAt(position).InsertBeforeSelf(wpImage);
139143
break;
144+
case FF.Shape ffShape:
145+
var wpShape = _ooxmlDoc.CreateShape(ffShape);
146+
enumerable1.ElementAt(position).InsertBeforeSelf(wpShape);
147+
break;
140148
}
141149

142150
}
@@ -216,6 +224,11 @@ internal void Append(FF.IElement newElement, Document doc)
216224
if (lastSectionProperties != null) staticDoc.MainDocumentPart.Document.Body.InsertBefore(wpImage, lastSectionProperties);
217225
else staticDoc.MainDocumentPart.Document.Body.Append(wpImage);
218226
break;
227+
case FF.Shape ffShape:
228+
var wpShape = _ooxmlDoc.CreateShape(ffShape);
229+
if (lastSectionProperties != null) staticDoc.MainDocumentPart.Document.Body.InsertBefore(wpShape, lastSectionProperties);
230+
else staticDoc.MainDocumentPart.Document.Body.Append(wpShape);
231+
break;
219232
}
220233

221234
}

0 commit comments

Comments
 (0)