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