Xamarin.Forms is a great way to implement cross-platform,
fully native mobile apps, but when developing these apps, there’s still a gap in
the components needed to fill out the functionality.
These gaps typically fall into two areas: platform
services (camera/GPS/maps/settings/etc.) and UI components.
The Xamarin Plugins are
a great resource for the former. There are a few resources for the latter, but
I have found the Syncfusion Xamarin Control Suite to
be the most helpful.
One of the more common needs, particularly of line-of-business
apps, is PDF integration (generating/reading/merging/etc.).
The Syncfusion Xamarin
samples seem to be
a little sparse, so I thought I’d share an example implementation here.
For more in-depth Syncfusion PDF documentation,
have a look here. They
provide quite a bit more PDF capability than what is covered here.
This post will cover a basic scenario of
generating a PDF document from invoice information using the Syncfusion
PDF component for
Source code for this post can be found here. If
you’re looking for the details, check out the GitHub repo–we’ll cover the basic
outlines of what’s necessary here.
1. Create context for
the PDF generation
In the case of the sample code, I’ve created a GenerateInvoiceContext class–which
mostly houses the Invoice from
which to generate the PDF.
All the information you require to generate the PDF
needs to be contained in this class.
2. Wrap the generation
logic into a command
Create an implementation class that contains the logic for
generating the PDF from the invoice. In the sample code, this is the GenerateInvoiceCommand class.
I’ve used the command pattern here because it’s a great way to separate out and
force the single responsibility design pattern. The basics for command
implementation are provided by the Command Pattern Wireframe nuget
package, including an ExcuteAsync<TIn,
TOut> command pattern to follow. So calling the command from the view
model looks something like this:
Note that you don’t need to wrap the logic
in a command–this is just my personal preference for keeping the code clean.
3. Creating a PDF
When generating PDF elements, you need to first create the
document and a page for the document before you can start adding elements. I’ve
created a PDFGenerator class
to make these tasks a little easier. In the Setup method, you’ll notice the creation of the document and the
first page, as well as common properties for the document to be generated:
4. Add elements to the PDF
Generally, you need to add elements to the PDF document using
a top-down approach, keeping track of the Y-position in order to make sure that
each element is placed correctly in reference to the previously added element.
I found it useful to prefer the methods on the Syncfusion
PDF component that return a PDFLayoutResult–this
helps in creating a reference for where the bounds of the next generated
controls need to be.
5. Save and launch the PDF
Until now, all the code could be shared among all the mobile
platforms (iOS / Android / UWP), but saving and launching the PDF document
requires platform-specific code. In order to call platform code from the shared
code, it’s useful to abstract the platform implementation behind an interface, which
is then implemented for each platform. I’ve again used the command pattern for ease-of-use: