Click here to Skip to main content
13,146,742 members (79,740 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.4K views
3 bookmarked
Posted 22 Nov 2016

Render PDF Pages with Xamarin.Android

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

Editorial Note

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

Share

About the Author

Frank Rem
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.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 22 Nov 2016
Article Copyright 2016 by Frank Rem
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid