Featured image of post MuPDF Xamarin Android binding on Nuget

MuPDF Xamarin Android binding on Nuget

MuPDF is a fast and small PDF library. With this MuPDF Xamarin Android binding available on Nuget, you will be able to render and manipulate PDF files.

This post was originally published in 2015 and may contain outdated information.

MuPDF is a small, fast and free PDF library written in portable C, available for Android. You can use this library for its built-in PDF viewer, for rendering PDF pages to images with a high-quality, manipulating PDF files in various ways…

It supports PDF 1.7 with transparency, encryption, hyperlinks, annotations, searching and more. It also reads OpenXPS documents. It does not support interactive features such as form filling and JavaScript.

If you want to use MuPDF in your Xamarin Android application, you can use this MuPDF Xamarin Android Binding nuget package that I made.

Here are the steps I followed to create this nuget package:

  • First, building the native MuPDF library creates a Java project for Eclipse ADT,
  • Secondly, the build of the Android project produces a JAR and four .so files,
  • Finally, I use a Android library project binding for Xamarin Android, which results in a DLL that I wrapped in a nuget package.

The source code of the MuPDF Xamarin Android library is available on GitHub. MuPDF is released under the terms of the Affero GNU General Public License.

# Rendering all pages of a PDF file to PNG with the MuPDF Xamarin Android library

var pdf = new MuPDFCore(this, pdfFilepath);
var cookie = new MuPDFCore.Cookie(pdf);
var count = pdf.CountPages();
for (int i = 0; i < count; i++)
    var size = pdf.GetPageSize(i);
    int pageWidth = (int)size.X;
    int pageHeight = (int)size.Y;
    var bitmap = Bitmap.CreateBitmap(ScreenWidth, ScreenHeight, Bitmap.Config.Argb8888);
    pdf.DrawPage(bitmap, i, pageWidth, pageHeight, 0, 0, ScreenWidth, ScreenHeight, cookie);
    String filename = String.Format("/mnt/sdcard/pdf-{0}.png", i);
    using (var fos = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
        bitmap.Compress(Bitmap.CompressFormat.Png, 100, fos);

# Using the MuPDF viewer included in the MuPDF Xamarin Android library

var uri = Uri.Parse("/mnt/sdcard/some.pdf");
var intent = new Intent(this, typeof (MuPDFActivity));
Licensed under CC BY 4.0
Ko-fi donations Buy me a coffee