Skip to content

Commit a80cf17

Browse files
Merge pull request #480 from Keerthana4775/main
ES-42173 - Add Sample for converting Word document to PowerPoint Presentation document
2 parents a6312f2 + f0a8d95 commit a80cf17

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.36105.23 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Word-document-to-PPTX", "Convert-Word-document-to-PPTX\Convert-Word-document-to-PPTX.csproj", "{54B715A3-744B-460B-B50E-2506C6ED6B6D}"
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+
{54B715A3-744B-460B-B50E-2506C6ED6B6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{54B715A3-744B-460B-B50E-2506C6ED6B6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{54B715A3-744B-460B-B50E-2506C6ED6B6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{54B715A3-744B-460B-B50E-2506C6ED6B6D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {8D3878F2-8F44-4CED-B36F-5DCD6D25668F}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Convert_Word_document_to_PPTX</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
14+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
15+
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<None Update="Data\Template.docx">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Output\.gitkeep">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
</Project>

Word-to-PPTX-Conversion/Convert-Word-document-to-PPTX/.NET/Convert-Word-document-to-PPTX/Output/.gitkeep

Whitespace-only changes.
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.Drawing;
3+
using Syncfusion.Presentation;
4+
5+
namespace Convert_Word_document_to_PPTX
6+
{
7+
class Program
8+
{
9+
private static IPresentation pptxDoc;
10+
static void Main(string[] args)
11+
{
12+
//Loads an existing Word document.
13+
using (WordDocument document = new WordDocument(Path.GetFullPath("../../../Data/Adventure.docx"), Syncfusion.DocIO.FormatType.Automatic))
14+
{
15+
// Create a new PowerPoint presentation.
16+
pptxDoc = Presentation.Create();
17+
// Iterate each section in the Word document and process its body.
18+
foreach (WSection section in document.Sections)
19+
{
20+
// Access the section body that contains paragraphs, tables, and content controls.
21+
WTextBody sectionBody = section.Body;
22+
AddTextBodyItems(sectionBody);
23+
}
24+
FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/DocxToPptx.pptx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
25+
pptxDoc.Save(outputStream);
26+
outputStream.Close();
27+
}
28+
}
29+
30+
/// <summary>
31+
/// Adds the text body items to the Presentation document
32+
/// </summary>
33+
/// <param name="docTextBody"></param>
34+
private static void AddTextBodyItems(WTextBody docTextBody)
35+
{
36+
AddTextBodyItems(docTextBody, null);
37+
}
38+
39+
/// <summary>
40+
/// Iterates the text body items of Word document and creates slides and add textbox accordingly
41+
/// </summary>
42+
/// <param name="docTextBody"></param>
43+
/// <param name="powerPointTableCell"></param>
44+
private static void AddTextBodyItems(WTextBody docTextBody, ICell powerPointTableCell)
45+
{
46+
ISlide powerPointSlide = null;
47+
IShape powerPointShape = null;
48+
IParagraph powerPointParagraph = null;
49+
50+
//Iterates through each of the child items of WTextBody
51+
for (int i = 0; i < docTextBody.ChildEntities.Count; i++)
52+
{
53+
//IEntity is the basic unit in DocIO DOM.
54+
//Accesses the body items (should be either paragraph, table or block content control) as IEntity
55+
IEntity bodyItemEntity = docTextBody.ChildEntities[i];
56+
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
57+
//Decides the element type by using EntityType
58+
switch (bodyItemEntity.EntityType)
59+
{
60+
case EntityType.Paragraph:
61+
WParagraph docParagraph = bodyItemEntity as WParagraph;
62+
if (docParagraph.ChildEntities.Count == 0)
63+
break;
64+
//Checkes whether the paragraph is list paragraph
65+
if (IsListParagraph(docParagraph))
66+
{
67+
if (docParagraph.ListFormat.ListType == Syncfusion.DocIO.DLS.ListType.NoList)
68+
{
69+
powerPointSlide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
70+
powerPointSlide.AddTextBox(50, 50, 756, 300);
71+
}
72+
powerPointShape = powerPointSlide.Shapes[0] as IShape;
73+
powerPointParagraph = powerPointShape.TextBody.AddParagraph(docParagraph.Text);
74+
//Checks whether the list type is numbered
75+
if (docParagraph.ListFormat.ListType == Syncfusion.DocIO.DLS.ListType.Numbered)
76+
ApplyListStyles(powerPointParagraph);
77+
}
78+
//Checks whether the paragraph is inside a cell
79+
else if (docParagraph.IsInCell && powerPointTableCell != null)
80+
{
81+
powerPointParagraph = powerPointTableCell.TextBody.AddParagraph();
82+
AddParagraphItems(docParagraph, powerPointParagraph, powerPointTableCell);
83+
}
84+
//Checks whether the paragraph is heading style
85+
else if (docParagraph.StyleName.Contains("Heading"))
86+
{
87+
powerPointSlide = pptxDoc.Slides.Add(SlideLayoutType.Title);
88+
powerPointSlide.Shapes.Remove(powerPointSlide.Shapes[1]);
89+
powerPointShape = powerPointSlide.Shapes[0] as IShape;
90+
powerPointParagraph = powerPointShape.TextBody.AddParagraph();
91+
92+
AddParagraphItems(docParagraph, powerPointParagraph);
93+
}
94+
else
95+
{
96+
powerPointSlide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
97+
powerPointShape = powerPointSlide.AddTextBox(50, 50, 800, 300);
98+
powerPointParagraph = powerPointShape.TextBody.AddParagraph();
99+
powerPointParagraph.FirstLineIndent = 30;
100+
101+
AddParagraphItems(docParagraph, powerPointParagraph);
102+
}
103+
break;
104+
case EntityType.Table:
105+
//Table is a collection of rows and cells
106+
//Iterates through table's DOM
107+
//Iterates the row collection in a table and creates a new slide for each row
108+
WTable table = bodyItemEntity as WTable;
109+
foreach (WTableRow row in table.Rows)
110+
{
111+
powerPointSlide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
112+
ITable powerPointTable = powerPointSlide.Shapes.AddTable(1, row.Cells.Count, 200, 140, 500, 220);
113+
powerPointTable.BuiltInStyle = BuiltInTableStyle.None;
114+
//Iterates the cell collection in a table row
115+
for (int j = 0; j < row.Cells.Count; j++)
116+
{
117+
WTableCell cell = row.Cells[j];
118+
//Table cell is derived from (also a) TextBody
119+
//Reusing the code meant for iterating TextBody
120+
AddTextBodyItems(cell as WTextBody, powerPointTable.Rows[0].Cells[j]);
121+
}
122+
}
123+
break;
124+
case EntityType.BlockContentControl:
125+
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
126+
//Iterates to the body items of Block Content Control.
127+
AddTextBodyItems(blockContentControl.TextBody);
128+
break;
129+
}
130+
}
131+
}
132+
133+
/// <summary>
134+
/// Applies the Numbered list style
135+
/// </summary>
136+
/// <param name="powerPointParagraph"></param>
137+
private static void ApplyListStyles(IParagraph powerPointParagraph)
138+
{
139+
powerPointParagraph.ListFormat.Type = Syncfusion.Presentation.ListType.Numbered;
140+
powerPointParagraph.ListFormat.NumberStyle = NumberedListStyle.ArabicPeriod;
141+
powerPointParagraph.ListFormat.StartValue = 1;
142+
// Sets the hanging value
143+
powerPointParagraph.FirstLineIndent = -20;
144+
// Sets the bullet character size. Here, 100 means 100% of its text. Possible values can range from 25 to 400.
145+
powerPointParagraph.ListFormat.Size = 100;
146+
powerPointParagraph.IndentLevelNumber = 1;
147+
}
148+
149+
/// <summary>
150+
/// Adds the paragraph items to the Presentation document
151+
/// </summary>
152+
/// <param name="docParagraph"></param>
153+
/// <param name="powerPointParagraph"></param>
154+
private static void AddParagraphItems(WParagraph docParagraph, IParagraph powerPointParagraph)
155+
{
156+
AddParagraphItems(docParagraph, powerPointParagraph, null);
157+
}
158+
159+
/// <summary>
160+
/// Iterates the paragraph and adds the paragraph items to the Presentation document
161+
/// </summary>
162+
/// <param name="docParagraph"></param>
163+
/// <param name="powerPointParagraph"></param>
164+
/// <param name="powerPointTableCell"></param>
165+
private static void AddParagraphItems(WParagraph docParagraph, IParagraph powerPointParagraph, ICell powerPointTableCell)
166+
{
167+
for (int i = 0; i < docParagraph.Items.Count; i++)
168+
{
169+
Entity entity = docParagraph.Items[i];
170+
//A paragraph can have child elements such as text, image, hyperlink, symbols, etc.,
171+
//Decides the element type by using EntityType
172+
switch (entity.EntityType)
173+
{
174+
case EntityType.TextRange:
175+
WTextRange textRange = entity as WTextRange;
176+
ITextPart textPart = powerPointParagraph.AddTextPart(textRange.Text);
177+
//Checks whether th paragraph is not in cell
178+
if (!docParagraph.IsInCell)
179+
{
180+
//Checks whether the paragraph is heading style paragraph
181+
if (docParagraph.StyleName.Contains("Heading"))
182+
textPart.Font.Bold = true;
183+
else
184+
{
185+
textPart.Font.Color.SystemColor = Color.Black;
186+
textPart.Font.FontSize = 32;
187+
}
188+
}
189+
break;
190+
case EntityType.Picture:
191+
//Checks whether the image is inside a cell
192+
if (docParagraph.IsInCell && powerPointTableCell != null)
193+
powerPointTableCell.Fill.PictureFill.ImageBytes = (entity as WPicture).ImageBytes;
194+
break;
195+
}
196+
}
197+
}
198+
199+
/// <summary>
200+
/// Checks whether the paragraph is list paragraph
201+
/// </summary>
202+
/// <param name="paragraph"></param>
203+
/// <returns></returns>
204+
private static bool IsListParagraph(WParagraph paragraph)
205+
{
206+
return paragraph.NextSibling is WParagraph && paragraph.PreviousSibling is WParagraph &&
207+
((paragraph.NextSibling as WParagraph).ListFormat.ListType != Syncfusion.DocIO.DLS.ListType.NoList
208+
|| (paragraph.PreviousSibling as WParagraph).ListFormat.ListType != Syncfusion.DocIO.DLS.ListType.NoList);
209+
}
210+
}
211+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Convert-Word-document-to-PPTX": {
4+
"commandName": "Project"
5+
},
6+
"Container (Dockerfile)": {
7+
"commandName": "Docker"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)