Convert Excel Document to PDF in Xamarin
29 Nov 20245 minutes to read
Syncfusion® XlsIO is a Xamarin Excel library used to create, read, edit and convert Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can convert an Excel document to PDF in Xamarin.
Steps to convert Excel document to PDF in Xamarin
Step 1: Create a new Xamarin.Forms application project.
Step 2: Name the project and click Create button.
Step 3: Select a project template and required platforms to deploy the application. In this application the portable assemblies to be shared across multiple platforms, the .NET Standard code sharing strategy has been selected. For more details about code sharing refer here.
NOTE
If .NET Standard is not available in the code sharing strategy, the Portable Class Library (PCL) can be selected.
Step 4: Install the Syncfusion.Xamarin.XlsIORenderer NuGet package as a reference to your project from NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Step 5: Add new Forms XAML page in portable project. If there is no XAML page is defined in the App class. Otherwise proceed to the next step.
- To add the new XAML page, right click on the project and select Add > New Item and add a Forms XAML Page from the list. Name it as MainXamlPage.
- In App class of portable project (App.cs), replace the existing constructor of App class with the code snippet given below which invokes the MainXamlPage.
public App()
{
// The root page of your application
MainPage = new MainXamlPage();
}
Step 6: In the MainXamlPage.xaml add new button as shown below.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Convert_Excel_to_PDF.MainPage">
<StackLayout VerticalOptions="Center">
<Button Text="Convert Excel to PDF" Clicked="OnButtonClicked" HorizontalOptions="Center"/>
</StackLayout>
</ContentPage>
Step 7: Include the following namespace in the MainXamlPage.xaml.cs file.
using Syncfusion.XlsIO;
using Syncfusion.Pdf;
using Syncfusion.XlsIORenderer;
Step 8: Include the below code snippet in the click event of the button in MainXamlPage.xaml.cs to convert an Excel document to PDF and save it in a stream.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
Assembly executingAssembly = typeof(App).GetTypeInfo().Assembly;
using(Stream inputStream = executingAssembly.GetManifestResourceStream("Convert-Excel-to-PDF.InputTemplate.xlsx"))
{
// Open the workbook.
IWorkbook workbook = application.Workbooks.Open(inputStream);
// Instantiate the Excel to PDF renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Create the MemoryStream to save the converted PDF.
MemoryStream pdfStream = new MemoryStream();
//Save the converted PDF document to MemoryStream.
pdfDocument.Save(pdfStream);
pdfStream.Position = 0;
//Save the stream as a file in the device and invoke it for viewing.
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Sample.pdf", "application/pdf", pdfStream);
}
}
Helper files for Xamarin
Refer the below helper files and add them into the mentioned project. These helper files allow you to save the stream as a physical file and open the file for viewing.
Project | File Name | Summary |
Represent the base interface for save operation | ||
Save implementation for iOS device | ||
Helper class for viewing the Excel document in iOS device | ||
Save implementation for Android device | ||
Save implementation for UWP device. |
Compile and execute the application. Now this application convert an Excel document to PDF.
A complete working example of how to convert an Excel document to PDF in Xamarin is present on this GitHub page.
By executing the program, you will get the PDF document as follows.
Click here to explore the rich set of Syncfusion® Excel library (XlsIO) features.
An online sample link to convert an Excel document to PDF in ASP.NET Core.