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+ }
0 commit comments