Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excel to PDF conversion using C# #77

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading