Skip to content

Commit d7378e4

Browse files
authored
Merge pull request #1717 from syncfusion-content/990137-ExcelToPdfUG
990137-Modify file streams of C# [Cross-platform] in XlsIO UG
2 parents 44c41e9 + 1bfa0c7 commit d7378e4

28 files changed

+157
-463
lines changed

Document-Processing/Excel/Conversions/Excel-to-PDF/NET/Excel-to-PDF-Conversion.md

Lines changed: 29 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2525
{
2626
IApplication application = excelEngine.Excel;
2727
application.DefaultVersion = ExcelVersion.Xlsx;
28-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
29-
IWorkbook workbook = application.Workbooks.Open(inputStream);
28+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
3029

3130
//Initialize XlsIO renderer.
3231
XlsIORenderer renderer = new XlsIORenderer();
@@ -36,13 +35,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
3635

3736
#region Save
3837
//Saving the workbook
39-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/WorkbookToPDF.pdf"), FileMode.Create, FileAccess.Write);
40-
pdfDocument.Save(outputStream);
38+
pdfDocument.Save(Path.GetFullPath("Output/WorkbookToPDF.pdf"));
4139
#endregion
42-
43-
//Dispose streams
44-
outputStream.Dispose();
45-
inputStream.Dispose();
4640
}
4741
{% endhighlight %}
4842

@@ -102,8 +96,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
10296
{
10397
IApplication application = excelEngine.Excel;
10498
application.DefaultVersion = ExcelVersion.Xlsx;
105-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
106-
IWorkbook workbook = application.Workbooks.Open(inputStream);
99+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
107100
IWorksheet worksheet = workbook.Worksheets[0];
108101

109102
//Initialize XlsIO renderer.
@@ -114,13 +107,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
114107

115108
#region Save
116109
//Saving the workbook
117-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/WorksheetToPDF.pdf"), FileMode.Create, FileAccess.Write);
118-
pdfDocument.Save(outputStream);
110+
pdfDocument.Save(Path.GetFullPath("Output/WorksheetToPDF.pdf"));
119111
#endregion
120-
121-
//Dispose streams
122-
outputStream.Dispose();
123-
inputStream.Dispose();
124112
}
125113
{% endhighlight %}
126114

@@ -174,8 +162,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
174162
{
175163
IApplication application = excelEngine.Excel;
176164
application.DefaultVersion = ExcelVersion.Xlsx;
177-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
178-
IWorkbook workbook = application.Workbooks.Open(inputStream);
165+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
179166

180167
//Initialize XlsIO renderer.
181168
XlsIORenderer renderer = new XlsIORenderer();
@@ -187,20 +174,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
187174

188175
#region Save
189176
//Saving the workbook
190-
FileStream outputStream = new FileStream(sheet.Name +".pdf", FileMode.Create, FileAccess.Write);
191-
pdfDocument.Save(outputStream);
177+
pdfDocument.Save(sheet.Name + ".pdf");
192178
#endregion
193-
194-
//Dispose streams
195-
outputStream.Dispose();
196-
inputStream.Dispose();
197-
198-
System.Diagnostics.Process process = new System.Diagnostics.Process();
199-
process.StartInfo = new System.Diagnostics.ProcessStartInfo(sheet.Name + ".pdf")
200-
{
201-
UseShellExecute = true
202-
};
203-
process.Start();
204179
}
205180
}
206181
{% endhighlight %}
@@ -262,8 +237,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
262237
{
263238
IApplication application = excelEngine.Excel;
264239
application.DefaultVersion = ExcelVersion.Xlsx;
265-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
266-
IWorkbook workbook = application.Workbooks.Open(inputStream);
240+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
267241
IWorksheet worksheet = workbook.Worksheets[0];
268242

269243
IChart chart = worksheet.Charts[0];
@@ -276,13 +250,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
276250

277251
#region Save
278252
//Saving the workbook
279-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ChartToPDF.pdf"), FileMode.Create, FileAccess.Write);
280-
pdfDocument.Save(outputStream);
253+
pdfDocument.Save(Path.GetFullPath("Output/ChartToPDF.pdf"));
281254
#endregion
282-
283-
//Dispose streams
284-
outputStream.Dispose();
285-
inputStream.Dispose();
286255
}
287256
{% endhighlight %}
288257

