Skip to content

Commit 26ddea2

Browse files
Merge pull request #30 from openize-com/muhammadumar-patch
Updated Presentation.cs
2 parents 23cc80c + 08fea58 commit 26ddea2

File tree

1 file changed

+146
-78
lines changed

1 file changed

+146
-78
lines changed

PowerPoint/Openize.Slides/Presentation.cs

Lines changed: 146 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Openize.Slides.Facade;
44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67

78
namespace Openize.Slides
89
{
@@ -15,78 +16,60 @@ public class Presentation
1516
private List<CommentAuthor> _CommentAuthors = null;
1617
private int _SlideWidth = 960;
1718
private int _SlideHeight = 720;
18-
private string _FilePath = null;
1919

20+
public PresentationDocumentFacade Facade => doc;
2021
/// <summary>
21-
/// Gets the facade for handling presentation document operations.
22-
/// </summary>
23-
public PresentationDocumentFacade Facade { get => doc; }
24-
25-
/// <summary>
26-
/// Gets or sets the slide width of the presentation.
22+
/// Set slide width
2723
/// </summary>
2824
public int SlideWidth { get => _SlideWidth; set => _SlideWidth = value; }
29-
3025
/// <summary>
31-
/// Gets or sets the slide height of the presentation.
26+
/// Set slide height
3227
/// </summary>
3328
public int SlideHeight { get => _SlideHeight; set => _SlideHeight = value; }
3429

35-
/// <summary>
36-
/// Gets or sets the file path of the presentation.
37-
/// </summary>
38-
public string FilePath { get => _FilePath; set => _FilePath = value; }
39-
40-
/// <summary>
41-
/// Initializes a new presentation document with the specified file path.
42-
/// </summary>
43-
/// <param name="FilePath">The file path for the presentation.</param>
4430
private Presentation(String FilePath)
4531
{
32+
_Slides = new List<Slide>();
33+
_CommentAuthors = new List<CommentAuthor>();
34+
4635
try
4736
{
48-
_FilePath = FilePath;
49-
_Slides = new List<Slide>();
50-
_CommentAuthors = new List<CommentAuthor>();
5137
doc = PresentationDocumentFacade.Create(FilePath);
5238
}
5339
catch (Exception ex)
5440
{
55-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Initializing presentation");
56-
throw new Common.OpenizeException(errorMessage, ex);
41+
throw new Common.OpenizeException($"Failed to create presentation at path '{FilePath}'.", ex);
5742
}
5843
}
5944

45+
private Presentation()
46+
{
47+
_Slides = new List<Slide>();
48+
_CommentAuthors = new List<CommentAuthor>();
49+
}
50+
6051
/// <summary>
61-
/// Initializes an empty presentation.
52+
/// Static method to instantiate a new object of Presentation class.
6253
/// </summary>
63-
private Presentation()
54+
/// <param name="FilePath">Presentation path as string</param>
55+
/// <returns>An instance of Presentation object</returns>
56+
public static Presentation Create(String FilePath)
6457
{
6558
try
6659
{
67-
_Slides = new List<Slide>();
68-
_CommentAuthors = new List<CommentAuthor>();
60+
return new Presentation(FilePath);
6961
}
7062
catch (Exception ex)
7163
{
72-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Initializing empty presentation");
73-
throw new Common.OpenizeException(errorMessage, ex);
64+
throw new Common.OpenizeException($"Error occurred while creating a new presentation: {FilePath}", ex);
7465
}
7566
}
7667

7768
/// <summary>
78-
/// Creates a new presentation instance with the specified file path.
69+
/// Static method to load an existing presentation.
7970
/// </summary>
80-
/// <param name="FilePath">The file path for the new presentation.</param>
81-
public static Presentation Create(String FilePath)
82-
{
83-
return new Presentation(FilePath);
84-
}
85-
86-
/// <summary>
87-
/// Opens an existing presentation file.
88-
/// </summary>
89-
/// <param name="FilePath">The file path of the presentation to open.</param>
71+
/// <param name="FilePath">Presentation path as string</param>
72+
/// <returns>Instance of Presentation object</returns>
9073
public static Presentation Open(String FilePath)
9174
{
9275
try
@@ -96,15 +79,14 @@ public static Presentation Open(String FilePath)
9679
}
9780
catch (Exception ex)
9881
{
99-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Opening presentation");
100-
throw new Common.OpenizeException(errorMessage, ex);
82+
throw new Common.OpenizeException($"Error occurred while opening the presentation: {FilePath}", ex);
10183
}
10284
}
10385

