Skip to content

Commit 08fd35d

Browse files
Released 24.1.0
1 parent bcbd58c commit 08fd35d

File tree

3 files changed

+252
-6
lines changed

3 files changed

+252
-6
lines changed

FileFormat.Slides.Examples.Usage/Program.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,35 @@ class Program
77
{
88
static void Main (string[ ] args)
99
{
10-
// SlideExamples slideExamples = new SlideExamples();
11-
// slideExamples.CreateNewSlideInNewPresentation();
10+
//SlideExamples slideExamples = new SlideExamples();
11+
12+
//slideExamples.CreateNewSlideInNewPresentation();
1213

13-
// slideExamples.CreateNewSlideInExistingPresentation(filename:"test.pptx");
14+
//slideExamples.CreateNewSlideInExistingPresentation(filename:"test.pptx");
1415

15-
// slideExamples.RemoveSlideInAnExistingPresentation(filename: "test.pptx");
16+
//slideExamples.RemoveSlideInAnExistingPresentation(filename: "test.pptx");
17+
18+
//slideExamples.AddBackgroundColorToAnExistingSlide(filename: "sample.pptx");
1619

1720
//TextExamples textExamples = new TextExamples();
21+
1822
//textExamples.CreateNewTextShapeInNewSlide();
23+
1924
//textExamples.AddNewTextShapeExistingSlide(filename: "sample.pptx");
2025

21-
ImageExamples imageExamples = new ImageExamples();
26+
//ImageExamples imageExamples = new ImageExamples();
27+
2228
//imageExamples.AddImageInASlide(imagename:"sample.jpg");
23-
imageExamples.UpdateImageInExistingSlide(filename: "sample.pptx", xAxis: 300.0, yAxis: 200.0);
29+
30+
//imageExamples.UpdateImageInExistingSlide(filename: "sample.pptx", xAxis: 300.0, yAxis: 200.0);
2431

32+
//StyledListExamples styledListExamples = new StyledListExamples();
33+
34+
//styledListExamples.CreateBulletedListInASlide();
35+
36+
//styledListExamples.AddListItemsInAnExistingList(filename: "test.pptx");
37+
38+
//styledListExamples.RemoveListItemsInAnExistingList(filename: "test.pptx");
2539
}
2640
}
2741
}

FileFormat.Slides.Examples/SlideExamples.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,29 @@ public void RemoveSlideInAnExistingPresentation (string documentDirectory = exis
135135
throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex);
136136
}
137137
}
138+
public void AddBackgroundColorToAnExistingSlide (string documentDirectory = existingDocsDirectory, string filename = "test.pptx")
139+
{
140+
141+
try
142+
{
143+
// Create instance of presentation
144+
Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}");
145+
// Get desired slide
146+
Slide slide = presentation.GetSlides()[0];
147+
// Set background color
148+
slide.BackgroundColor = Colors.Fuchsia;
149+
// Update slide
150+
slide.Update();
151+
// Save presentation
152+
presentation.Save();
153+
154+
}
155+
catch (System.Exception ex)
156+
{
157+
throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex);
158+
}
159+
160+
}
138161
}
162+
139163
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using FileFormat.Slides.Common;
2+
using FileFormat.Slides.Common.Enumerations;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace FileFormat.Slides.Examples
8+
{
9+
/// <summary>
10+
/// Provides C# code examples for creating, reading, and modifying slides in a Presentation
11+
/// using the <a href="https://www.nuget.org/packages/FileFormat.Slides">FileFormat.Slides</a> library.
12+
/// </summary>
13+
public class StyledListExamples
14+
{
15+
private const string newDocsDirectory = "../../../Presentations/New";
16+
private const string existingDocsDirectory = "../../../Presentations/Existing";
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="SlideExamples"/> class.
19+
/// Prepares the directory 'Presentations/New' for storing or loading PowerPoint(PPT or PPTX) presentations
20+
/// at the root of the project.
21+
/// If the directory doesn't exist, it is created. If it already exists,
22+
/// existing files are deleted, and the directory is cleaned up.
23+
/// </summary>
24+
public StyledListExamples ()
25+
{
26+
if (!System.IO.Directory.Exists(newDocsDirectory))
27+
{
28+
// If it doesn't exist, create the directory
29+
System.IO.Directory.CreateDirectory(newDocsDirectory);
30+
System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " +
31+
$"created successfully.");
32+
}
33+
else
34+
{
35+
var files = System.IO.Directory.GetFiles(System.IO.Path.GetFullPath(newDocsDirectory));
36+
foreach (var file in files)
37+
{
38+
System.IO.File.Delete(file);
39+
System.Console.WriteLine($"File deleted: {file}");
40+
}
41+
System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(newDocsDirectory)}' " +
42+
$"cleaned up.");
43+
}
44+
}
45+
/// <summary>
46+
/// This method creates bulleted list in a slide
47+
/// </summary>
48+
/// <param name="documentDirectory">Path of the presentation folder</param>
49+
/// <param name="filename">Presentation name</param>
50+
public void CreateBulletedListInASlide (string documentDirectory = newDocsDirectory, string filename = "test.pptx")
51+
{
52+
try
53+
{
54+
// Create instance of presentation
55+
Presentation presentation = Presentation.Create($"{documentDirectory}/{filename}");
56+
//Create instances of text shapes and set their properties.
57+
TextShape shape = new TextShape();
58+
shape.Y = 20.0;
59+
shape.FontSize = 80;
60+
shape.FontFamily = "Baguet Script";
61+
shape.Text = "Bulleted List Example";
62+
TextShape shape2 = new TextShape();
63+
shape2.FontSize = 100;
64+
shape2.Y = 180.0;
65+
// Create instance of Bulleted List
66+
StyledList list = new StyledList(ListType.Bulleted);
67+
// Add items to list
68+
list.AddListItem("USA");
69+
list.AddListItem("Canada");
70+
list.AddListItem("Brazil");
71+
list.AddListItem("Mexico");
72+
// Assign list to text shape
73+
shape2.TextList = list;
74+
// Create slide
75+
Slide slide = new Slide();
76+
// Set background color of slide because default background color is black.
77+
slide.BackgroundColor = Colors.Silver;
78+
// Add text shapes.
79+
slide.AddTextShapes(shape);
80+
slide.AddTextShapes(shape2);
81+
// Adding slides
82+
presentation.AppendSlide(slide);
83+
// Save presentation
84+
presentation.Save();
85+
86+
}
87+
catch (System.Exception ex)
88+
{
89+
throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex);
90+
}
91+
}
92+
/// <summary>
93+
/// This method creates numbered list in a slide
94+
/// </summary>
95+
/// <param name="documentDirectory">Path of the presentation folder</param>
96+
/// <param name="filename">Presentation name</param>
97+
public void CreateNumberedListInASlide (string documentDirectory = existingDocsDirectory, string filename = "test.pptx")
98+
{
99+
try
100+
{
101+
102+
// Create instance of presentation
103+
Presentation presentation = Presentation.Create($"{documentDirectory}/{filename}");
104+
//Create instances of text shapes and set their properties.
105+
TextShape shape = new TextShape();
106+
shape.Y = 20.0;
107+
shape.FontSize = 80;
108+
shape.FontFamily = "Amasis MT Pro Black";
109+
shape.Text = "Numbered List Example";
110+
TextShape shape2 = new TextShape();
111+
shape2.FontSize = 100;
112+
shape2.Y = 180.0;
113+
// Create instance of Bulleted List
114+
StyledList list = new StyledList(ListType.Bulleted);
115+
// Add items to list
116+
list.AddListItem("Tennis");
117+
list.AddListItem("Cricket");
118+
list.AddListItem("Hockey");
119+
list.AddListItem("Football");
120+
list.AddListItem("Snooker");
121+
// Assign list to text shape
122+
shape2.TextList = list;
123+
// Create slide
124+
Slide slide = new Slide();
125+
// Set background color of slide because default background color is black.
126+
slide.BackgroundColor = Colors.Silver;
127+
// Add text shapes.
128+
slide.AddTextShapes(shape);
129+
slide.AddTextShapes(shape2);
130+
// Adding slides
131+
presentation.AppendSlide(slide);
132+
// Save presentation
133+
presentation.Save();
134+
135+
}
136+
catch (System.Exception ex)
137+
{
138+
throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex);
139+
}
140+
}
141+
/// <summary>
142+
/// This method adds the list items in an existing numbered or bulleted list.
143+
/// </summary>
144+
/// <param name="documentDirectory">Path of the presentation folder</param>
145+
/// <param name="filename">Presentation name</param>
146+
public void AddListItemsInAnExistingList(string documentDirectory = existingDocsDirectory, string filename = "test.pptx")
147+
{
148+
try
149+
{
150+
// Create instance of presentation
151+
Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}");
152+
// Get desired slide
153+
Slide slide = presentation.GetSlides()[3];
154+
// Create instance of Bulleted List
155+
TextShape shape = slide.TextShapes[0];
156+
StyledList list = shape.TextList;
157+
// Add items to list
158+
list.AddListItem("Tennis");
159+
list.AddListItem("Cricket");
160+
list.AddListItem("Hockey");
161+
list.AddListItem("Football");
162+
list.AddListItem("Snooker");
163+
// Update the list
164+
list.Update();
165+
// Save presentation
166+
presentation.Save();
167+
168+
}
169+
catch (System.Exception ex)
170+
{
171+
throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex);
172+
}
173+
}
174+
/// <summary>
175+
/// This method removes the list items in an existing numbered or bulleted list.
176+
/// </summary>
177+
/// <param name="documentDirectory">Path of the presentation folder</param>
178+
/// <param name="filename">Presentation name</param>
179+
public void RemoveListItemsInAnExistingList (string documentDirectory = existingDocsDirectory, string filename = "test.pptx")
180+
{
181+
try
182+
{
183+
// Create instance of presentation
184+
Presentation presentation = Presentation.Open($"{documentDirectory}/{filename}");
185+
// Get desired slide
186+
Slide slide = presentation.GetSlides()[3];
187+
// Create instance of Bulleted List
188+
TextShape shape = slide.TextShapes[0];
189+
StyledList list = shape.TextList;
190+
// Remove a range of list items
191+
list.ListItems.RemoveRange(0,4);
192+
// Or you can remove all items like
193+
list.ListItems.RemoveAt(1);
194+
// Update the list
195+
list.Update();
196+
// Save presentation
197+
presentation.Save();
198+
199+
}
200+
catch (System.Exception ex)
201+
{
202+
throw new FileFormat.Slides.Common.FileFormatException("An error occurred.", ex);
203+
}
204+
}
205+
206+
}
207+
208+
}

0 commit comments

Comments
 (0)