@@ -347,8 +316,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
347316
{
348317
IApplication application = excelEngine.Excel;
349318
application.DefaultVersion = ExcelVersion.Xlsx;
350-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
351-
IWorkbook workbook = application.Workbooks.Open(inputStream);
319+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
352320
IWorksheet worksheet = workbook.Worksheets[0];
353321

354322
//Set print location to comments
@@ -362,13 +330,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
362330

363331
#region Save
364332
//Saving the workbook
365-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CommentsInPlaceToPDF.pdf"), FileMode.Create, FileAccess.Write);
366-
pdfDocument.Save(outputStream);
333+
pdfDocument.Save(Path.GetFullPath("Output/CommentsInPlaceToPDF.pdf"));
367334
#endregion
368-
369-
//Dispose streams
370-
outputStream.Dispose();
371-
inputStream.Dispose();
372335
}
373336
{% endhighlight %}
374337

@@ -440,8 +403,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
440403
{
441404
IApplication application = excelEngine.Excel;
442405
application.DefaultVersion = ExcelVersion.Xlsx;
443-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
444-
IWorkbook workbook = application.Workbooks.Open(inputStream);
406+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
445407
IWorksheet worksheet = workbook.Worksheets[0];
446408

447409
//Set print location to comments
@@ -455,13 +417,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
455417

456418
#region Save
457419
//Saving the workbook
458-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CommentsToPDFAtEnd.pdf"), FileMode.Create, FileAccess.Write);
459-
pdfDocument.Save(outputStream);
420+
pdfDocument.Save(Path.GetFullPath("Output/CommentsToPDFAtEnd.pdf"));
460421
#endregion
461-
462-
//Dispose streams
463-
outputStream.Dispose();
464-
inputStream.Dispose();
465422
}
466423
{% endhighlight %}
467424

@@ -534,8 +491,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
534491
{
535492
IApplication application = excelEngine.Excel;
536493
application.DefaultVersion = ExcelVersion.Xlsx;
537-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
538-
IWorkbook workbook = application.Workbooks.Open(inputStream);
494+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
539495
IWorksheet worksheet = workbook.Worksheets[0];
540496

541497
//Set print location to comments
@@ -549,13 +505,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
549505

550506
#region Save
551507
//Saving the workbook
552-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/NoCommentsInPDF.pdf"), FileMode.Create, FileAccess.Write);
553-
pdfDocument.Save(outputStream);
508+
pdfDocument.Save(Path.GetFullPath("Output/NoCommentsInPDF.pdf"));
554509
#endregion
555-
556-
//Dispose streams
557-
outputStream.Dispose();
558-
inputStream.Dispose();
559510
}
560511
{% endhighlight %}
561512

@@ -632,8 +583,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
632583
{
633584
IApplication application = excelEngine.Excel;
634585
application.DefaultVersion = ExcelVersion.Xlsx;
635-
FileStream inputStream = new FileStream("CommentsTemplate.xlsx", FileMode.Open, FileAccess.Read);
636-
IWorkbook workbook = application.Workbooks.Open(inputStream);
586+
IWorkbook workbook = application.Workbooks.Open("CommentsTemplate.xlsx");
637587
IWorksheet worksheet = workbook.Worksheets[0];
638588

639589
//Add threaded comment
@@ -651,9 +601,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
651601

652602
//Convert Excel document into PDF document
653603
PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);
654-
Stream stream = new FileStream("ExcelComments.pdf", FileMode.Create, FileAccess.ReadWrite);
655-
pdfDocument.Save(stream);
656-
stream.Dispose();
604+
pdfDocument.Save("ExcelComments.pdf");
657605
}
658606
{% endhighlight %}
659607

@@ -761,17 +709,12 @@ namespace FontSubstitution
761709
//Initialize XlsIO renderer.
762710
XlsIORenderer renderer = new XlsIORenderer();
763711

764-
FileStream excelStream = new FileStream("Template.xlsx", FileMode.Open, FileAccess.Read);
765-
IWorkbook workbook = application.Workbooks.Open(excelStream);
712+
IWorkbook workbook = application.Workbooks.Open("Template.xlsx");
766713

767714
//Convert Excel document with charts into PDF document
768715
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
769716

770-
Stream stream = new FileStream("ExcelToPDF.pdf", FileMode.Create, FileAccess.ReadWrite);
771-
pdfDocument.Save(stream);
772-
773-
excelStream.Dispose();
774-
stream.Dispose();
717+
pdfDocument.Save("ExcelToPDF.pdf");
775718
}
776719
}
777720

@@ -1207,8 +1150,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
12071150
{
12081151
IApplication application = excelEngine.Excel;
12091152
application.DefaultVersion = ExcelVersion.Xlsx;
1210-
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
1211-
IWorkbook workbook = application.Workbooks.Open(fileStream);
1153+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
12121154

12131155
//Initialize XlsIORenderer
12141156
application.XlsIORenderer = new XlsIORenderer();
@@ -1222,13 +1164,11 @@ using (ExcelEngine excelEngine = new ExcelEngine())
12221164
//Convert Excel document into PDF document
12231165
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
12241166

1225-
//Save the converted PDF document to stream.
1226-
FileStream stream = new FileStream("Sample.pdf", FileMode.Create, FileAccess.ReadWrite);
1227-
pdfDocument.Save(stream);
1167+
//Save the converted PDF document
1168+
pdfDocument.Save("Sample.pdf");
12281169

12291170
//Close and Dispose
12301171
workbook.Close();
1231-
stream.Dispose();
12321172
}
12331173
{% endhighlight %}
12341174

@@ -1288,8 +1228,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
12881228
{
12891229
IApplication application = excelEngine.Excel;
12901230
application.DefaultVersion = ExcelVersion.Xlsx;
1291-
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
1292-
IWorkbook workbook = application.Workbooks.Open(fileStream);
1231+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
12931232

12941233
//Initialize XlsIORenderer
12951234
application.XlsIORenderer = new XlsIORenderer();
@@ -1312,13 +1251,11 @@ using (ExcelEngine excelEngine = new ExcelEngine())
13121251
//Convert Excel document into PDF document
13131252
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
13141253

1315-
//Save the converted PDF document to stream.
1316-
FileStream stream = new FileStream("Sample.pdf", FileMode.Create, FileAccess.ReadWrite);
1317-
pdfDocument.Save(stream);
1254+
//Save the converted PDF document
1255+
pdfDocument.Save("Sample.pdf");
13181256

13191257
//Close and Dispose
13201258
workbook.Close();
1321-
stream.Dispose();
13221259
}
13231260
{% endhighlight %}
13241261

@@ -1398,8 +1335,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
13981335
{
13991336
IApplication application = excelEngine.Excel;
14001337
application.DefaultVersion = ExcelVersion.Xlsx;
1401-
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
1402-
IWorkbook workbook = application.Workbooks.Open(fileStream);
1338+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
14031339

14041340
//Initialize XlsIORenderer
14051341
application.XlsIORenderer = new XlsIORenderer();
@@ -1423,12 +1359,10 @@ using (ExcelEngine excelEngine = new ExcelEngine())
14231359
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
14241360

14251361
//Excel to PDF
1426-
Stream stream = new FileStream("Sample.pdf", FileMode.Create, FileAccess.ReadWrite);
1427-
pdfDocument.Save(stream);
1362+
pdfDocument.Save("Sample.pdf");
14281363

14291364
//Close and Dispose
14301365
workbook.Close();
1431-
stream.Dispose();
14321366
}
14331367
{% endhighlight %}
14341368

@@ -1506,8 +1440,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
15061440
{
15071441
IApplication application = excelEngine.Excel;
15081442
application.DefaultVersion = ExcelVersion.Xlsx;
1509-
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
1510-
IWorkbook workbook = application.Workbooks.Open(fileStream);
1443+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
15111444

15121445
//Initialize XlsIORenderer
15131446
application.XlsIORenderer = new XlsIORenderer();
@@ -1529,13 +1462,11 @@ using (ExcelEngine excelEngine = new ExcelEngine())
15291462
//Convert Excel document into PDF document
15301463
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
15311464

1532-
//Save the PDF document to stream
1533-
FileStream stream = new FileStream("Sample.pdf", FileMode.Create, FileAccess.ReadWrite);
1534-
pdfDocument.Save(stream);
1465+
//Save the PDF document
1466+
pdfDocument.Save("Sample.pdf");
15351467

15361468
//Close and Dispose
15371469
workbook.Close();
1538-
stream.Dispose();
15391470
}
15401471
{% endhighlight %}
15411472

0 commit comments

Comments
 (0)