10486
/// <summary>
105-
/// Appends a slide to the presentation.
87+
/// This method is responsible to append a slide.
10688
/// </summary>
107-
/// <param name="slide">The slide object to append.</param>
89+
/// <param name="slide">An object of a slide</param>
10890
public void AppendSlide(Slide slide)
10991
{
11092
try
@@ -117,84 +99,170 @@ public void AppendSlide(Slide slide)
11799
}
118100
catch (Exception ex)
119101
{
120-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Appending slide");
121-
throw new Common.OpenizeException(errorMessage, ex);
102+
throw new Common.OpenizeException("Failed to append slide to presentation.", ex);
122103
}
123104
}
105+
/// <summary>
106+
/// Method to get the list of all slides of a presentation
107+
/// </summary>
108+
/// <returns></returns>
109+
/// <example>
110+
/// <code>
111+
/// Presentation presentation = Presentation.Open("D:\\AsposeSampleData\\sample.pptx");
112+
/// var slides = presentation.GetSlides();
113+
/// var slide = slides[0];
114+
/// ...
115+
/// </code>
116+
/// </example>
117+
public List<Slide> GetSlides()
118+
{
119+
if (!doc.IsNewPresentation)
120+
{
121+
foreach (var slidepart in doc.PresentationSlideParts)
122+
{
123+
var slide = new Slide(false);
124+
125+
SlideFacade slideFacade = new SlideFacade(false);
126+
slideFacade.TextShapeFacades = TextShapeFacade.PopulateTextShapes(slidepart);
127+
slideFacade.RectangleShapeFacades = RectangleShapeFacade.PopulateRectangleShapes(slidepart);
128+
slideFacade.ImagesFacade = ImageFacade.PopulateImages(slidepart);
129+
slideFacade.PresentationSlide = slidepart.Slide;
130+
slideFacade.TableFacades = TableFacade.PopulateTables(slidepart);
131+
slideFacade.SlidePart = slidepart;
132+
slideFacade.CommentPart = slidepart.SlideCommentsPart;
133+
slideFacade.NotesPart = slidepart.NotesSlidePart;
134+
slideFacade.RelationshipId = doc.GetSlideRelationshipId(slidepart);
135+
slide.TextShapes = TextShape.GetTextShapes(slideFacade.TextShapeFacades);
136+
slide.Rectangles = Rectangle.GetRectangles(slideFacade.RectangleShapeFacades);
137+
slide.Circles = Circle.GetCircles(slideFacade.CircleShapeFacades);
138+
slide.Images = Image.GetImages(slideFacade.ImagesFacade);
139+
slide.Tables = Table.GetTables(slideFacade.TableFacades);
140+
slide.SlideFacade = slideFacade;
141+
slide.SlidePresentation = this;
142+
_Slides.Add(slide);
143+
}
144+
}
145+
return _Slides;
146+
147+
}
124148

125149
/// <summary>
126-
/// Copies a slide from another presentation.
150+
/// Method to save the new or changed presentation.
127151
/// </summary>
128-
/// <param name="slide">The slide object to copy.</param>
129-
public void CopySlide(Slide slide)
152+
public void Save()
130153
{
131154
try
132155
{
133-
doc.CopySlide(slide.SlideFacade);
134-
_Slides.Add(slide);
156+
doc.Save();
135157
}
136158
catch (Exception ex)
137159
{
138-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Copying slide");
139-
throw new Common.OpenizeException(errorMessage, ex);
160+
throw new Common.OpenizeException("Failed to save the presentation.", ex);
140161
}
141162
}
142163

143164
/// <summary>
144-
/// Retrieves all slides from the presentation.
165+
/// Method to close a presentation.
145166
/// </summary>
146-
public List<Slide> GetSlides()
167+
public void close()
147168
{
148169
try
149170
{
150-
if (!doc.IsNewPresentation)
151-
{
152-
foreach (var slidepart in doc.PresentationSlideParts)
153-
{
154-
var slide = new Slide(false);
155-
slide.SlideFacade = new SlideFacade(false);
156-
_Slides.Add(slide);
157-
}
158-
}
159-
return _Slides;
171+
doc.Dispose();
160172
}
161173
catch (Exception ex)
162174
{
163-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Getting slides");
164-
throw new Common.OpenizeException(errorMessage, ex);
175+
throw new Common.OpenizeException("Failed to close the presentation.", ex);
165176
}
166177
}
167178

168179
/// <summary>
169-
/// Saves the presentation document.
180+
/// Extract and save images of a presentation into a directory.
170181
/// </summary>
171-
public void Save()
182+
/// <param name="outputFolder">Folder path as string</param>
183+
public void ExtractAndSaveImages(string outputFolder)
172184
{
173185
try
174186
{
175-
doc.Close(FilePath);
187+
doc.ExtractAndSaveImages(outputFolder);
176188
}
177189
catch (Exception ex)
178190
{
179-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Saving presentation");
180-
throw new Common.OpenizeException(errorMessage, ex);
191+
throw new Common.OpenizeException($"Failed to extract and save images to folder '{outputFolder}'.", ex);
181192
}
182193
}
183194

184195
/// <summary>
185-
/// Closes the presentation document.
196+
/// Method to remove a slide at a specific index.
186197
/// </summary>
187-
public void Close()
198+
/// <param name="slideIndex">Index of a slide</param>
199+
public String RemoveSlide(int slideIndex)
188200
{
189201
try
190202
{
191-
doc.Close(FilePath);
203+
return doc.RemoveSlide(slideIndex);
192204
}
193205
catch (Exception ex)
194206
{
195-
string errorMessage = Common.OpenizeException.ConstructMessage(ex, "Closing presentation");
196-
throw new Common.OpenizeException(errorMessage, ex);
207+
throw new Common.OpenizeException($"Failed to remove slide at index {slideIndex}.", ex);
208+
}
209+
}
210+
/// <summary>
211+
/// Create comment author using this method
212+
/// </summary>
213+
/// <param name="author"> Pass comment author object</param>
214+
public void CreateAuthor(CommentAuthor author)
215+
{
216+
doc.CreateAuthor(author.Id, author.ColorIndex, author.Name, author.InitialLetter);
217+
_CommentAuthors.Add(author);
218+
}
219+
/// <summary>
220+
/// Get the list of comment author
221+
/// </summary>
222+
/// <returns></returns>
223+
public List<CommentAuthor> GetCommentAuthors()
224+
{
225+
List<CommentAuthor> authorList = new List<CommentAuthor>();
226+
var FacadeAuthors = doc.GetCommentAuthors();
227+
foreach (var author in FacadeAuthors)
228+
{
229+
CommentAuthor commentAuthor = new CommentAuthor();
230+
commentAuthor.InitialLetter = author["Initials"];
231+
commentAuthor.ColorIndex = Convert.ToInt32(author["ColorIndex"]);
232+
commentAuthor.Name = author["Name"];
233+
commentAuthor.Id = Convert.ToInt32(author["Id"]);
234+
authorList.Add(commentAuthor);
197235
}
236+
return authorList;
237+
}
238+
/// <summary>
239+
/// Method to remove comment author.
240+
/// </summary>
241+
/// <param name="author"></param>
242+
public void RemoveCommentAuthor(CommentAuthor author)
243+
{
244+
doc.RemoveCommentAuthor(author.Id);
245+
_CommentAuthors.Remove(author);
246+
}
247+
/// <summary>
248+
/// Method to insert a slide at a specific index
249+
/// </summary>
250+
/// <param name="index">Index of a slide</param>
251+
/// <param name="slide">A slide object</param>
252+
public void InsertSlideAt(int index, Slide slide)
253+
{
254+
slide.SlideIndex = index;
255+
slide.SlideFacade.SlideIndex = index;
256+
doc.InsertSlide(index, slide.SlideFacade);
257+
}
258+
259+
/// <summary>
260+
/// This method exports all existing notes of a PPT/PPTX to TXT file.
261+
/// </summary>
262+
/// <param name="filePath"> File path where to save TXT file</param>
263+
public void SaveAllNotesToTextFile(string filePath)
264+
{
265+
doc.SaveAllNotesToTextFile(filePath);
198266
}
199267
}
200-
}
268+
}

0 commit comments

Comments
 (0)