From e3411d880080db974c7ef4a02705340304e63522 Mon Sep 17 00:00:00 2001 From: Suresh-03 Date: Wed, 16 Jul 2025 13:59:16 +0530 Subject: [PATCH] Alignment Fix, Anchor Configuration, Null Reference When Getting Circle Shapes Fix --- .gitignore | 3 + FileFormat.Slides-Test/Program.cs | 152 +++--- FileFormat.Slides.Common/Constants.cs | 8 + FileFormat.Slides.Common/Enumerations.cs | 31 +- .../FileFormat.Slides.Common.csproj | 8 +- FileFormat.Slides.Facade/SlideFacade.cs | 413 +++++++++-------- FileFormat.Slides.Facade/TextShapeFacade.cs | 431 ++++++++++-------- FileFormat.Slides/FileFormat.Slides.csproj | 2 +- FileFormat.Slides/Presentation.cs | 114 ++--- FileFormat.Slides/Slide.cs | 325 +++++++------ FileFormat.Slides/TextShape.cs | 66 +-- 11 files changed, 814 insertions(+), 739 deletions(-) create mode 100644 .gitignore create mode 100644 FileFormat.Slides.Common/Constants.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f677870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin +obj +.vs \ No newline at end of file diff --git a/FileFormat.Slides-Test/Program.cs b/FileFormat.Slides-Test/Program.cs index 826cd3c..3839776 100644 --- a/FileFormat.Slides-Test/Program.cs +++ b/FileFormat.Slides-Test/Program.cs @@ -1,81 +1,91 @@ -using DocumentFormat.OpenXml; -using PKG = DocumentFormat.OpenXml.Packaging; -using P = DocumentFormat.OpenXml.Presentation; -using System; -using GeneratedCode; + using FileFormat.Slides; -using System.Collections.Generic; using FileFormat.Slides.Common; -//using DocumentFormat.OpenXml.Presentation; +using System.Collections.Generic; + class Program -{ - static void Main() { - /* Create new Presentation - Presentation presentation = Presentation.Create("D:\\AsposeSampleResults\\test2.pptx"); - TextShape shape = new TextShape(); - shape.Text = "Title: Here is my first title From FF"; - shape.TextColor = "980078"; - shape.FontFamily = "Baguet Script"; - TextShape shape2 = new TextShape(); - shape2.Text = "Body : Here is my first title From FF"; - shape2.FontFamily = "BIZ UDGothic"; - shape2.FontSize = 3000; - shape2.Y = Utility.EmuToPixels(2499619); - // First slide - Slide slide = new Slide(); - slide.AddTextShapes(shape); - slide.AddTextShapes(shape2); - // 2nd slide - Slide slide1 = new Slide(); - slide1.AddTextShapes(shape); - slide1.AddTextShapes(shape2); - // Adding slides - presentation.AppendSlide(slide); - presentation.AppendSlide(slide1); - presentation.Save();*/ + public const string PPT_FILE_PATH = Constants.OUTPUT_DIR + Constants.PPT_FILE; + static void Main() + { + /* Create new Presentation*/ /* - Presentation presentation = Presentation.Create("D:\\AsposeSampleResults\\test2.pptx"); + Presentation presentation = Presentation.Create(PPT_FILE_PATH); + TextShape shape = new TextShape(); + shape.Text = "Title: Here is my first title From FF"; + shape.TextColor = "980078"; + shape.FontFamily = "Baguet Script"; + shape.Alignment = FileFormat.Slides.Common.Enumerations.TextAlignment.Left; + shape.Anchor = FileFormat.Slides.Common.Enumerations.TextAnchor.Top; + TextShape shape2 = new TextShape(); + shape2.Text = "Body : Here is my first title From FF"; + shape2.FontFamily = "BIZ UDGothic"; + shape2.FontSize = 30; + shape2.Alignment = FileFormat.Slides.Common.Enumerations.TextAlignment.Right; + shape2.Y = FileFormat.Slides.Common.Utility.EmuToPixels(2499619); + shape2.Anchor = FileFormat.Slides.Common.Enumerations.TextAnchor.Bottom; + + // First slide + Slide slide = new Slide(); + slide.AddTextShapes(shape); + slide.AddTextShapes(shape2); + // 2nd slide + Slide slide1 = new Slide(); + slide1.AddTextShapes(shape); + slide1.AddTextShapes(shape2); + // Adding slides + presentation.AppendSlide(slide); + presentation.AppendSlide(slide1); + presentation.Save(); + */ + /* + Presentation presentation = Presentation.Create(PPT_FILE_PATH); TextShape shape = new TextShape(); shape.Text = "Title: Here is my first title From FF"; shape.BackgroundColor = "5f7200"; + shape.Alignment = FileFormat.Slides.Common.Enumerations.TextAlignment.Left; + shape.Anchor = FileFormat.Slides.Common.Enumerations.TextAnchor.Top; shape.FontSize = 80; - shape.TextColor = "980078"; + shape.TextColor = "ffffff"; shape.FontFamily = "Baguet Script"; TextShape shape2 = new TextShape(); shape2.BackgroundColor = "ff7f90"; List TextSegments = new List(); - TextSegments.Add(new TextSegment{Color= "980078", FontSize = 70, FontFamily = "Calibri", Text = "Body:" }.create()); - TextSegments.Add(new TextSegment{ Color = "000000", FontSize = 32, FontFamily = "Baguet Script", Text = " Here is my text Segment" }.create()); - + TextSegments.Add(new TextSegment { Color = "980078", FontSize = 70, FontFamily = "Calibri", Text = "Body:" }.create()); + TextSegments.Add(new TextSegment { Color = "ffffff", FontSize = 32, FontFamily = "Baguet Script", Text = " Here is my text Segment" }.create()); + shape2.Y = Utility.EmuToPixels(3499619); + shape2.Alignment = FileFormat.Slides.Common.Enumerations.TextAlignment.Right; + shape2.Anchor = FileFormat.Slides.Common.Enumerations.TextAnchor.Bottom; // First slide Slide slide = new Slide(); slide.AddTextShapes(shape); slide.AddTextShapes(shape2, TextSegments); - + // Adding slides presentation.AppendSlide(slide); - - presentation.Save();*/ - /* Open and update a PPTX file - Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\sample.pptx"); + presentation.Save(); + */ + + ///* + //Open and update a PPTX file + Presentation presentation = Presentation.Open(PPT_FILE_PATH); var slides = presentation.GetSlides(); - var slide = slides[3]; + var slide = slides[0]; List shapes = slide.GetTextShapesByText("PRESENTATION"); - var shape = slide.TextShapes[1]; + var shape = slide.TextShapes[0]; //shape.X = 100000; //shape.Y = 100000; - shape.Alignment= FileFormat.Slides.Common.Enumerations.TextAlignment.Left; - shape.Text = "Muhammad Umar"; + shape.Alignment = FileFormat.Slides.Common.Enumerations.TextAlignment.Center; + shape.Text = "Test Update Text"; shape.Update(); presentation.Save(); - */ + //*/ /* * Remove a slide from presentation. @@ -523,32 +533,32 @@ static void Main() presentation.AppendSlide(slide); presentation.Save();*/ - Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\draw.pptx"); + //Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\draw.pptx"); + //Slide slide = new Slide(); + + //Trapezoid trapezoid = new Trapezoid(); + //trapezoid.Animation = FileFormat.Slides.Common.Enumerations.AnimationType.FlyIn; + //trapezoid.Width = 300.0; + //trapezoid.Height = 300.0; + //trapezoid.X = 300.0; + //trapezoid.Y = 300.0; + //slide.DrawTrapezoid(trapezoid); + //presentation.AppendSlide(slide); + //presentation.Save(); + + /* Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\draw.pptx"); Slide slide = new Slide(); - Trapezoid trapezoid = new Trapezoid(); - trapezoid.Animation = FileFormat.Slides.Common.Enumerations.AnimationType.FlyIn; - trapezoid.Width = 300.0; - trapezoid.Height = 300.0; - trapezoid.X = 300.0; - trapezoid.Y = 300.0; - slide.DrawTrapezoid(trapezoid); + Pie pie = new Pie(); + pie.Animation = FileFormat.Slides.Common.Enumerations.AnimationType.FlyIn; + pie.Width = 300.0; + pie.Height = 300.0; + pie.X = 300.0; + pie.Y = 300.0; + slide.DrawPie(pie); presentation.AppendSlide(slide); - presentation.Save(); - - /* Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\draw.pptx"); - Slide slide = new Slide(); - - Pie pie = new Pie(); - pie.Animation = FileFormat.Slides.Common.Enumerations.AnimationType.FlyIn; - pie.Width = 300.0; - pie.Height = 300.0; - pie.X = 300.0; - pie.Y = 300.0; - slide.DrawPie(pie); - presentation.AppendSlide(slide); - presentation.Save();*/ + presentation.Save();*/ - } + } -} + } diff --git a/FileFormat.Slides.Common/Constants.cs b/FileFormat.Slides.Common/Constants.cs new file mode 100644 index 0000000..9b03a1f --- /dev/null +++ b/FileFormat.Slides.Common/Constants.cs @@ -0,0 +1,8 @@ +namespace FileFormat.Slides.Common + { + public class Constants + { + public const string OUTPUT_DIR = "C:\\Users\\Kaar User\\Documents\\PresentationMaker\\"; + public const string PPT_FILE = "sample.pptx"; + } + } diff --git a/FileFormat.Slides.Common/Enumerations.cs b/FileFormat.Slides.Common/Enumerations.cs index 9b3e8d6..2bbcb54 100644 --- a/FileFormat.Slides.Common/Enumerations.cs +++ b/FileFormat.Slides.Common/Enumerations.cs @@ -1,29 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace FileFormat.Slides.Common.Enumerations -{ +namespace FileFormat.Slides.Common.Enumerations + { /// /// Specifies the alignment of text elements. /// public enum TextAlignment - { + { Left, Right, Center, None - } + } /// /// Specifies the type of styled list /// public enum ListType - { + { Bulleted, Numbered - } + } public enum AnimationType - { + { None, // No animation Fade, // Fade in or out Wipe, // Wipe across the screen @@ -42,6 +38,15 @@ public enum AnimationType Wheel, // Wheel animation Float, // Float in or out Custom // Custom animation defined by the user - } + } + + public enum TextAnchor + { + Center, + Top, + Bottom, + None + } + -} + } diff --git a/FileFormat.Slides.Common/FileFormat.Slides.Common.csproj b/FileFormat.Slides.Common/FileFormat.Slides.Common.csproj index 49c4d01..0fb1506 100644 --- a/FileFormat.Slides.Common/FileFormat.Slides.Common.csproj +++ b/FileFormat.Slides.Common/FileFormat.Slides.Common.csproj @@ -1,11 +1,15 @@ - + netcoreapp3.1 - D:\Local_Repos\FileFormat.Slides\FileFormat.Slides.Common\FileFormat.Slides.Common.xml + C:\Users\Kaar User\Documents\FileFormatSlides\FileFormat.Slides.Common.xml + + + + diff --git a/FileFormat.Slides.Facade/SlideFacade.cs b/FileFormat.Slides.Facade/SlideFacade.cs index e4be7de..1632741 100644 --- a/FileFormat.Slides.Facade/SlideFacade.cs +++ b/FileFormat.Slides.Facade/SlideFacade.cs @@ -1,25 +1,21 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; -using P = DocumentFormat.OpenXml.Presentation; -using D = DocumentFormat.OpenXml.Drawing; -using Text = DocumentFormat.OpenXml.Drawing.Text; -using NonVisualGroupShapeProperties = DocumentFormat.OpenXml.Presentation.NonVisualGroupShapeProperties; -using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint; -using FileFormat.Slides.Common.Enumerations; using FileFormat.Slides.Common; +using FileFormat.Slides.Common.Enumerations; +using System; +using System.Collections.Generic; +using System.Linq; +using D = DocumentFormat.OpenXml.Drawing; +using P = DocumentFormat.OpenXml.Presentation; namespace FileFormat.Slides.Facade -{ - public class SlideFacade { - + public class SlideFacade + { + private Slide _PresentationSlide = null; - private SlidePart _SlidePart = null; + private SlidePart _SlidePart = null; private String _RelationshipId; private int _SlideIndex; @@ -65,7 +61,7 @@ public class SlideFacade private NotesSlidePart _NotesPart; - + public Slide PresentationSlide { get => _PresentationSlide; set => _PresentationSlide = value; } public string RelationshipId { get => _RelationshipId; set => _RelationshipId = value; } @@ -92,18 +88,18 @@ public class SlideFacade public List HexagonFacades { get => _HexagonFacades; set => _HexagonFacades = value; } public List TrapezoidFacades { get => _TrapezoidFacades; set => _TrapezoidFacades = value; } public List PieFacades { get => _PieFacades; set => _PieFacades = value; } - - public SlideFacade (bool isNewSlide) - { - if (isNewSlide) + + public SlideFacade(bool isNewSlide) { + if (isNewSlide) + { Utility.NextIndex += 1; _RelationshipId = Utility.GetUniqueRelationshipId(); var _PresentationPart = PresentationDocumentFacade.getInstance().GetPresentationPart(); var _SlideIdList = _PresentationPart.Presentation.SlideIdList; _SlideIdList.Append(new SlideId() { Id = (UInt32Value)Utility.GetRandomSlideId(), RelationshipId = _RelationshipId }); - + _PresentationSlide = new Slide( new CommonSlideData( new ShapeTree( @@ -117,21 +113,21 @@ public SlideFacade (bool isNewSlide) if (PresentationDocumentFacade.getInstance().PresentationSlideLayoutParts.Count != 0) _SlidePart.AddPart(PresentationDocumentFacade.getInstance().PresentationSlideLayoutParts[0]); + } } - } - public void SetSlideBackground (string color) - { + public void SetSlideBackground(string color) + { // Check if color is not null or empty if (!string.IsNullOrEmpty(color)) - { + { // Check if there is already a Background element Background existingBackground = _PresentationSlide.CommonSlideData.Elements().FirstOrDefault(); // If an existing background is found, remove it if (existingBackground != null) - { + { _PresentationSlide.CommonSlideData.RemoveChild(existingBackground); - } + } // Create a new Background element with the specified color Background newBackground = new Background(); @@ -144,19 +140,19 @@ public void SetSlideBackground (string color) // Insert the new Background element before the ShapeTree _PresentationSlide.CommonSlideData.InsertBefore(newBackground, _PresentationSlide.CommonSlideData.ShapeTree); + } } - } - + public IEnumerable> GetComments() - { + { List> comments = new List>(); if (_CommentPart != null) - { + { var commentList = _CommentPart.CommentList; // Extract comment authors foreach (var comment in commentList.Elements()) - { + { Dictionary CommentProperties = new Dictionary { { "Index", comment.Index}, @@ -168,13 +164,13 @@ public IEnumerable> GetComments() }; comments.Add(CommentProperties); + } } - } return comments; - } - public TextShapeFacade AddTextShape (String text, Int32 fontSize, TextAlignment alignment, Int64 _x, Int64 _y, + } + public TextShapeFacade AddTextShape(String text, Int32 fontSize, TextAlignment alignment, TextAnchor anchor, Int64 _x, Int64 _y, Int64 width, Int64 height, String fontFamily, String textColor, String backgroundColor) - { + { // Create an instance of TextShapeFacade TextShapeFacade textShapeFacade = new TextShapeFacade(); @@ -186,6 +182,7 @@ public TextShapeFacade AddTextShape (String text, Int32 fontSize, TextAlignment .WithTextColor(textColor) .WithBackgroundColor(backgroundColor) .WithAlignment(alignment) + .WithAnchor(anchor) .WithPosition(_x, _y) .WithSize(width, height); @@ -194,31 +191,31 @@ public TextShapeFacade AddTextShape (String text, Int32 fontSize, TextAlignment // Append the textBoxShape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(textBoxShape); if (textShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(textBoxShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return textShapeFacade; - } + } public RectangleShapeFacade DrawRectangle(Int64 _x, Int64 _y, Int64 width, Int64 height, String backgroundColor, RectangleShapeFacade facade) - { + { RectangleShapeFacade _RectangleShapeFacade; // Use provided facade or create a new one if (facade == null) - { + { _RectangleShapeFacade = new RectangleShapeFacade(); - } + } else - { + { _RectangleShapeFacade = facade; - } + } // Set properties _RectangleShapeFacade @@ -229,34 +226,34 @@ public RectangleShapeFacade DrawRectangle(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape rectangleShape = _RectangleShapeFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(rectangleShape); // Handle animation if (_RectangleShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(rectangleShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _RectangleShapeFacade; - } + } public TriangleShapeFacade DrawTriangle(Int64 _x, Int64 _y, Int64 width, Int64 height, String backgroundColor, TriangleShapeFacade facade) - { + { TriangleShapeFacade _TriangleShapeFacade; // Use provided facade or create a new one if (facade == null) - { + { _TriangleShapeFacade = new TriangleShapeFacade(); - } + } else - { + { _TriangleShapeFacade = facade; - } + } // Set properties _TriangleShapeFacade @@ -267,34 +264,34 @@ public TriangleShapeFacade DrawTriangle(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape triangleShape = _TriangleShapeFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(triangleShape); // Handle animation if (_TriangleShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(triangleShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _TriangleShapeFacade; - } + } public DiamondShapeFacade DrawDiamond(Int64 _x, Int64 _y, Int64 width, Int64 height, String backgroundColor, DiamondShapeFacade facade) - { + { DiamondShapeFacade _DiamondShapeFacade; // Use provided facade or create a new one if (facade == null) - { + { _DiamondShapeFacade = new DiamondShapeFacade(); - } + } else - { + { _DiamondShapeFacade = facade; - } + } // Set properties _DiamondShapeFacade @@ -305,34 +302,34 @@ public DiamondShapeFacade DrawDiamond(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape diamondShape = _DiamondShapeFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(diamondShape); // Handle animation if (_DiamondShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(diamondShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _DiamondShapeFacade; - } + } public LineFacade DrawLine(Int64 _x, Int64 _y, Int64 width, Int64 height, LineFacade facade) - { + { LineFacade _LineFacade; // Use provided facade or create a new one if (facade == null) - { + { _LineFacade = new LineFacade(); - } + } else - { + { _LineFacade = facade; - } + } // Set properties _LineFacade @@ -342,34 +339,34 @@ public LineFacade DrawLine(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape line = _LineFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(line); // Handle animation if (_LineFacade.Animation != AnimationType.None) - { + { CallAnimation(line.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _LineFacade; - } + } public CurvedLineFacade DrawCurvedLine(Int64 _x, Int64 _y, Int64 width, Int64 height, CurvedLineFacade facade) - { + { CurvedLineFacade _CurvedLineFacade; // Use provided facade or create a new one if (facade == null) - { + { _CurvedLineFacade = new CurvedLineFacade(); - } + } else - { + { _CurvedLineFacade = facade; - } + } // Set properties _CurvedLineFacade @@ -379,34 +376,34 @@ public CurvedLineFacade DrawCurvedLine(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape curvedLine = _CurvedLineFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(curvedLine); // Handle animation if (_CurvedLineFacade.Animation != AnimationType.None) - { + { CallAnimation(curvedLine.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _CurvedLineFacade; - } + } public ArrowFacade DrawArrow(Int64 _x, Int64 _y, Int64 width, Int64 height, ArrowFacade facade) - { + { ArrowFacade _ArrowFacade; // Use provided facade or create a new one if (facade == null) - { + { _ArrowFacade = new ArrowFacade(); - } + } else - { + { _ArrowFacade = facade; - } + } // Set properties _ArrowFacade @@ -416,34 +413,34 @@ public ArrowFacade DrawArrow(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape arrow = _ArrowFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(arrow); // Handle animation if (_ArrowFacade.Animation != AnimationType.None) - { + { CallAnimation(arrow.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _ArrowFacade; - } + } public DoubleArrowFacade DrawDoubleArrow(Int64 _x, Int64 _y, Int64 width, Int64 height, DoubleArrowFacade facade) - { + { DoubleArrowFacade _DoubleArrowFacade; // Use provided facade or create a new one if (facade == null) - { + { _DoubleArrowFacade = new DoubleArrowFacade(); - } + } else - { + { _DoubleArrowFacade = facade; - } + } // Set properties _DoubleArrowFacade @@ -453,34 +450,34 @@ public DoubleArrowFacade DrawDoubleArrow(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape doubleArrow = _DoubleArrowFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(doubleArrow); // Handle animation if (_DoubleArrowFacade.Animation != AnimationType.None) - { + { CallAnimation(doubleArrow.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _DoubleArrowFacade; - } + } public DoubleBraceFacade DrawDoubleBrace(Int64 _x, Int64 _y, Int64 width, Int64 height, DoubleBraceFacade facade) - { + { DoubleBraceFacade _DoubleBraceFacade; // Use provided facade or create a new one if (facade == null) - { + { _DoubleBraceFacade = new DoubleBraceFacade(); - } + } else - { + { _DoubleBraceFacade = facade; - } + } // Set properties _DoubleBraceFacade @@ -490,34 +487,34 @@ public DoubleBraceFacade DrawDoubleBrace(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape doubleBrace = _DoubleBraceFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(doubleBrace); // Handle animation if (_DoubleBraceFacade.Animation != AnimationType.None) - { + { CallAnimation(doubleBrace.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _DoubleBraceFacade; - } + } public DoubleBracketFacade DrawDoubleBracket(Int64 _x, Int64 _y, Int64 width, Int64 height, DoubleBracketFacade facade) - { + { DoubleBracketFacade _DoubleBracketFacade; // Use provided facade or create a new one if (facade == null) - { + { _DoubleBracketFacade = new DoubleBracketFacade(); - } + } else - { + { _DoubleBracketFacade = facade; - } + } // Set properties _DoubleBracketFacade @@ -527,23 +524,23 @@ public DoubleBracketFacade DrawDoubleBracket(Int64 _x, Int64 _y, // Create the shape and append it to the slide P.Shape doubleBracket = _DoubleBracketFacade.CreateShape(); if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(doubleBracket); // Handle animation if (_DoubleBracketFacade.Animation != AnimationType.None) - { + { CallAnimation(doubleBracket.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _DoubleBracketFacade; - } + } public PentagonFacade DrawPentagon(Int64 _x, Int64 _y, Int64 width, Int64 height, PentagonFacade facade) - { + { // Use the provided PentagonFacade or create a new instance PentagonFacade _PentagonFacade = facade ?? new PentagonFacade(); @@ -557,24 +554,24 @@ public PentagonFacade DrawPentagon(Int64 _x, Int64 _y, // Append the shape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(_Pentagon); // Handle animation if necessary if (_PentagonFacade.Animation != AnimationType.None) - { + { CallAnimation(_Pentagon.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _PentagonFacade; - } + } public HexagonFacade DrawHexagon(Int64 _x, Int64 _y, Int64 width, Int64 height, HexagonFacade facade) - { + { // Use the provided HexagonFacade or create a new instance HexagonFacade _HexagonFacade = facade ?? new HexagonFacade(); @@ -588,24 +585,24 @@ public HexagonFacade DrawHexagon(Int64 _x, Int64 _y, // Append the shape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(_Hexagon); // Handle animation if necessary if (_HexagonFacade.Animation != AnimationType.None) - { + { CallAnimation(_Hexagon.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _HexagonFacade; - } + } public TrapezoidFacade DrawTrapezoid(Int64 _x, Int64 _y, Int64 width, Int64 height, TrapezoidFacade facade) - { + { // Use the provided TrapezoidFacade or create a new instance TrapezoidFacade _TrapezoidFacade = facade ?? new TrapezoidFacade(); @@ -619,35 +616,35 @@ public TrapezoidFacade DrawTrapezoid(Int64 _x, Int64 _y, // Append the shape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(_Trapezoid); // Handle animation if necessary if (_TrapezoidFacade.Animation != AnimationType.None) - { + { CallAnimation(_Trapezoid.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _TrapezoidFacade; - } + } public CircleShapeFacade DrawCircle(Int64 _x, Int64 _y, Int64 width, Int64 height, String backgroundColor, CircleShapeFacade facade) - { + { CircleShapeFacade _CircleShapeFacade; // Create an instance of CircleShapeFacade if facade is null, otherwise use the provided facade if (facade == null) - { + { _CircleShapeFacade = new CircleShapeFacade(); - } + } else - { + { _CircleShapeFacade = facade; - } + } // Set properties using the provided parameters _CircleShapeFacade @@ -660,34 +657,34 @@ public CircleShapeFacade DrawCircle(Int64 _x, Int64 _y, // Append the CircleShape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(CircleShape); // Handle animation if present if (_CircleShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(CircleShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _CircleShapeFacade; - } + } public PieFacade DrawPie(Int64 _x, Int64 _y, Int64 width, Int64 height, PieFacade facade) - { + { PieFacade _PieFacade; // Create an instance of TextShapeFacade if (facade == null) - { + { _PieFacade = new PieFacade(); - } + } else - { + { _PieFacade = facade; - } + } _PieFacade.Animation = facade.Animation; // Set properties using the provided parameters _PieFacade @@ -699,19 +696,19 @@ public PieFacade DrawPie(Int64 _x, Int64 _y, // Append the textBoxShape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(_Pie); if (_PieFacade.Animation != AnimationType.None) - { + { CallAnimation(_Pie.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return _PieFacade; - } - public TextShapeFacade AddTextListShape (List textList, ListFacade facade, Int32 fontSize, TextAlignment alignment, Int64 _x, Int64 _y, + } + public TextShapeFacade AddTextListShape(List textList, ListFacade facade, Int32 fontSize, TextAlignment alignment, TextAnchor anchor, Int64 _x, Int64 _y, Int64 width, Int64 height, String fontFamily, String textColor, String backgroundColor) - { + { // Create an instance of TextShapeFacade TextShapeFacade textShapeFacade = new TextShapeFacade(); @@ -722,36 +719,38 @@ public TextShapeFacade AddTextListShape (List textList, ListFacade facad .WithTextColor(textColor) .WithBackgroundColor(backgroundColor) .WithAlignment(alignment) + .WithAnchor(anchor) .WithPosition(_x, _y) .WithSize(width, height); // Create the P.Shape using the CreateShape method - P.Shape textBoxShape = textShapeFacade.CreateListShape(textList,facade); + P.Shape textBoxShape = textShapeFacade.CreateListShape(textList, facade); // Append the textBoxShape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(textBoxShape); //_TextShapeFacades.Add(textShapeFacade); if (textShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(textBoxShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return textShapeFacade; - } - public TextShapeFacade AddTextShape ( List textSegmentFacades,TextAlignment alignment, Int64 _x, Int64 _y, + } + public TextShapeFacade AddTextShape(List textSegmentFacades, TextAlignment alignment, TextAnchor anchor, Int64 _x, Int64 _y, Int64 width, Int64 height, String backgroundColor) - { + { // Create an instance of TextShapeFacade TextShapeFacade textShapeFacade = new TextShapeFacade(); // Set properties using the provided parameters textShapeFacade .WithAlignment(alignment) + .WithAnchor(anchor) .WithPosition(_x, _y) .WithSize(width, height) .WithBackgroundColor(backgroundColor); @@ -761,39 +760,39 @@ public TextShapeFacade AddTextShape ( List textSegmentFacades // Append the textBoxShape to the ShapeTree of the presentation slide if (_PresentationSlide.CommonSlideData.ShapeTree == null) - { + { _PresentationSlide.CommonSlideData.ShapeTree = new P.ShapeTree(); - } + } _PresentationSlide.CommonSlideData.ShapeTree.Append(textBoxShape); //_TextShapeFacades.Add(textShapeFacade); if (textShapeFacade.Animation != AnimationType.None) - { + { CallAnimation(textBoxShape.NonVisualShapeProperties.NonVisualDrawingProperties.Id); - } + } return textShapeFacade; - } + } public void AddNote(String noteText) - { + { var relId = _RelationshipId; - + NotesSlidePart notesSlidePart1; string existingSlideNote = noteText; if (_SlidePart.NotesSlidePart != null) - { + { //Appened new note to existing note. existingSlideNote = _SlidePart.NotesSlidePart.NotesSlide.InnerText + "\n" + noteText; //var val = (NotesSlidePart)_SlidePart.GetPartById(relId); //var val = _SlidePart.NotesSlidePart; notesSlidePart1 = _NotesPart; - } + } else - { + { //Add a new noteto a slide. notesSlidePart1 = _SlidePart.AddNewPart(relId); - } + } NotesSlide notesSlide = new NotesSlide( new CommonSlideData(new ShapeTree( @@ -826,40 +825,40 @@ public void AddNote(String noteText) new ColorMapOverride(new D.MasterColorMapping())); notesSlidePart1.NotesSlide = notesSlide; - } + } public void RemoveNote() - { - if (_SlidePart.NotesSlidePart != null) { + if (_SlidePart.NotesSlidePart != null) + { // Clear the existing notes. _SlidePart.DeletePart(_SlidePart.NotesSlidePart); + } } - } - public void AddImage (ImageFacade picture ) - { + public void AddImage(ImageFacade picture) + { _PresentationSlide.CommonSlideData.ShapeTree.Append(picture.Image); - } - - public void Update () - { + } + + public void Update() + { this.SetSlideBackground(_BackgroundColor); - } + } private void CallAnimation(string shapeId) - { - + { + AnimateFacade animateFacade = new AnimateFacade(); // Optionally, override default properties animateFacade.ShapeId = shapeId; // You can change the ShapeId if needed _PresentationSlide.Append(animateFacade.animate()); - } + } public void close() - { + { _PresentationSlide.RemoveAllChildren(); _PresentationSlide.Remove(); _PresentationSlide = null; - } + } + } } -} diff --git a/FileFormat.Slides.Facade/TextShapeFacade.cs b/FileFormat.Slides.Facade/TextShapeFacade.cs index e4ad533..5e3aa6c 100644 --- a/FileFormat.Slides.Facade/TextShapeFacade.cs +++ b/FileFormat.Slides.Facade/TextShapeFacade.cs @@ -1,25 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; -using P = DocumentFormat.OpenXml.Presentation; +using FileFormat.Slides.Common.Enumerations; +using System; +using System.Collections.Generic; +using System.Linq; using D = DocumentFormat.OpenXml.Drawing; +using P = DocumentFormat.OpenXml.Presentation; using Text = DocumentFormat.OpenXml.Drawing.Text; -using NonVisualGroupShapeProperties = DocumentFormat.OpenXml.Presentation.NonVisualGroupShapeProperties; -using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint; -using FileFormat.Slides.Common.Enumerations; -using FileFormat.Slides.Common; namespace FileFormat.Slides.Facade -{ - public class TextShapeFacade { + public class TextShapeFacade + { private string _Text; private int _fontSize; private TextAlignment _alignment; + private TextAnchor _anchor; private long _x; private long _y; private long _width; @@ -36,19 +34,20 @@ public class TextShapeFacade public string Text { get => _Text; set => _Text = value; } // Public property to set and get font size in points public int FontSize - { - get { + get + { // Convert fontSizeTwentieths to points return (int)_fontSize / 20 / 2; - } + } set - { + { // Convert points to twentieths of a point and store in fontSizeTwentieths _fontSize = (int)(value * 20 * 2); + } } - } public TextAlignment Alignment { get => _alignment; set => _alignment = value; } + public TextAnchor Anchor { get => _anchor; set => _anchor = value; } public long X { get => _x; set => _x = value; } public long Y { get => _y; set => _y = value; } public long Width { get => _width; set => _width = value; } @@ -63,62 +62,69 @@ public int FontSize public ListFacade TextList { get => _TextList; set => _TextList = value; } public AnimationType Animation { get => _Animation; set => _Animation = value; } - public TextShapeFacade () - { + public TextShapeFacade() + { // Set default values or initialization logic if needed FontSize = 12; // Example default font size - } + } - public TextShapeFacade WithText (string text) - { + public TextShapeFacade WithText(string text) + { Text = text; return this; - } + } - public TextShapeFacade WithFontSize (int fontSize) - { + public TextShapeFacade WithFontSize(int fontSize) + { FontSize = fontSize; return this; - } - public TextShapeFacade WithFontFamily (String fontfamily) - { + } + public TextShapeFacade WithFontFamily(String fontfamily) + { FontFamily = fontfamily; return this; - } - public TextShapeFacade WithTextColor (String textColor) - { + } + public TextShapeFacade WithTextColor(String textColor) + { TextColor = textColor; return this; - } - public TextShapeFacade WithBackgroundColor (String backgroundColor) - { + } + public TextShapeFacade WithBackgroundColor(String backgroundColor) + { BackgroundColor = backgroundColor; return this; - } - public TextShapeFacade WithAlignment (TextAlignment alignment) - { + } + public TextShapeFacade WithAlignment(TextAlignment alignment) + { Alignment = alignment; return this; - } + } - public TextShapeFacade WithPosition (long x, long y) - { + public TextShapeFacade WithAnchor(TextAnchor anchor) + { + Anchor = anchor; + return this; + } + + public TextShapeFacade WithPosition(long x, long y) + { X = x; Y = y; return this; - } + } - public TextShapeFacade WithSize (long width, long height) - { + public TextShapeFacade WithSize(long width, long height) + { Width = width; Height = height; return this; - } + } - public P.Shape CreateShape () - { + public P.Shape CreateShape() + { TextAlignmentTypeValues alignmentType = ConvertAlignmentToTypeValues(Alignment); + TextAnchoringTypeValues anchorType = ConvertAnchorToTypeValues(Anchor); P.Shape shape1 = new P.Shape(); shape1.Append(CreateNonVisualShapeProperties()); if (_BackgroundColor is null) @@ -126,13 +132,13 @@ public P.Shape CreateShape () else shape1.Append(CreateShapeProperties(X, Y, Width, Height, BackgroundColor)); shape1.Append(CreateShapeStyle()); - shape1.Append(CreateTextBody(_TextColor, _FontFamily, _fontSize, _Text, alignmentType)); + shape1.Append(CreateTextBody(_TextColor, _FontFamily, _fontSize, _Text, alignmentType, anchorType)); return shape1; - } - public P.Shape CreateListShape (List listItems, ListFacade list) - { + } + public P.Shape CreateListShape(List listItems, ListFacade list) + { TextAlignmentTypeValues alignmentType = ConvertAlignmentToTypeValues(Alignment); P.Shape shape1 = new P.Shape(); shape1.Append(CreateNonVisualShapeProperties()); @@ -143,10 +149,11 @@ public P.Shape CreateListShape (List listItems, ListFacade list) shape1.Append(CreateShapeStyle()); shape1.Append(list.CreateList(listItems, _TextColor, _FontFamily, _fontSize, new P.TextBody())); return shape1; - } - public P.Shape CreateShape (List textSegmentFacades) - { + } + public P.Shape CreateShape(List textSegmentFacades) + { TextAlignmentTypeValues alignmentType = ConvertAlignmentToTypeValues(Alignment); + TextAnchoringTypeValues anchorType = ConvertAnchorToTypeValues(Anchor); P.Shape shape1 = new P.Shape(); shape1.Append(CreateNonVisualShapeProperties()); if (_BackgroundColor is null) @@ -154,17 +161,17 @@ public P.Shape CreateShape (List textSegmentFacades) else shape1.Append(CreateShapeProperties(X, Y, Width, Height, BackgroundColor)); shape1.Append(CreateShapeStyle()); - shape1.Append(CreateTextBody(textSegmentFacades, alignmentType)); + shape1.Append(CreateTextBody(textSegmentFacades, alignmentType, anchorType)); return shape1; - } + } - private P.ShapeStyle CreateShapeStyle () - { + private P.ShapeStyle CreateShapeStyle() + { P.ShapeStyle shapeStyle1 = new P.ShapeStyle(); D.LineReference lineReference1 = new D.LineReference() { Index = (UInt32Value)2U }; @@ -197,9 +204,9 @@ private P.ShapeStyle CreateShapeStyle () shapeStyle1.Append(fontReference1); return shapeStyle1; - } - private P.ShapeProperties CreateShapeProperties (long x, long y, long width, long height, string rgbColorHex = "Transparent") - { + } + private P.ShapeProperties CreateShapeProperties(long x, long y, long width, long height, string rgbColorHex = "Transparent") + { P.ShapeProperties shapeProperties1 = new P.ShapeProperties(); D.Transform2D transform2D1 = new D.Transform2D(); @@ -217,10 +224,10 @@ private P.ShapeProperties CreateShapeProperties (long x, long y, long width, lon D.SolidFill solidFill1 = new D.SolidFill(); if (rgbColorHex != "Transparent") - { + { D.RgbColorModelHex rgbColorModelHex1 = new D.RgbColorModelHex() { Val = rgbColorHex }; solidFill1.Append(rgbColorModelHex1); - } + } D.Outline outline1 = new D.Outline() { Width = 12700 }; D.SolidFill solidFill2 = new D.SolidFill(); @@ -240,11 +247,11 @@ private P.ShapeProperties CreateShapeProperties (long x, long y, long width, lon return shapeProperties1; - } - private P.TextBody CreateTextBody (string textColor, string font, int fontSize, string Text, TextAlignmentTypeValues alignmentType) - { + } + private P.TextBody CreateTextBody(string textColor, string font, int fontSize, string Text, TextAlignmentTypeValues alignmentType, TextAnchoringTypeValues anchorType) + { P.TextBody textBody1 = new P.TextBody(); - D.BodyProperties bodyProperties1 = new D.BodyProperties() { RightToLeftColumns = false, Anchor = D.TextAnchoringTypeValues.Center }; + D.BodyProperties bodyProperties1 = new D.BodyProperties() { RightToLeftColumns = false, Anchor = anchorType }; D.ListStyle listStyle1 = new D.ListStyle(); D.Paragraph paragraph1 = new D.Paragraph( @@ -252,11 +259,11 @@ private P.TextBody CreateTextBody (string textColor, string font, int fontSize, new Run( new RunProperties(new SolidFill(new RgbColorModelHex() { Val = _TextColor }), new LatinFont() { Typeface = _FontFamily }) - { FontSize = _fontSize, Dirty = false }, + { FontSize = _fontSize, Dirty = false }, new Text() { Text = Text } ) ); - D.ParagraphProperties paragraphProperties1 = new D.ParagraphProperties() { Alignment = D.TextAlignmentTypeValues.Center }; + D.ParagraphProperties paragraphProperties1 = new D.ParagraphProperties() { Alignment = alignmentType }; D.EndParagraphRunProperties endParagraphRunProperties1 = new D.EndParagraphRunProperties() { Language = "es-ES" }; paragraph1.Append(paragraphProperties1); @@ -267,20 +274,20 @@ private P.TextBody CreateTextBody (string textColor, string font, int fontSize, textBody1.Append(paragraph1); return textBody1; - } - private P.TextBody CreateTextBody (List textSegmentFacades, TextAlignmentTypeValues alignmentType) - { + } + private P.TextBody CreateTextBody(List textSegmentFacades, TextAlignmentTypeValues alignmentType, TextAnchoringTypeValues anchorType) + { P.TextBody textBody1 = new P.TextBody(); - D.BodyProperties bodyProperties1 = new D.BodyProperties() { RightToLeftColumns = false, Anchor = D.TextAnchoringTypeValues.Center }; + D.BodyProperties bodyProperties1 = new D.BodyProperties() { RightToLeftColumns = false, Anchor = anchorType }; D.ListStyle listStyle1 = new D.ListStyle(); D.Paragraph paragraph1 = new D.Paragraph(); D.ParagraphProperties paragraphProperties1 = new D.ParagraphProperties() { Alignment = alignmentType }; // Append each run to the paragraph foreach (TextSegmentFacade textSegmentFacade in textSegmentFacades) - { + { paragraph1.Append(textSegmentFacade); - } + } D.EndParagraphRunProperties endParagraphRunProperties1 = new D.EndParagraphRunProperties() { Language = "es-ES" }; @@ -292,10 +299,10 @@ private P.TextBody CreateTextBody (List textSegmentFacades, T textBody1.Append(paragraph1); return textBody1; - } + } - private P.NonVisualShapeProperties CreateNonVisualShapeProperties () - { + private P.NonVisualShapeProperties CreateNonVisualShapeProperties() + { P.NonVisualShapeProperties nonVisualShapeProperties1 = new P.NonVisualShapeProperties(); P.NonVisualDrawingProperties nonVisualDrawingProperties1 = new P.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "1 Shape Name" }; P.NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new P.NonVisualShapeDrawingProperties(); @@ -306,14 +313,14 @@ private P.NonVisualShapeProperties CreateNonVisualShapeProperties () nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1); return nonVisualShapeProperties1; - } + } - public void UpdateShape () - { - if (TextBoxShape == null) + public void UpdateShape() { + if (TextBoxShape == null) + { throw new InvalidOperationException("Shape has not been created yet. Call CreateShape() first."); - } + } var alignmentType = TextAlignmentTypeValues.Justified; if (Alignment != TextAlignment.None) @@ -325,48 +332,48 @@ public void UpdateShape () TextBoxShape.NonVisualShapeProperties.NonVisualShapeDrawingProperties = new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }); TextBoxShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(new PlaceholderShape()); if (Width != 0) - { - TextBoxShape.ShapeProperties.Transform2D = new D.Transform2D() { + TextBoxShape.ShapeProperties.Transform2D = new D.Transform2D() + { Offset = new D.Offset() { X = X, Y = Y }, Extents = new D.Extents() { Cx = Width, Cy = Height } - }; - } + }; + } if (!String.IsNullOrEmpty(_BackgroundColor)) - { - if (_BackgroundColor == "Transparent") { - if (TextBoxShape.ShapeProperties.Descendants().FirstOrDefault() == null) + if (_BackgroundColor == "Transparent") { + if (TextBoxShape.ShapeProperties.Descendants().FirstOrDefault() == null) + { if (TextBoxShape.ShapeProperties.Descendants().FirstOrDefault() != null) TextBoxShape.ShapeProperties.Descendants().FirstOrDefault().Remove(); - } + } else - { + { TextBoxShape.ShapeProperties.Append(new NoFill()); - } + } - } + } else - { - if (TextBoxShape.ShapeProperties.Descendants().FirstOrDefault() != null) { + if (TextBoxShape.ShapeProperties.Descendants().FirstOrDefault() != null) + { TextBoxShape.ShapeProperties.Descendants().FirstOrDefault().Remove(); - } + } var fill = TextBoxShape.ShapeProperties.Descendants().FirstOrDefault(); if (fill != null) - { + { fill.RemoveAllChildren(); fill.Append(new RgbColorModelHex() { Val = _BackgroundColor }); - } + } else - { + { TextBoxShape.ShapeProperties.Append(new SolidFill(new RgbColorModelHex() { Val = _BackgroundColor })); - } + } + } } - } var existingParagraphText = TextBoxShape.TextBody.Descendants().FirstOrDefault(); TextBoxShape.TextBody.Elements().FirstOrDefault().RemoveAllChildren(); @@ -377,126 +384,127 @@ public void UpdateShape () var runProperties = TextBoxShape.TextBody.Descendants().FirstOrDefault(); if (_fontSize != 0) - { + { runProperties.FontSize = _fontSize; - } + } else if (runProperties.FontSize != null) - { + { - } + } else - { + { // Extract font size from associated slide layout var slideLayout = _AssociatedSlidePart.SlideLayoutPart.SlideLayout; var defaultRunProperties = slideLayout.Descendants().ToList()[_ShapeIndex]; if (defaultRunProperties != null) - { + { runProperties.FontSize = defaultRunProperties.FontSize; + } } - } var latinFont = runProperties.Elements().FirstOrDefault(); if (!string.IsNullOrEmpty(_FontFamily)) - { + { latinFont = new LatinFont() { Typeface = _FontFamily }; - } + } else if (latinFont != null) - { + { - } + } else - { + { // Extract font family from associated slide layout var slideLayout = _AssociatedSlidePart.SlideLayoutPart.SlideLayout; var defaultRunProperties = slideLayout.Descendants().ToList()[_ShapeIndex]; var defaultLatinFont = defaultRunProperties.Elements().FirstOrDefault(); if (defaultRunProperties != null && defaultLatinFont != null) - { + { latinFont = new LatinFont() { Typeface = defaultLatinFont.Typeface }; + } } - } var solidFill = runProperties.Elements().FirstOrDefault(); if (!string.IsNullOrEmpty(_TextColor)) - { + { // Clear existing font-related properties runProperties.Elements().ToList().ForEach(l => l.Remove()); // Set text color using hex code (e.g., #FF0000 for red) solidFill = new SolidFill(new RgbColorModelHex() { Val = _TextColor }); runProperties.AppendChild(solidFill); - } + } else if (solidFill != null) - { + { - } + } else - { + { // Extract color from associated slide layout var slideLayout = _AssociatedSlidePart.SlideLayoutPart.SlideLayout; var defaultRunProperties = slideLayout.Descendants().ToList()[_ShapeIndex]; if (defaultRunProperties != null) - { + { // Clear existing font-related properties runProperties.Elements().ToList().ForEach(l => l.Remove()); // Set text color using hex code (e.g., #FF0000 for red) solidFill = new SolidFill(new RgbColorModelHex() { Val = _TextColor }); runProperties.AppendChild(solidFill); + } } - } TextBoxShape.TextBody.Elements().FirstOrDefault().Elements().FirstOrDefault().Text.Text = Text; - } + } - public void RemoveShape (SlidePart slidePart) - { + public void RemoveShape(SlidePart slidePart) + { // Ensure slidePart is not null if (slidePart == null) - { + { throw new ArgumentNullException("slidePart cannot be null."); - } + } // Find the ShapeTree in CommonSlideData CommonSlideData commonSlideData = slidePart.Slide.CommonSlideData; if (commonSlideData != null && commonSlideData.ShapeTree != null) - { + { // Remove the specified shape from the ShapeTree var shapesToRemove = commonSlideData.ShapeTree.Elements().Where(shape => shape == TextBoxShape).ToList(); foreach (var shape in shapesToRemove) - { + { shape.Remove(); + } } } - } - public void RemoveShape (P.Shape shape) - { + public void RemoveShape(P.Shape shape) + { shape.Remove(); - } + } // Method to populate List from a collection of P.Shape - public static List PopulateTextShapes (SlidePart slidePart) - { + public static List PopulateTextShapes(SlidePart slidePart) + { IEnumerable shapes = slidePart.Slide.CommonSlideData.ShapeTree.Elements(); var textShapes = new List(); var shapeIndex = 0; foreach (var shape in shapes) - { + { var textShapeFacade = new TextShapeFacade - { + { TextBoxShape = shape, // Store the P.Shape in the private field Text = GetTextFromTextShape(shape), FontSize = GetFontSizeFromTextShape(shape), TextColor = GetColorFromTextShape(shape), Alignment = GetAlignmentFromTextShape(shape), + Anchor = GetAnchorFromTextShape(shape), FontFamily = GetFontFamilyFromTextShape(shape), X = GetXFromShape(shape), Y = GetYFromShape(shape), @@ -505,44 +513,44 @@ public static List PopulateTextShapes (SlidePart slidePart) TextList = GetStyledList(shape), AssociatedSlidePart = slidePart, ShapeIndex = shapeIndex - }; + }; textShapes.Add(textShapeFacade); shapeIndex += 1; - } + } return textShapes; - } + } - private static string GetTextFromTextShape (P.Shape textShape) - { - if (textShape.TextBody != null) + private static string GetTextFromTextShape(P.Shape textShape) { + if (textShape.TextBody != null) + { return textShape.TextBody.Descendants().FirstOrDefault()?.Text; - } + } return null; - } + } - private static int GetFontSizeFromTextShape (P.Shape textShape) - { + private static int GetFontSizeFromTextShape(P.Shape textShape) + { var paragraph = textShape.TextBody?.Elements().FirstOrDefault(); if (paragraph != null) - { + { var defaultRunProperties = paragraph.Elements().FirstOrDefault()?.Elements().FirstOrDefault(); if (defaultRunProperties != null) - { + { return defaultRunProperties.FontSize; + } } - } return 0; - } - private static ListFacade GetStyledList (P.Shape textShape) - { - if (textShape.TextBody.Descendants().Count() > 0) + } + private static ListFacade GetStyledList(P.Shape textShape) { + if (textShape.TextBody.Descendants().Count() > 0) + { var bulletFont = textShape.TextBody.Descendants().FirstOrDefault(); var paragraphs = textShape.TextBody.Descendants(); var runProperties = paragraphs.FirstOrDefault().Descendants().FirstOrDefault(); @@ -554,102 +562,108 @@ private static ListFacade GetStyledList (P.Shape textShape) listFacade.FontFamily = runProperties.Descendants().FirstOrDefault().Typeface; listFacade.TextColor = runProperties.Descendants().FirstOrDefault().Val; foreach (var paragraph in paragraphs) - { + { listItems.Add(paragraph.InnerText); - } + } listFacade.ListItems = listItems; return listFacade; - } + } return null; - } - private static string GetFontFamilyFromTextShape (P.Shape textShape) - { + } + private static string GetFontFamilyFromTextShape(P.Shape textShape) + { var paragraph = textShape.TextBody?.Elements().FirstOrDefault(); if (paragraph != null) - { + { var defaultRunProperties = paragraph.Elements().FirstOrDefault()?.Elements().FirstOrDefault(); if (defaultRunProperties != null) - { + { var latinFont = defaultRunProperties.Elements().FirstOrDefault(); if (latinFont != null) - { + { return latinFont.Typeface; + } } } - } return null; // or an appropriate default value for FontFamily - } - private static string GetColorFromTextShape (P.Shape textShape) - { + } + private static string GetColorFromTextShape(P.Shape textShape) + { var paragraph = textShape.TextBody?.Elements().FirstOrDefault(); if (paragraph != null) - { + { var defaultRunProperties = paragraph.Elements().FirstOrDefault()?.Elements().FirstOrDefault(); if (defaultRunProperties != null) - { + { var solidFill = defaultRunProperties.Elements().FirstOrDefault(); if (solidFill != null) - { + { var rgbColor = solidFill.Elements().FirstOrDefault(); if (rgbColor != null) - { + { return rgbColor.Val; + } } } } - } return null; // or an appropriate default value for color code - } + } - private static TextAlignment GetAlignmentFromTextShape (P.Shape textShape) - { + private static TextAlignment GetAlignmentFromTextShape(P.Shape textShape) + { var alignment = textShape.TextBody?.Descendants().FirstOrDefault(); if (alignment != null) - { + { alignment = null; - } + } var paragraphProperties = textShape?.Descendants()?.FirstOrDefault()?.Descendants()? .FirstOrDefault(); TextAlignmentTypeValues alignmentType = textShape.TextBody.Descendants().FirstOrDefault()?.Alignment ?? TextAlignmentTypeValues.Justified; return ConvertAlignmentFromTypeValues(alignmentType); - } + } - private static long GetXFromShape (P.Shape shape) - { + private static TextAnchor GetAnchorFromTextShape(P.Shape textShape) + { + var anchor = textShape.TextBody?.Descendants().FirstOrDefault()?.Anchor ?? TextAnchoringTypeValues.Center; + return ConvertAnchorFromTypeValues(anchor); + } + + private static long GetXFromShape(P.Shape shape) + { return shape.ShapeProperties?.Transform2D?.Offset?.X ?? 0; - } + } - private static long GetYFromShape (P.Shape shape) - { + private static long GetYFromShape(P.Shape shape) + { return shape.ShapeProperties?.Transform2D?.Offset?.Y ?? 0; - } + } - private static long GetWidthFromShape (P.Shape shape) - { + private static long GetWidthFromShape(P.Shape shape) + { return shape.ShapeProperties?.Transform2D?.Extents?.Cx ?? 0; - } + } - private static long GetHeightFromShape (P.Shape shape) - { + private static long GetHeightFromShape(P.Shape shape) + { return shape.ShapeProperties?.Transform2D?.Extents?.Cy ?? 0; - } + } - private static TextAlignmentTypeValues ConvertAlignmentToTypeValues (TextAlignment alignment) - { - switch (alignment) + private static TextAlignmentTypeValues ConvertAlignmentToTypeValues(TextAlignment alignment) { + switch (alignment) + { case TextAlignment.Left: return TextAlignmentTypeValues.Left; case TextAlignment.Center: @@ -660,11 +674,11 @@ private static TextAlignmentTypeValues ConvertAlignmentToTypeValues (TextAlignme return TextAlignmentTypeValues.Justified; default: throw new ArgumentOutOfRangeException(nameof(alignment), alignment, null); + } } - } private static TextAlignment ConvertAlignmentFromTypeValues(TextAlignmentTypeValues alignmentType) - { + { if (alignmentType.Equals(TextAlignmentTypeValues.Left)) return TextAlignment.Left; if (alignmentType.Equals(TextAlignmentTypeValues.Center)) @@ -675,6 +689,33 @@ private static TextAlignment ConvertAlignmentFromTypeValues(TextAlignmentTypeVal return TextAlignment.None; throw new ArgumentOutOfRangeException(nameof(alignmentType), alignmentType, null); + } + + private static TextAnchoringTypeValues ConvertAnchorToTypeValues(TextAnchor alignment) + { + switch (alignment) + { + case TextAnchor.Top: + return TextAnchoringTypeValues.Top; + case TextAnchor.Center: + return TextAnchoringTypeValues.Center; + case TextAnchor.Bottom: + return TextAnchoringTypeValues.Bottom; + default: + throw new ArgumentOutOfRangeException(nameof(alignment), alignment, null); + } + } + + private static TextAnchor ConvertAnchorFromTypeValues(TextAnchoringTypeValues anchorType) + { + if (anchorType.Equals(TextAnchoringTypeValues.Top)) + return TextAnchor.Top; + if (anchorType.Equals(TextAnchoringTypeValues.Bottom)) + return TextAnchor.Bottom; + if (anchorType.Equals(TextAnchoringTypeValues.Center)) + return TextAnchor.Center; + + throw new ArgumentOutOfRangeException(nameof(anchorType), anchorType, null); + } } } -} diff --git a/FileFormat.Slides/FileFormat.Slides.csproj b/FileFormat.Slides/FileFormat.Slides.csproj index aedc70f..8328a41 100644 --- a/FileFormat.Slides/FileFormat.Slides.csproj +++ b/FileFormat.Slides/FileFormat.Slides.csproj @@ -6,7 +6,7 @@ - D:\Local_Repos\FileFormat.Slides\FileFormat.Slides\FileFormat.Slides.xml + C:\Users\Kaar User\Documents\FileFormatSlides\FileFormat.Slides.xml diff --git a/FileFormat.Slides/Presentation.cs b/FileFormat.Slides/Presentation.cs index c03d1f3..35becb0 100644 --- a/FileFormat.Slides/Presentation.cs +++ b/FileFormat.Slides/Presentation.cs @@ -1,21 +1,20 @@ using DocumentFormat.OpenXml; -using DocumentFormat.OpenXml.Presentation; using FileFormat.Slides.Facade; using System; using System.Collections.Generic; namespace FileFormat.Slides -{ + { /// /// Represents the presentation document. /// public class Presentation - { + { private static String _FileName = "MyPresentation.pptx"; private static String _DirectoryPath = "D:\\AsposeSampleResults\\"; private static PresentationDocumentFacade doc = null; private List _Slides = null; - private List _CommentAuthors=null; + private List _CommentAuthors = null; private int _SlideWidth = 960; private int _SlideHeight = 720; @@ -36,24 +35,24 @@ public class Presentation /// Initializes the presentation object. /// /// Presentation path as string - private Presentation (String FilePath) - { - + private Presentation(String FilePath) + { + _Slides = new List(); _CommentAuthors = new List(); doc = PresentationDocumentFacade.Create(FilePath); - - } + + } /// /// Default constructor to initialize presentation object. /// - private Presentation () - { + private Presentation() + { _Slides = new List(); _CommentAuthors = new List(); - } + } /// /// Static method to instantiate a new object of Presentation class. /// @@ -82,10 +81,10 @@ private Presentation () /// presentation.Save(); /// /// - public static Presentation Create (String FilePath) - { + public static Presentation Create(String FilePath) + { return new Presentation(FilePath); - } + } /// /// Static method to load an existing presentation. /// @@ -107,52 +106,52 @@ public static Presentation Create (String FilePath) /// presentation.Save(); /// /// - public static Presentation Open (String FilePath) - { + public static Presentation Open(String FilePath) + { doc = PresentationDocumentFacade.Open(FilePath); return new Presentation(); - } + } /// /// Create comment author using this method /// /// Pass comment author object public void CreateAuthor(CommentAuthor author) - { + { doc.CreateAuthor(author.Id, author.ColorIndex, author.Name, author.InitialLetter); _CommentAuthors.Add(author); - } + } /// /// Get the list of comment author /// /// public List GetCommentAuthors() - { - List authorList = new List(); - var FacadeAuthors = doc.GetCommentAuthors(); - foreach(var author in FacadeAuthors) { + List authorList = new List(); + var FacadeAuthors = doc.GetCommentAuthors(); + foreach (var author in FacadeAuthors) + { CommentAuthor commentAuthor = new CommentAuthor(); commentAuthor.InitialLetter = author["Initials"]; commentAuthor.ColorIndex = Convert.ToInt32(author["ColorIndex"]); commentAuthor.Name = author["Name"]; - commentAuthor.Id = Convert.ToInt32(author["Id"]); + commentAuthor.Id = Convert.ToInt32(author["Id"]); authorList.Add(commentAuthor); - } + } return authorList; - } + } /// /// This method is responsible to append a slide. /// /// An object of a slide - public void AppendSlide (Slide slide) - { + public void AppendSlide(Slide slide) + { slide.SlideFacade.SetSlideBackground(slide.BackgroundColor); doc.SlideWidth = new Int32Value((int)Common.Utility.PixelsToEmu(SlideWidth)); doc.SlideHeight = new Int32Value((int)Common.Utility.PixelsToEmu(SlideHeight)); doc.AppendSlide(slide.SlideFacade); _Slides.Add(slide); - } + } /// /// Method to get the list of all slides of a presentation /// @@ -165,20 +164,21 @@ public void AppendSlide (Slide slide) /// ... /// /// - public List GetSlides () - { - if (!doc.IsNewPresentation) + public List GetSlides() { - foreach (var slidepart in doc.PresentationSlideParts) + if (!doc.IsNewPresentation) { + foreach (var slidepart in doc.PresentationSlideParts) + { var slide = new Slide(false); - + SlideFacade slideFacade = new SlideFacade(false); slideFacade.TextShapeFacades = TextShapeFacade.PopulateTextShapes(slidepart); slideFacade.RectangleShapeFacades = RectangleShapeFacade.PopulateRectangleShapes(slidepart); + slideFacade.CircleShapeFacades = CircleShapeFacade.PopulateCircleShapes(slidepart); slideFacade.ImagesFacade = ImageFacade.PopulateImages(slidepart); slideFacade.PresentationSlide = slidepart.Slide; - slideFacade.TableFacades= TableFacade.PopulateTables(slidepart); + slideFacade.TableFacades = TableFacade.PopulateTables(slidepart); slideFacade.SlidePart = slidepart; slideFacade.CommentPart = slidepart.SlideCommentsPart; slideFacade.NotesPart = slidepart.NotesSlidePart; @@ -191,19 +191,19 @@ public List GetSlides () slide.SlideFacade = slideFacade; slide.SlidePresentation = this; _Slides.Add(slide); + } } - } return _Slides; - } + } /// /// Extract and save images of a presentation into a director /// /// Folder path as string - public void ExtractAndSaveImages (string outputFolder) - { + public void ExtractAndSaveImages(string outputFolder) + { doc.ExtractAndSaveImages(outputFolder); - } + } /// /// Method to remove a slide at a specific index /// @@ -216,53 +216,53 @@ public void ExtractAndSaveImages (string outputFolder) /// presentation.Save(); /// /// - public String RemoveSlide (int slideIndex) - { + public String RemoveSlide(int slideIndex) + { return doc.RemoveSlide(slideIndex); - } + } /// /// Method to remove comment author. /// /// public void RemoveCommentAuthor(CommentAuthor author) - { + { doc.RemoveCommentAuthor(author.Id); _CommentAuthors.Remove(author); - } + } /// /// Method to insert a slide at a specific index /// /// Index of a slide /// A slide object - public void InsertSlideAt (int index, Slide slide) - { + public void InsertSlideAt(int index, Slide slide) + { slide.SlideIndex = index; slide.SlideFacade.SlideIndex = index; doc.InsertSlide(index, slide.SlideFacade); - } + } /// /// This method exports all existing notes of a PPT/PPTX to TXT file. /// /// File path where to save TXT file public void SaveAllNotesToTextFile(string filePath) - { + { doc.SaveAllNotesToTextFile(filePath); - } + } /// /// Method to save the new or changed presentation. /// - public void Save () - { - doc.Save(); + public void Save() + { + doc.Save(); - } + } /// /// Method to close a presentation. /// public void close() - { + { doc.Dispose(); + } } } -} diff --git a/FileFormat.Slides/Slide.cs b/FileFormat.Slides/Slide.cs index 6a49b31..87ac998 100644 --- a/FileFormat.Slides/Slide.cs +++ b/FileFormat.Slides/Slide.cs @@ -1,7 +1,4 @@ using DocumentFormat.OpenXml; -using DocumentFormat.OpenXml.Bibliography; -using DocumentFormat.OpenXml.Drawing; -using DocumentFormat.OpenXml.Drawing.Diagrams; using FileFormat.Slides.Common; using FileFormat.Slides.Common.Enumerations; using FileFormat.Slides.Facade; @@ -11,12 +8,12 @@ using System.Linq; namespace FileFormat.Slides -{ + { /// /// Represents the slide object within a presentatction /// public class Slide - { + { private SlideFacade _SlideFacade; private String _RelationshipId; private int _SlideIndex; @@ -38,7 +35,7 @@ public class Slide private List _Images; private List _Tables; private static String _BackgroundColor = null; - private CommentFacade _CommentFacade=null; + private CommentFacade _CommentFacade = null; private Presentation _SlidePresentation; private int _CommentIndex = 0; private AnimationType _Animation = AnimationType.None; @@ -138,7 +135,7 @@ public class Slide /// public List Pies { get => _Pies; set => _Pies = value; } - + /// /// Constructor for the Slide class. @@ -146,10 +143,10 @@ public class Slide /// /// it intializes the Slide Facade set the slide index and intializes the lists of text shapes and images. /// - public Slide () - { - try + public Slide() { + try + { _SlideIndex = Utility.SlideNextIndex; Utility.SlideNextIndex += 1; _SlideFacade = new SlideFacade(true); @@ -174,20 +171,20 @@ public Slide () _Tables = new List
(); _SlideFacade.BackgroundColor = _BackgroundColor; _CommentFacade = new CommentFacade(); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Initializing slide"); throw new Common.FileFormatException(errorMessage, ex); - } + } - } + } /// /// Contructor which accepts bool value /// /// - public Slide (bool isNewSlide) - { + public Slide(bool isNewSlide) + { if (isNewSlide) _SlideFacade = new SlideFacade(true); else @@ -213,60 +210,60 @@ public Slide (bool isNewSlide) _Images = new List(); _Tables = new List
(); _CommentFacade = new CommentFacade(); - } + } /// /// Method to add a text shape in a slide. /// /// An object of TextShape class. - public void AddTextShapes (TextShape textShape) - { - try + public void AddTextShapes(TextShape textShape) { - if (textShape.TextList == null) + try { - textShape.Facade = _SlideFacade.AddTextShape(textShape.Text, textShape.FontSize, TextAlignment.Center, + if (textShape.TextList == null) + { + textShape.Facade = _SlideFacade.AddTextShape(textShape.Text, textShape.FontSize, textShape.Alignment, textShape.Anchor, Utility.PixelsToEmu(textShape.X), Utility.PixelsToEmu(textShape.Y) , Utility.PixelsToEmu(textShape.Width), Utility.PixelsToEmu(textShape.Height), textShape.FontFamily, textShape.TextColor, textShape.BackgroundColor); - } + } else - { - textShape.Facade = _SlideFacade.AddTextListShape(textShape.TextList.ListItems, textShape.TextList.Facade, textShape.FontSize, TextAlignment.Center, + { + textShape.Facade = _SlideFacade.AddTextListShape(textShape.TextList.ListItems, textShape.TextList.Facade, textShape.FontSize, textShape.Alignment, textShape.Anchor, Utility.PixelsToEmu(textShape.X), Utility.PixelsToEmu(textShape.Y) , Utility.PixelsToEmu(textShape.Width), Utility.PixelsToEmu(textShape.Height), textShape.FontFamily, textShape.TextColor, textShape.BackgroundColor); - } + } textShape.ShapeIndex = _TextShapes.Count + 1; _TextShapes.Add(textShape); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding text shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a rectangular shape /// /// /// public void DrawRectangle(Rectangle rect) - { - try { + try + { var facade = new RectangleShapeFacade(); facade.BackgroundColor = rect.BackgroundColor; rect.Facade = _SlideFacade.DrawRectangle(Utility.PixelsToEmu(rect.X), Utility.PixelsToEmu(rect.Y), Utility.PixelsToEmu(rect.Width), Utility.PixelsToEmu(rect.Height), facade.BackgroundColor, facade); rect.ShapeIndex = _Rectangles.Count + 1; _Rectangles.Add(rect); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding rectangular shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a triangular shape @@ -274,22 +271,22 @@ public void DrawRectangle(Rectangle rect) /// /// public void DrawTriangle(Triangle triangle) - { - try { + try + { var facade = new TriangleShapeFacade(); facade.BackgroundColor = triangle.BackgroundColor; triangle.Facade = _SlideFacade.DrawTriangle(Utility.PixelsToEmu(triangle.X), Utility.PixelsToEmu(triangle.Y), Utility.PixelsToEmu(triangle.Width), Utility.PixelsToEmu(triangle.Height), facade.BackgroundColor, facade); triangle.ShapeIndex = _Triangles.Count + 1; _Triangles.Add(triangle); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding triangular shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a diamond shape @@ -297,22 +294,22 @@ public void DrawTriangle(Triangle triangle) /// /// public void DrawDiamond(Diamond diamond) - { - try { + try + { var facade = new DiamondShapeFacade(); facade.BackgroundColor = diamond.BackgroundColor; diamond.Facade = _SlideFacade.DrawDiamond(Utility.PixelsToEmu(diamond.X), Utility.PixelsToEmu(diamond.Y), Utility.PixelsToEmu(diamond.Width), Utility.PixelsToEmu(diamond.Height), facade.BackgroundColor, facade); diamond.ShapeIndex = _Diamonds.Count + 1; _Diamonds.Add(diamond); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding diamond shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a line shape @@ -320,21 +317,21 @@ public void DrawDiamond(Diamond diamond) /// /// public void DrawLine(Line line) - { - try { + try + { var facade = new LineFacade(); line.Facade = _SlideFacade.DrawLine(Utility.PixelsToEmu(line.X), Utility.PixelsToEmu(line.Y), Utility.PixelsToEmu(line.Width), Utility.PixelsToEmu(line.Height), facade); line.ShapeIndex = _Lines.Count + 1; _Lines.Add(line); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding line shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a curved line @@ -342,21 +339,21 @@ public void DrawLine(Line line) /// /// public void DrawCurvedLine(CurvedLine curvedLine) - { - try { + try + { var facade = new CurvedLineFacade(); curvedLine.Facade = _SlideFacade.DrawCurvedLine(Utility.PixelsToEmu(curvedLine.X), Utility.PixelsToEmu(curvedLine.Y), Utility.PixelsToEmu(curvedLine.Width), Utility.PixelsToEmu(curvedLine.Height), facade); curvedLine.ShapeIndex = _CurvedLines.Count + 1; _CurvedLines.Add(curvedLine); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding curved line shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw an arrow shape @@ -364,22 +361,22 @@ public void DrawCurvedLine(CurvedLine curvedLine) /// /// public void DrawArrow(Arrow arrow) - { - try { + try + { var facade = new ArrowFacade(); facade.Animation = arrow.Animation; arrow.Facade = _SlideFacade.DrawArrow(Utility.PixelsToEmu(arrow.X), Utility.PixelsToEmu(arrow.Y), Utility.PixelsToEmu(arrow.Width), Utility.PixelsToEmu(arrow.Height), facade); arrow.ShapeIndex = _Arrows.Count + 1; _Arrows.Add(arrow); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding arrow shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a double arrow shape @@ -387,22 +384,22 @@ public void DrawArrow(Arrow arrow) /// /// public void DrawDoubleArrow(DoubleArrow doubleArrow) - { - try { + try + { var facade = new DoubleArrowFacade(); facade.Animation = doubleArrow.Animation; doubleArrow.Facade = _SlideFacade.DrawDoubleArrow(Utility.PixelsToEmu(doubleArrow.X), Utility.PixelsToEmu(doubleArrow.Y), Utility.PixelsToEmu(doubleArrow.Width), Utility.PixelsToEmu(doubleArrow.Height), facade); doubleArrow.ShapeIndex = _DoubleArrows.Count + 1; _DoubleArrows.Add(doubleArrow); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding double arrow shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a double brace shape @@ -410,22 +407,22 @@ public void DrawDoubleArrow(DoubleArrow doubleArrow) /// /// public void DrawDoubleBrace(DoubleBrace doubleBrace) - { - try { + try + { var facade = new DoubleBraceFacade(); facade.Animation = doubleBrace.Animation; doubleBrace.Facade = _SlideFacade.DrawDoubleBrace(Utility.PixelsToEmu(doubleBrace.X), Utility.PixelsToEmu(doubleBrace.Y), Utility.PixelsToEmu(doubleBrace.Width), Utility.PixelsToEmu(doubleBrace.Height), facade); doubleBrace.ShapeIndex = _DoubleBraces.Count + 1; _DoubleBraces.Add(doubleBrace); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding double brace shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a double bracket shape @@ -433,22 +430,22 @@ public void DrawDoubleBrace(DoubleBrace doubleBrace) /// /// public void DrawDoubleBracket(DoubleBracket doubleBracket) - { - try { + try + { var facade = new DoubleBracketFacade(); - facade.Animation= doubleBracket.Animation; + facade.Animation = doubleBracket.Animation; doubleBracket.Facade = _SlideFacade.DrawDoubleBracket(Utility.PixelsToEmu(doubleBracket.X), Utility.PixelsToEmu(doubleBracket.Y), Utility.PixelsToEmu(doubleBracket.Width), Utility.PixelsToEmu(doubleBracket.Height), facade); doubleBracket.ShapeIndex = _DoubleBrackets.Count + 1; _DoubleBrackets.Add(doubleBracket); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding double bracket shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a pentagon shape @@ -456,22 +453,22 @@ public void DrawDoubleBracket(DoubleBracket doubleBracket) /// /// public void DrawPentagon(Pentagon pentagon) - { - try { + try + { var facade = new PentagonFacade(); - facade.Animation= pentagon.Animation; + facade.Animation = pentagon.Animation; pentagon.Facade = _SlideFacade.DrawPentagon(Utility.PixelsToEmu(pentagon.X), Utility.PixelsToEmu(pentagon.Y), Utility.PixelsToEmu(pentagon.Width), Utility.PixelsToEmu(pentagon.Height), facade); pentagon.ShapeIndex = _Pentagons.Count + 1; _Pentagons.Add(pentagon); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding pentagon shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a hexagon shape @@ -479,22 +476,22 @@ public void DrawPentagon(Pentagon pentagon) /// /// public void DrawHexagon(Hexagon hexagon) - { - try { + try + { var facade = new HexagonFacade(); facade.Animation = hexagon.Animation; hexagon.Facade = _SlideFacade.DrawHexagon(Utility.PixelsToEmu(hexagon.X), Utility.PixelsToEmu(hexagon.Y), Utility.PixelsToEmu(hexagon.Width), Utility.PixelsToEmu(hexagon.Height), facade); hexagon.ShapeIndex = _Hexagons.Count + 1; _Hexagons.Add(hexagon); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding hexagon shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a trapezoid shape @@ -502,108 +499,108 @@ public void DrawHexagon(Hexagon hexagon) /// /// public void DrawTrapezoid(Trapezoid trapezoid) - { - try { + try + { var facade = new TrapezoidFacade(); - facade.Animation= trapezoid.Animation; + facade.Animation = trapezoid.Animation; trapezoid.Facade = _SlideFacade.DrawTrapezoid(Utility.PixelsToEmu(trapezoid.X), Utility.PixelsToEmu(trapezoid.Y), Utility.PixelsToEmu(trapezoid.Width), Utility.PixelsToEmu(trapezoid.Height), facade); trapezoid.ShapeIndex = _Trapezoids.Count + 1; _Trapezoids.Add(trapezoid); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding trapezoid shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } public void DrawPie(Pie pie) - { - try { - var Facade= new PieFacade(); - Facade.Animation= pie.Animation; - pie.Facade = _SlideFacade.DrawPie(Utility.PixelsToEmu(pie.X), Utility.PixelsToEmu(pie.Y), - Utility.PixelsToEmu(pie.Width), Utility.PixelsToEmu(pie.Height), Facade); + try + { + var Facade = new PieFacade(); + Facade.Animation = pie.Animation; + pie.Facade = _SlideFacade.DrawPie(Utility.PixelsToEmu(pie.X), Utility.PixelsToEmu(pie.Y), + Utility.PixelsToEmu(pie.Width), Utility.PixelsToEmu(pie.Height), Facade); pie.ShapeIndex = _Pies.Count + 1; Pies.Add(pie); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding text shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to draw a circular shape /// /// /// public void DrawCircle(Circle circle) - { - try { + try + { var facade = new CircleShapeFacade(); - facade.Animation= circle.Animation; + facade.Animation = circle.Animation; facade.BackgroundColor = circle.BackgroundColor; circle.Facade = _SlideFacade.DrawCircle(Utility.PixelsToEmu(circle.X), Utility.PixelsToEmu(circle.Y), Utility.PixelsToEmu(circle.Width), Utility.PixelsToEmu(circle.Height), facade.BackgroundColor, facade); circle.ShapeIndex = _Circles.Count + 1; _Circles.Add(circle); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding circular shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to add/update note to a slide /// /// Text you want to add as note public void AddNote(String noteText) - { + { _SlideFacade.AddNote(noteText); - } + } /// /// Method to remove Notes of a slide /// public void RemoveNote() - { + { _SlideFacade.RemoveNote(); - } - public void AddTextShapes (TextShape textShape, List textSegments) - { - try + } + public void AddTextShapes(TextShape textShape, List textSegments) { + try + { List textSegmentFacades = new List(); foreach (var ts in textSegments) - { + { textSegmentFacades.Add(ts.Facade); - } - textShape.Facade = _SlideFacade.AddTextShape(textSegmentFacades, TextAlignment.Center, + } + textShape.Facade = _SlideFacade.AddTextShape(textSegmentFacades, textShape.Alignment, textShape.Anchor, Utility.PixelsToEmu(textShape.X), Utility.PixelsToEmu(textShape.Y) , Utility.PixelsToEmu(textShape.Width), Utility.PixelsToEmu(textShape.Height), textShape.BackgroundColor); textShape.ShapeIndex = _TextShapes.Count + 1; _TextShapes.Add(textShape); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding text shape"); throw new Common.FileFormatException(errorMessage, ex); + } } - } /// /// Method to add images to a slide. /// /// An object of Image class - public void AddImage (Image image) - { - try + public void AddImage(Image image) { + try + { image.Facade = new ImageFacade(); image.Facade.X = Utility.PixelsToEmu(image.X); image.Facade.Y = Utility.PixelsToEmu(image.Y); @@ -613,52 +610,52 @@ public void AddImage (Image image) _SlideFacade.AddImage(image.Facade); image.ImageIndex = _Images.Count + 1; _Images.Add(image); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding image"); throw new Common.FileFormatException(errorMessage, ex); - } + } - } + } /// /// Method to add comments to a slide. /// /// An object of Comment class public void AddComment(Comment comment) - { - try { + try + { comment.Facade = _CommentFacade; if (_CommentIndex == 0) - { + { comment.Facade.SetAssociatedSlidePart(_SlideFacade.SlidePart, _SlidePresentation.Facade.CommentAuthorPart); - } + } UInt32Value id = new UInt32Value { Value = (uint)comment.AuthorId }; - comment.Facade.GenerateComment(id,comment.Text, comment.InsertedAt, comment.X, comment.Y); + comment.Facade.GenerateComment(id, comment.Text, comment.InsertedAt, comment.X, comment.Y); _CommentIndex += 1; - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding Comment"); throw new Common.FileFormatException(errorMessage, ex); - } + } - } + } /// /// Method to get the list of comments. /// /// public List GetComments() - { - var comments= new List(); - var facadeComments= _SlideFacade.GetComments(); - foreach (var facadeComment in facadeComments) { + var comments = new List(); + var facadeComments = _SlideFacade.GetComments(); + foreach (var facadeComment in facadeComments) + { Comment comment = new Comment(); comment.Text = facadeComment["Text"]; comment.AuthorId = Convert.ToInt32(facadeComment["AuthorId"]); - comment.CommentIndex= Convert.ToInt32(facadeComment["Index"]); + comment.CommentIndex = Convert.ToInt32(facadeComment["Index"]); comment.InsertedAt = Convert.ToDateTime(facadeComment["DateTime"]); comment.X = Convert.ToInt64(facadeComment["XPos"]); comment.Y = Convert.ToInt64(facadeComment["YPos"]); @@ -666,66 +663,66 @@ public List GetComments() comment.Facade.CommentPart = _SlideFacade.CommentPart; comments.Add(comment); - } + } return comments; - } + } /// /// Method to add table to a slide. /// /// An object of Table class - public void AddTable (Table table) - { - try + public void AddTable(Table table) { + try + { table.Facade = new TableFacade(); table.Facade.X = Utility.PixelsToEmu(table.X); table.Facade.Y = Utility.PixelsToEmu(table.Y); table.Facade.Width = Utility.PixelsToEmu(table.Width); table.Facade.Height = Utility.PixelsToEmu(table.Height); if (table.Theme == null) - { + { table.Theme = Table.TableStyle.LightStyle1; - } - + } + table.Facade.GenerateTable(_SlideFacade.SlidePart, table.GetDataTable()); table.TableIndex = _Tables.Count + 1; _Tables.Add(table); - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Adding table"); throw new Common.FileFormatException(errorMessage, ex); - } + } - } + } /// /// Get text shapes by searching a text term. /// /// Search term as string /// - public List GetTextShapesByText (String text) - { - try + public List GetTextShapesByText(String text) { + try + { List shapes = TextShapes.Where(shape => shape.Text.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0).ToList(); return shapes; - } + } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Getting Shapes"); throw new Common.FileFormatException(errorMessage, ex); + } } - } - + /// /// Method to update a slide properties e.g. background color. /// - public void Update () - { + public void Update() + { _SlideFacade.BackgroundColor = _BackgroundColor; _SlideFacade.Update(); - } + } + } } -} diff --git a/FileFormat.Slides/TextShape.cs b/FileFormat.Slides/TextShape.cs index 7d55763..d9d52a4 100644 --- a/FileFormat.Slides/TextShape.cs +++ b/FileFormat.Slides/TextShape.cs @@ -5,15 +5,16 @@ using System.Collections.Generic; namespace FileFormat.Slides -{ + { /// /// This class represents the text shape within a slide. /// public class TextShape - { + { private String _Text; private Int32 _FontSize; private TextAlignment _Alignment = TextAlignment.None; + private TextAnchor _Anchor = TextAnchor.None; private double _x; private double _y; private double _Width; @@ -38,6 +39,10 @@ public class TextShape /// public TextAlignment Alignment { get => _Alignment; set => _Alignment = value; } /// + /// Property to get or set anchor of the shape. + /// + public TextAnchor Anchor { get => _Anchor; set => _Anchor = value; } + /// /// Property to get or set X coordinate of the shape /// public double X { get => _x; set => _x = value; } @@ -87,8 +92,8 @@ public class TextShape /// /// Constructor of the TextShape class inititalizes the object of TextShapeFacade and populate its fields. /// - public TextShape () - { + public TextShape() + { _Facade = new TextShapeFacade(); _Facade.ShapeIndex = _shapeIndex; _Text = "Default Text"; @@ -97,30 +102,32 @@ public TextShape () _TextColor = "000000"; _BackgroundColor = "Transparent"; _Alignment = TextAlignment.Center; + _Anchor = TextAnchor.Center; _x = Utility.EmuToPixels(1349828); _y = Utility.EmuToPixels(1999619); _Width = Utility.EmuToPixels(6000000); _Height = Utility.EmuToPixels(2000000); _TextList = null; Populate_Facade(); - } + } /// /// Method to update text shape. /// - public void Update () - { + public void Update() + { Populate_Facade(); _Facade.UpdateShape(); - } + } /// /// Method to populate the fields of respective facade. /// - private void Populate_Facade () - { + private void Populate_Facade() + { _Facade.Text = _Text; _Facade.FontSize = _FontSize; _Facade.Alignment = _Alignment; + _Facade.Anchor = _Anchor; _Facade.TextColor = _TextColor; _Facade.BackgroundColor = _BackgroundColor; _Facade.FontFamily = _FontFamily; @@ -129,24 +136,25 @@ private void Populate_Facade () _Facade.Width = Utility.PixelsToEmu(_Width); _Facade.Height = Utility.PixelsToEmu(_Height); _Facade.TextList = _TextList?.Facade; - } + } /// /// Method for getting the list of text shapes. /// /// An object of TextShapeFacade. /// - public static List GetTextShapes (List textShapeFacades) - { + public static List GetTextShapes(List textShapeFacades) + { List textShapes = new List(); try - { - foreach (var facade in textShapeFacades) { - TextShape textShape = new TextShape + foreach (var facade in textShapeFacades) { + TextShape textShape = new TextShape + { Text = facade.Text, FontSize = facade.FontSize, Alignment = facade.Alignment, + Anchor = facade.Anchor, FontFamily = facade.FontFamily, TextColor = facade.TextColor, BackgroundColor = facade.BackgroundColor, @@ -157,29 +165,29 @@ public static List GetTextShapes (List textShapeFaca Facade = facade, TextList = GetTextList(facade?.TextList), ShapeIndex = facade.ShapeIndex - }; + }; textShapes.Add(textShape); + } } - } catch (Exception ex) - { + { string errorMessage = Common.FileFormatException.ConstructMessage(ex, "Getting Text Shapes"); throw new Common.FileFormatException(errorMessage, ex); - } + } return textShapes; - } + } /// /// This method is used to get the StyledList from a text shape /// /// /// - private static StyledList GetTextList (ListFacade facade) - { - if (facade != null) + private static StyledList GetTextList(ListFacade facade) { + if (facade != null) + { StyledList list = new StyledList(facade.ListType); list.ListItems = facade.ListItems; list.TextColor = facade.TextColor; @@ -187,15 +195,15 @@ private static StyledList GetTextList (ListFacade facade) list.FontSize = facade.FontSize; list.Facade = facade; return list; - } + } return null; - } + } /// /// Method to remove the textshape of a slide. /// - public void Remove () - { + public void Remove() + { _Facade.RemoveShape(this.Facade.TextBoxShape); + } } } -}