Skip to content

Commit 73b8554

Browse files
Added the sample Iterate-slide-elements
1 parent 87b9478 commit 73b8554

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modify-PowerPoint-elements", "Modify-PowerPoint-elements\Modify-PowerPoint-elements.csproj", "{BC2C1CE5-4219-4073-99D4-91E543250166}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{BC2C1CE5-4219-4073-99D4-91E543250166}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BC2C1CE5-4219-4073-99D4-91E543250166}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BC2C1CE5-4219-4073-99D4-91E543250166}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BC2C1CE5-4219-4073-99D4-91E543250166}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Iterate_slide_elements</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Presentation.NET" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.pptx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using Syncfusion.Presentation;
2+
using Syncfusion.OfficeChart;
3+
4+
class Program
5+
{
6+
public static void Main(string[] args)
7+
{
8+
// Load or open a PowerPoint presentation
9+
using (FileStream inputStream = new(Path.GetFullPath(@"Data/Template.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
10+
{
11+
using (IPresentation presentation = Presentation.Open(inputStream))
12+
{
13+
// Iterate through each slide in the presentation
14+
foreach (ISlide slide in presentation.Slides)
15+
{
16+
// Iterate through each shape in the slide
17+
foreach (IShape shape in slide.Shapes)
18+
{
19+
// Modify the shape properties (text, size, hyperlinks, etc.)
20+
ModifySlideElements(shape, presentation);
21+
}
22+
}
23+
24+
// Save the modified presentation to an output file
25+
using (FileStream outputStream = new(Path.GetFullPath(@"../../../Output/Result.pptx"), FileMode.Create, FileAccess.ReadWrite))
26+
{
27+
presentation.Save(outputStream);
28+
}
29+
}
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Modifies slide elements based on their type.
35+
/// </summary>
36+
private static void ModifySlideElements(IShape shape, IPresentation presentation)
37+
{
38+
switch (shape.SlideItemType)
39+
{
40+
case SlideItemType.AutoShape:
41+
{
42+
// Modify text if present in the shape
43+
if (!string.IsNullOrEmpty(shape.TextBody.Text))
44+
{
45+
ModifyTextPart(shape.TextBody);
46+
}
47+
// If shape is a rectangle, add a hyperlink
48+
else if (shape.AutoShapeType == AutoShapeType.Rectangle)
49+
{
50+
shape.SetHyperlink("www.google.com");
51+
}
52+
break;
53+
}
54+
55+
case SlideItemType.Placeholder:
56+
{
57+
// Modify text if present in the placeholder
58+
if (!string.IsNullOrEmpty(shape.TextBody.Text))
59+
{
60+
ModifyTextPart(shape.TextBody);
61+
}
62+
break;
63+
}
64+
65+
case SlideItemType.Picture:
66+
{
67+
// Resize the picture
68+
IPicture picture = shape as IPicture;
69+
picture.Height = 160;
70+
picture.Width = 130;
71+
break;
72+
}
73+
74+
case SlideItemType.Table:
75+
{
76+
// Get the table shape
77+
ITable table = shape as ITable;
78+
79+
// Iterate through rows and modify text in each cell
80+
foreach (IRow row in table.Rows)
81+
{
82+
foreach (ICell cell in row.Cells)
83+
{
84+
ModifyTextPart(cell.TextBody);
85+
}
86+
}
87+
break;
88+
}
89+
90+
case SlideItemType.GroupShape:
91+
{
92+
// Get the group shape and iterate through child shapes
93+
IGroupShape groupShape = shape as IGroupShape;
94+
foreach (IShape childShape in groupShape.Shapes)
95+
{
96+
ModifySlideElements(childShape, presentation);
97+
}
98+
break;
99+
}
100+
101+
case SlideItemType.Chart:
102+
{
103+
// Modify chart properties
104+
IPresentationChart chart = shape as IPresentationChart;
105+
chart.ChartTitle = "Purchase Details";
106+
chart.ChartTitleArea.Bold = true;
107+
chart.ChartTitleArea.Color = OfficeKnownColors.Red;
108+
chart.ChartTitleArea.Size = 20;
109+
break;
110+
}
111+
112+
case SlideItemType.SmartArt:
113+
{
114+
// Modify SmartArt content
115+
ISmartArt smartArt = shape as ISmartArt;
116+
ISmartArtNode smartArtNode = smartArt.Nodes[0];
117+
smartArtNode.TextBody.Text = "Requirement";
118+
break;
119+
}
120+
121+
case SlideItemType.OleObject:
122+
{
123+
// Modify OLE object size
124+
IOleObject oleObject = shape as IOleObject;
125+
oleObject.Width = 300;
126+
break;
127+
}
128+
}
129+
}
130+
131+
/// <summary>
132+
/// Modifies the text content within a text body.
133+
/// </summary>
134+
private static void ModifyTextPart(ITextBody textBody)
135+
{
136+
// Iterate through paragraphs in the text body
137+
foreach (IParagraph paragraph in textBody.Paragraphs)
138+
{
139+
// Iterate through text parts and modify the text
140+
foreach (ITextPart textPart in paragraph.TextParts)
141+
{
142+
textPart.Text = "Adventure Works";
143+
}
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)