Generate PDF using Xamarin Forms in 5 Easy Steps | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Guest Blog Post: Malcolm Jack Generates PDF Documents Using Xamarin.Forms and Syncfusion in 5 Easy Steps

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 Xamarin.

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 document

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 document

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 document

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:

 

Now each platform should provide an implementation, which is resolved using Xamarin.Forms DependencyService (or you can wire it up using another container service like Autofac ). An example of the Android implementation can be found here

A screen grab of the finished product:

Bonus

The Syncfusion Xamarin components are pretty handy and, in my experience, the support I’ve received from them has been exceptional. The best part, though, is that all of it is available for FREE–if you’re a single developer or a small business. Check out the Community License here.

As someone who has a side project that he hopes to turn into a business one day, it’s these kinds of licensing models that are really helpful: thanks, Syncfusion!

Check out Malcom Jack’s full blog here!

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed

Table of Contents