Click here to Skip to main content
15,861,125 members
Articles / Mobile Apps / Xamarin
Article

Render PDF Pages with Xamarin.Android

22 Nov 2016CPOL1 min read 19.4K   3  
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
<?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:

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
Worked for some years as a software engineer, architect and project leader for different software companies. Works at TallComponents, vendor of class libraries for creating, manipulating and rendering PDF documents.

Comments and Discussions

 
-- There are no messages in this forum --