Create PDF files in Xamarin

GemBox.Pdf is a standalone .NET component that offers cross-platform support, including non-Windows platforms like Xamarin and Mono. This means that you can use it on Android and iOS mobile devices.

By using GemBox.Pdf, you will be able to process your documents (read, write, and convert PDF files) from native mobile apps.

See the following example to learn how to create a PDF file in Xamarin.Forms mobile application, using GemBox.Pdf.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="MainPage">

    <StackLayout Padding="50" 
                 Spacing="20">

        <Label Text="GemBox.Pdf Example"
               HorizontalOptions="Center"
               FontSize="Large"
               Margin="0,0,0,30" />

        <Label HorizontalOptions="Center" 
               Text="Document page count" />

        <Label HorizontalOptions="Center"
               FontSize="Large"
               FontAttributes="Bold"
               BindingContext="{x:Reference pages}" 
               Text="{Binding Value}" />

        <Stepper x:Name="pages" 
                 HorizontalOptions="Center"
                 Minimum="1" 
                 Maximum="10"
                 Value="2" />

        <ActivityIndicator x:Name="activity" />

        <Button x:Name="button"
                Text="Create document"
                Clicked="Button_Clicked"/>

    </StackLayout>
</ContentPage>
using GemBox.Pdf;
using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        InitializeComponent();
    }

    private string CreateDocument()
    {
        var document = new PdfDocument();

        for (int i = 0; i < pages.Value; ++i)
            document.Pages.Add();

        var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Example.pdf");

        document.Save(filePath);

        return filePath;
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        button.IsEnabled = false;
        activity.IsRunning = true;

        try
        {
            var filePath = await Task.Run(() => CreateDocument());
            await Launcher.OpenAsync(new OpenFileRequest(Path.GetFileName(filePath), new ReadOnlyFile(filePath)));
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", ex.Message, "Close");
        }

        activity.IsRunning = false;
        button.IsEnabled = true;
    }
}
Imports GemBox.Pdf
Imports System
Imports System.ComponentModel
Imports System.IO
Imports System.Threading.Tasks
Imports Xamarin.Essentials
Imports Xamarin.Forms

Partial Public Class MainPage
    Inherits ContentPage

    Public Sub New()
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
        InitializeComponent()
    End Sub

    Private Function CreateDocument() As String
        Dim document As New PdfDocument()

        For i As Integer = 0 To pages.Value - 1
            document.Pages.Add()
        Next

        Dim filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Example.pdf")

        document.Save(filePath)

        Return filePath
    End Function

    Private Async Sub Button_Clicked(sender As Object, e As EventArgs)
        button.IsEnabled = False
        activity.IsRunning = True

        Try
            Dim filePath = Await Task.Run(Function() CreateDocument())
            Await Launcher.OpenAsync(New OpenFileRequest(Path.GetFileName(filePath), New ReadOnlyFile(filePath)))
        Catch ex As Exception
            Await DisplayAlert("Error", ex.Message, "Close")
        End Try

        activity.IsRunning = False
        button.IsEnabled = True
    End Sub
End Class
Pdf file generator on a native Android mobile app with Xamarin.Forms
Screenshot of Xamarin Android app that creates a PDF file

Limitations on Android or iOS

You can use the full functionality of GemBox.Pdf in Xamarin applications, but with the following exceptions:

These features currently have WPF dependencies which means they require a .NET Windows Desktop Runtime. However, we do have plans for providing cross-platform support for them in future releases.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy