PDF (Portable Document Format) is a file format used to display the document with same formatting, independent of application software, hardware, and operating system. Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a PDF document in Xamarin.
Steps to create PDF programmatically:
- Create a new C# Xamarin.Forms application project.
- 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.
- Install the Syncfusion.Xamarin.PDF NuGet package as a reference to your .NET Framework applications from NuGet.org.
- 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 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 following code snippet, which invokes the MainXamlPage.
- In the MainXamlPage.xaml, add new button as follows.
- Include the following namespace in the MainXamlPage.xaml.cs file.
- Include the following code snippet in the click event of the button in MainXamlPage.xaml.cs, to create a PDF file and save it in a stream.
- Download the helper files from this link 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.
Represent the base interface for save operation
Save implementation for iOS device
Helper class for viewing the PDF file in iOS device
Save implementation for Android device
Save implementation for Windows phone device
Save implementation for UWP device.
Save implementation for WinRT device.
- Compile and execute the application. This creates a simple PDF document.
By executing the program, you will get the PDF document as follows.
Download the complete work sample from Create-PDF-file.zips.
Take a moment to peruse the documentation, where you can find other options like drawing right-to-left text and multi-column text, consuming TrueType fonts, Standards fonts, and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Create a PDF file in ASP.NET Core
Create a PDF file in ASP.NET MVC
Create a PDF file in Windows Forms
Create a PDF file in WPF
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.