Skip to content

Commit

Permalink
Excel to PDF conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohan2401 committed Feb 29, 2024
1 parent 0573238 commit 33f887b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Real Time Examples in Excel/ExcelToPDF/ExcelToPDF.sln
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIORenderer.Net.Core" Version="24.2.8" />
</ItemGroup>

<ItemGroup>
<Folder Include="Output\" />
</ItemGroup>

</Project>
Binary file not shown.
61 changes: 61 additions & 0 deletions Real Time Examples in Excel/ExcelToPDF/ExcelToPDF/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 33f887b

Please sign in to comment.