55using Syncfusion . DocIO . DLS ;
66using Syncfusion . DocIO ;
77using File = Google . Apis . Drive . v3 . Data . File ;
8- using Google . Apis . Auth . OAuth2 . Flows ;
98
109namespace Save_Word_document
1110{
1211 internal class Program
1312 {
1413 static void Main ( string [ ] args )
1514 {
16- //Creating a new document
15+ //Creating a new document.
1716 WordDocument document = new WordDocument ( ) ;
18- //Adding a new section to the document
17+ //Adding a new section to the document.
1918 WSection section = document . AddSection ( ) as WSection ;
20- //Set Margin of the section
19+ //Set Margin of the section.
2120 section . PageSetup . Margins . All = 72 ;
22- //Set page size of the section
21+ //Set page size of the section.
2322 section . PageSetup . PageSize = new Syncfusion . Drawing . SizeF ( 612 , 792 ) ;
2423
25- //Create Paragraph styles
24+ //Create Paragraph styles.
2625 WParagraphStyle style = document . AddParagraphStyle ( "Normal" ) as WParagraphStyle ;
2726 style . CharacterFormat . FontName = "Calibri" ;
2827 style . CharacterFormat . FontSize = 11f ;
@@ -43,7 +42,7 @@ static void Main(string[] args)
4342
4443 IWParagraph paragraph = section . HeadersFooters . Header . AddParagraph ( ) ;
4544
46- //Gets the image stream
45+ //Gets the image stream.
4746 FileStream imageStream = new FileStream ( Path . GetFullPath ( "../../../Data/AdventureCycle.jpg" ) , FileMode . Open , FileAccess . Read ) ;
4847 IWPicture picture = paragraph . AppendPicture ( imageStream ) ;
4948 picture . TextWrappingStyle = TextWrappingStyle . InFrontOfText ;
@@ -61,22 +60,22 @@ static void Main(string[] args)
6160 textRange . CharacterFormat . FontName = "Calibri" ;
6261 textRange . CharacterFormat . TextColor = Syncfusion . Drawing . Color . Red ;
6362
64- //Appends paragraph
63+ //Appends paragraph.
6564 paragraph = section . AddParagraph ( ) ;
6665 paragraph . ApplyStyle ( "Heading 1" ) ;
6766 paragraph . ParagraphFormat . HorizontalAlignment = HorizontalAlignment . Center ;
6867 textRange = paragraph . AppendText ( "Adventure Works Cycles" ) as WTextRange ;
6968 textRange . CharacterFormat . FontSize = 18f ;
7069 textRange . CharacterFormat . FontName = "Calibri" ;
7170
72- //Appends paragraph
71+ //Appends paragraph.
7372 paragraph = section . AddParagraph ( ) ;
7473 paragraph . ParagraphFormat . FirstLineIndent = 36 ;
7574 paragraph . BreakCharacterFormat . FontSize = 12f ;
7675 textRange = paragraph . AppendText ( "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base." ) as WTextRange ;
7776 textRange . CharacterFormat . FontSize = 12f ;
7877
79- //Appends paragraph
78+ //Appends paragraph.
8079 paragraph = section . AddParagraph ( ) ;
8180 paragraph . ParagraphFormat . FirstLineIndent = 36 ;
8281 paragraph . BreakCharacterFormat . FontSize = 12f ;
@@ -90,18 +89,18 @@ static void Main(string[] args)
9089 textRange . CharacterFormat . FontSize = 16f ;
9190 textRange . CharacterFormat . FontName = "Calibri" ;
9291
93- //Appends table
92+ //Appends table.
9493 IWTable table = section . AddTable ( ) ;
9594 table . ResetCells ( 3 , 2 ) ;
9695 table . TableFormat . Borders . BorderType = BorderStyle . None ;
9796 table . TableFormat . IsAutoResized = true ;
9897
99- //Appends paragraph
98+ //Appends paragraph.
10099 paragraph = table [ 0 , 0 ] . AddParagraph ( ) ;
101100 paragraph . ParagraphFormat . AfterSpacing = 0 ;
102101 paragraph . BreakCharacterFormat . FontSize = 12f ;
103102
104- //Appends picture to the paragraph
103+ //Appends picture to the paragraph.
105104 FileStream image1 = new FileStream ( Path . GetFullPath ( "../../../Data/Mountain-200.jpg" ) , FileMode . Open , FileAccess . Read ) ;
106105 picture = paragraph . AppendPicture ( image1 ) ;
107106 picture . TextWrappingStyle = TextWrappingStyle . TopAndBottom ;
@@ -112,14 +111,14 @@ static void Main(string[] args)
112111 picture . WidthScale = 79 ;
113112 picture . HeightScale = 79 ;
114113
115- //Appends paragraph
114+ //Appends paragraph.
116115 paragraph = table [ 0 , 1 ] . AddParagraph ( ) ;
117116 paragraph . ApplyStyle ( "Heading 1" ) ;
118117 paragraph . ParagraphFormat . AfterSpacing = 0 ;
119118 paragraph . ParagraphFormat . LineSpacing = 12f ;
120119 paragraph . AppendText ( "Mountain-200" ) ;
121120
122- //Appends paragraph
121+ //Appends paragraph.
123122 paragraph = table [ 0 , 1 ] . AddParagraph ( ) ;
124123 paragraph . ParagraphFormat . AfterSpacing = 0 ;
125124 paragraph . ParagraphFormat . LineSpacing = 12f ;
@@ -138,20 +137,20 @@ static void Main(string[] args)
138137 textRange . CharacterFormat . FontSize = 12f ;
139138 textRange . CharacterFormat . FontName = "Times New Roman" ;
140139
141- //Appends paragraph
140+ //Appends paragraph.
142141 paragraph = table [ 0 , 1 ] . AddParagraph ( ) ;
143142 paragraph . ParagraphFormat . AfterSpacing = 0 ;
144143 paragraph . ParagraphFormat . LineSpacing = 12f ;
145144 paragraph . BreakCharacterFormat . FontSize = 12f ;
146145
147- //Appends paragraph
146+ //Appends paragraph.
148147 paragraph = table [ 1 , 0 ] . AddParagraph ( ) ;
149148 paragraph . ApplyStyle ( "Heading 1" ) ;
150149 paragraph . ParagraphFormat . AfterSpacing = 0 ;
151150 paragraph . ParagraphFormat . LineSpacing = 12f ;
152151 paragraph . AppendText ( "Mountain-300 " ) ;
153152
154- //Appends paragraph
153+ //Appends paragraph.
155154 paragraph = table [ 1 , 0 ] . AddParagraph ( ) ;
156155 paragraph . ParagraphFormat . AfterSpacing = 0 ;
157156 paragraph . ParagraphFormat . LineSpacing = 12f ;
@@ -170,18 +169,18 @@ static void Main(string[] args)
170169 textRange . CharacterFormat . FontSize = 12f ;
171170 textRange . CharacterFormat . FontName = "Times New Roman" ;
172171
173- //Appends paragraph
172+ //Appends paragraph.
174173 paragraph = table [ 1 , 0 ] . AddParagraph ( ) ;
175174 paragraph . ParagraphFormat . AfterSpacing = 0 ;
176175 paragraph . ParagraphFormat . LineSpacing = 12f ;
177176 paragraph . BreakCharacterFormat . FontSize = 12f ;
178177
179- //Appends paragraph
178+ //Appends paragraph.
180179 paragraph = table [ 1 , 1 ] . AddParagraph ( ) ;
181180 paragraph . ApplyStyle ( "Heading 1" ) ;
182181 paragraph . ParagraphFormat . LineSpacing = 12f ;
183182
184- //Appends picture to the paragraph
183+ //Appends picture to the paragraph.
185184 FileStream image2 = new FileStream ( Path . GetFullPath ( "../../../Data/Mountain-300.jpg" ) , FileMode . Open , FileAccess . Read ) ;
186185 picture = paragraph . AppendPicture ( image2 ) ;
187186 picture . TextWrappingStyle = TextWrappingStyle . TopAndBottom ;
@@ -192,12 +191,12 @@ static void Main(string[] args)
192191 picture . WidthScale = 75 ;
193192 picture . HeightScale = 75 ;
194193
195- //Appends paragraph
194+ //Appends paragraph.
196195 paragraph = table [ 2 , 0 ] . AddParagraph ( ) ;
197196 paragraph . ApplyStyle ( "Heading 1" ) ;
198197 paragraph . ParagraphFormat . LineSpacing = 12f ;
199198
200- //Appends picture to the paragraph
199+ //Appends picture to the paragraph.
201200 FileStream image3 = new FileStream ( Path . GetFullPath ( "../../../Data/Road-550-W.jpg" ) , FileMode . Open , FileAccess . Read ) ;
202201 picture = paragraph . AppendPicture ( image3 ) ;
203202 picture . TextWrappingStyle = TextWrappingStyle . TopAndBottom ;
@@ -208,14 +207,14 @@ static void Main(string[] args)
208207 picture . WidthScale = 92 ;
209208 picture . HeightScale = 92 ;
210209
211- //Appends paragraph
210+ //Appends paragraph.
212211 paragraph = table [ 2 , 1 ] . AddParagraph ( ) ;
213212 paragraph . ApplyStyle ( "Heading 1" ) ;
214213 paragraph . ParagraphFormat . AfterSpacing = 0 ;
215214 paragraph . ParagraphFormat . LineSpacing = 12f ;
216215 paragraph . AppendText ( "Road-150 " ) ;
217216
218- //Appends paragraph
217+ //Appends paragraph.
219218 paragraph = table [ 2 , 1 ] . AddParagraph ( ) ;
220219 paragraph . ParagraphFormat . AfterSpacing = 0 ;
221220 paragraph . ParagraphFormat . LineSpacing = 12f ;
@@ -234,14 +233,14 @@ static void Main(string[] args)
234233 textRange . CharacterFormat . FontSize = 12f ;
235234 textRange . CharacterFormat . FontName = "Times New Roman" ;
236235
237- //Appends paragraph
236+ //Appends paragraph.
238237 section . AddParagraph ( ) ;
239238
240- //Saves the Word document to MemoryStream
239+ //Saves the Word document to MemoryStream.
241240 MemoryStream stream = new MemoryStream ( ) ;
242241 document . Save ( stream , FormatType . Docx ) ;
243242
244- // Load Google Drive API credentials from a file
243+ // Load Google Drive API credentials from a file.
245244 UserCredential credential ;
246245 string [ ] Scopes = { DriveService . Scope . Drive } ;
247246 string ApplicationName = "YourAppName" ;
@@ -257,28 +256,31 @@ static void Main(string[] args)
257256 new FileDataStore ( credPath , true ) ) . Result ;
258257 }
259258
260- // Create a new instance of Google Drive service
259+ // Create a new instance of Google Drive service.
261260 var service = new DriveService ( new BaseClientService . Initializer ( )
262261 {
263262 HttpClientInitializer = credential ,
264263 ApplicationName = ApplicationName ,
265264 } ) ;
266265
267- // Create metadata for the file to be uploaded
266+ // Create metadata for the file to be uploaded.
268267 var fileMetadata = new File ( )
269268 {
270- Name = "Output.docx" , // Name of the file in Google Drive
269+ Name = "Output.docx" , // Name of the file in Google Drive.
271270 MimeType = "application/msword" ,
272271 } ;
273272 FilesResource . CreateMediaUpload request ;
274- // Create a memory stream from the Word document
273+ // Create a memory stream from the Word document.
275274 using ( var fs = new MemoryStream ( stream . ToArray ( ) ) )
276275 {
277- // Create an upload request for Google Drive
276+ // Create an upload request for Google Drive.
278277 request = service . Files . Create ( fileMetadata , fs , "application/msword" ) ;
279- // Upload the file
278+ // Upload the file.
280279 request . Upload ( ) ;
281280 }
281+ //Dispose the stream.
282+ stream . Dispose ( ) ;
283+ document . Close ( ) ;
282284 }
283285 }
284286}
0 commit comments