Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Render PDF Pages with Xamarin.Android

22 Nov 2016 1  
This article explains how to render PDF pages with Xamarin.Android.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

This article shows how to use PDFRasterizer.NET 4.0 to render a PDF page to a bitmap and to an Android canvas. PDFRasterizer.NET 4.0 supports various frameworks including Xamarin.Android. This article was written when PDFRasterizer.NET 4.0 was still in beta. You can view the latest changelog here.

Render PDF page to PNG

The code discussed here draws a PDF page in a Xamarin.Android app by converting a PDF page to a bitmap and assigning that bitmap to an ImageView. To keep the code sample simple, the PDF document itself is embedded as a resource. In a more realistic code sample it would be selected from the device or from cloud storage.

We will show the relevant code snippets. You can download the full source code from github.

Layout

The main layout of the application only has an ImageView and looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <ImageView
      android:id="@+id/imageView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

</LinearLayout>

Activity.OnCreateLayout

The main activity overrides the OnCreate and performs the following tasks:

  • create a PDF Page object from "tiger.pdf" that is embedded as a resource
  • render the PDF Page object to a Bitmap
  • assign the Bitmap to the ImageView that is part of the main layout

It looks as follows:

protected async override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Main);

    var assembly = Assembly.GetExecutingAssembly();
    var inputStream = new MemoryStream();

    // create a PDF Page object from "tiger.pdf" that is embedded as a resource
    using (Stream resourceStream = assembly.GetManifestResourceStream("DrawPdf.Android.tiger.pdf"))
    {
        resourceStream.CopyTo(inputStream);
    }

    using (var outputStream = new MemoryStream())
    {
        // render PDF Page object to a Bitmap
        await Task.Run(() =>
        {
            Document document = new Document(inputStream);
            Page page = document.Pages[0];
            page.SaveAsBitmap(outputStream, CompressFormat.Png, 72);
        });

        // assign the Bitmap to the ImageView
        Bitmap bmp = global::Android.Graphics.BitmapFactory.DecodeByteArray(outputStream.GetBuffer(), 0, (int) outputStream.Length);
        ImageView imageView = FindViewById<ImageView>(Resource.Id.imageView);
        imageView.SetImageBitmap(bmp);
    }
}

Render PDF to Android.Canvas

The code discussed here implements a custom View (PdfPageView) and draws a PDF page directly to an Android Canvas.

We will show the relevant code snippets. You can download the full source code from github.

Activity.OnCreate

Inside OnCreate of the main activity, a Page instance is contructed and passed to the PdfPageView ctor. The custom view is set as the content view:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // create PDF page from the PDF document which is embedded as a resource
    var assembly = Assembly.GetExecutingAssembly();
    var inputStream = new MemoryStream();
    using (Stream resourceStream = assembly.GetManifestResourceStream("DrawPdf.Android.tiger.pdf"))
    {
        resourceStream.CopyTo(inputStream);
    }
    Document document = new Document(inputStream);
    Page page = document.Pages[0];
    
    // create a customized view for the page
    SetContentView(new PdfPageView(this, page));
}

PdfPageView

The custom View uses the Page.Draw method to draw directly to the canvas. It looks like this:

public class PdfPageView : View
{
    Page _page;

    public PdfPageView(Context context, Page page) :
        base(context)
    {
        _page = page;
    }

    protected override void OnDraw(Canvas canvas)
    {
        _page.Draw(canvas, 1);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here