diff --git a/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF.sln b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF.sln new file mode 100644 index 00000000..50eab588 --- /dev/null +++ b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34414.90 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelToPDF", "ExcelToPDF\ExcelToPDF.csproj", "{9F221130-3DD2-4908-A5AC-90C4256FDAA0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9F221130-3DD2-4908-A5AC-90C4256FDAA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F221130-3DD2-4908-A5AC-90C4256FDAA0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F221130-3DD2-4908-A5AC-90C4256FDAA0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F221130-3DD2-4908-A5AC-90C4256FDAA0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7BCABE38-D969-49F0-B057-2F80DB8458E1} + EndGlobalSection +EndGlobal diff --git a/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Data/Invoice.xlsx b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Data/Invoice.xlsx new file mode 100644 index 00000000..803b694f Binary files /dev/null and b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Data/Invoice.xlsx differ diff --git a/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/ExcelToPDF.csproj b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/ExcelToPDF.csproj new file mode 100644 index 00000000..d63d007b --- /dev/null +++ b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/ExcelToPDF.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Output/Invoice.pdf b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Output/Invoice.pdf new file mode 100644 index 00000000..4e881474 Binary files /dev/null and b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Output/Invoice.pdf differ diff --git a/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Program.cs b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Program.cs new file mode 100644 index 00000000..0776d8eb --- /dev/null +++ b/Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Program.cs @@ -0,0 +1,61 @@ +using Syncfusion.XlsIO; +using Syncfusion.XlsIORenderer; +using Syncfusion.Pdf; + +namespace ExcelToPDF +{ + class Program + { + static void Main(string[] args) + { + string filePath = "../../../Data/Invoice.xlsx"; + Stream inputExcelData = File.OpenRead(filePath); + Stream outputPDFData = ConvertExcelToPDF(inputExcelData); + File.WriteAllBytes("../../../Output/Invoice.pdf", ((MemoryStream)outputPDFData).ToArray()); + } + + static Stream ConvertExcelToPDF(Stream inputExcelData) + { + MemoryStream pdfStream = new MemoryStream(); + + //Instantiate the spreadsheet creation engine. + using (ExcelEngine excelEngine = new ExcelEngine()) + { + //Instantiate the Excel application object. + IApplication application = excelEngine.Excel; + + //Set the default application version. + application.DefaultVersion = ExcelVersion.Xlsx; + + //Load the existing Excel file into IWorkbook. + IWorkbook workbook = application.Workbooks.Open(inputExcelData); + + //Settings for Excel to PDF conversion + XlsIORendererSettings settings = new XlsIORendererSettings(); + + //Set the layout option to fit all columns on one page. + settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; + + //Initialize the XlsIORenderer. + XlsIORenderer renderer = new XlsIORenderer(); + + //Initialize the PDF document. + PdfDocument pdfDocument = new PdfDocument(); + + //Convert the Excel document to PDF. + pdfDocument = renderer.ConvertToPDF(workbook, settings); + + //Save the PDF file. + pdfDocument.Save(pdfStream); + + //Close the PDF document. + pdfDocument.Close(); + + //Close the workbook. + workbook.Close(); + } + + return pdfStream; + } + } +} \ No newline at end of file