Skip to content

Create and Load OpenXML shapes #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions FileFormat.Words.IElements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,92 @@ public class Image : IElement
/// </summary>
public int Width { get; set; }
}

/// <summary>
/// Represents a shape element in a Word document.
/// </summary>
public class Shape : IElement
{
/// <summary>
/// Gets the unique identifier of the shape.
/// </summary>
public int ElementId { get; internal set; }

/// <summary>
/// Gets or sets the x position of the shape.
/// </summary>
public int X { get; set; }

/// <summary>
/// Gets or sets the y position of the shape.
/// </summary>
public int Y { get; set; }

/// <summary>
/// Gets or sets the height of the shape.
/// </summary>
public int Height { get; set; }

/// <summary>
/// Gets or sets the width of the shape.
/// </summary>
public int Width { get; set; }

/// <summary>
/// Gets or sets the type of the shape.
/// </summary>
public ShapeType Type { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Shape"/> class.
/// </summary>
public Shape()
{
X = 100;
Y = 100;
Width = 200;
Height = 200;
Type = ShapeType.Ellipse;
}

/// <summary>
/// Initializes a new instance of the <see cref="Shape"/> class with specified values.
/// </summary>
/// <param name="x">x position of the shape.</param>
/// <param name="y">y position of the shape.</param>
/// <param name="width">Width of the shape.</param>
/// <param name="height">Height of the shape.</param>
public Shape(int x,int y,int width,int height,ShapeType shapeType)
{
X = x;
Y = y;
Width = width;
Height = height;
Type = shapeType;
}
}

/// <summary>
/// Specifies the type of a shape within the word document.
/// </summary>
public enum ShapeType
{
/// <summary>
/// Ellipse or Oval shape.
/// </summary>
Ellipse,

/// <summary>
/// Diamond shape.
/// </summary>
Diamond,

/// <summary>
/// Hexagone shape.
/// </summary>
Hexagone
}

/// <summary>
/// Represents a table element in a Word document.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions FileFormat.Words.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ public int InsertBefore(IElement newElement, IElement element)
case Image image:
image.ElementId = newElementId;
break;
case Shape shape:
shape.ElementId = newElementId;
break;
}

try
Expand Down Expand Up @@ -456,6 +459,9 @@ public int InsertBefore(IElement newElement, int elementId)
case Image image:
image.ElementId = newElementId;
break;
case Shape shape:
shape.ElementId = newElementId;
break;
}

try
Expand Down Expand Up @@ -496,6 +502,9 @@ internal int Append(IElement newElement)
case Image image:
image.ElementId = newElementId;
break;
case Shape shape:
shape.ElementId = newElementId;
break;
}

var originalCount = _lstStructure.Count;
Expand Down Expand Up @@ -576,6 +585,9 @@ public int InsertAfter(IElement newElement, IElement element)
case Image image:
image.ElementId = newElementId;
break;
case Shape shape:
shape.ElementId = newElementId;
break;
}

try
Expand Down Expand Up @@ -633,6 +645,9 @@ public int InsertAfter(IElement newElement, int elementId)
case Image image:
image.ElementId = newElementId;
break;
case Shape shape:
shape.ElementId = newElementId;
break;
}

try
Expand Down Expand Up @@ -912,6 +927,10 @@ public class Body
/// </summary>
public List<Image> Images { get; internal set; }
/// <summary>
/// Gets the list of shapes in the body.
/// </summary>
public List<Shape> Shapes { get; internal set; }
/// <summary>
/// Gets the list of sections in the body.
/// </summary>
public List<Section> Sections { get; internal set; }
Expand All @@ -927,6 +946,7 @@ public Body(Document doc)
Paragraphs = new List<Paragraph>();
Tables = new List<Table>();
Images = new List<Image>();
Shapes = new List<Shape>();
Sections = new List<Section>();
foreach (var element in doc.GetElements())
{
Expand All @@ -945,6 +965,11 @@ public Body(Document doc)
Images.Add((Image)element);
}

if (element is Shape)
{
Shapes.Add((Shape)element);
}

if (element is Section section)
{
Sections.Add(section);
Expand Down
15 changes: 14 additions & 1 deletion OpenXML.Words.Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ internal void Insert(FF.IElement newElement, int position, Document doc)
elements.ElementAt(position).InsertBeforeSelf(wpImage);
break;

case FF.Shape ffShape:
var wpShape = _ooxmlDoc.CreateShape(ffShape);
elements.ElementAt(position).InsertBeforeSelf(wpShape);
break;
}

}
catch (Exception ex)
{
staticDoc.MainDocumentPart.Document.Body.RemoveAllChildren();
staticDoc.MainDocumentPart.Document.Body.Append(originalElements);
var errorMessage = ConstructMessage(ex, "Remove OOXML Element(s)");
var errorMessage = ConstructMessage(ex, "Insert OOXML Element(s)");
throw new FileFormatException(errorMessage, ex);
}
}
Expand Down Expand Up @@ -137,6 +141,10 @@ internal void Update(FF.IElement newElement, int position, Document doc)
var wpImage = _ooxmlDoc.CreateImage(ffImage, staticDoc.MainDocumentPart);
enumerable1.ElementAt(position).InsertBeforeSelf(wpImage);
break;
case FF.Shape ffShape:
var wpShape = _ooxmlDoc.CreateShape(ffShape);
enumerable1.ElementAt(position).InsertBeforeSelf(wpShape);
break;
}

}
Expand Down Expand Up @@ -216,6 +224,11 @@ internal void Append(FF.IElement newElement, Document doc)
if (lastSectionProperties != null) staticDoc.MainDocumentPart.Document.Body.InsertBefore(wpImage, lastSectionProperties);
else staticDoc.MainDocumentPart.Document.Body.Append(wpImage);
break;
case FF.Shape ffShape:
var wpShape = _ooxmlDoc.CreateShape(ffShape);
if (lastSectionProperties != null) staticDoc.MainDocumentPart.Document.Body.InsertBefore(wpShape, lastSectionProperties);
else staticDoc.MainDocumentPart.Document.Body.Append(wpShape);
break;
}

}
Expand Down
Loading