Tuesday, June 11, 2013

PDF to image on Android, easy steps with Xamarin

      Let’s assume that you are developing an Android version of the already existing .NET application (desktop or web).And this application processes PDF files and saves them as images somewhere.

      I would personally look for a cross –platform solution that would reduce the amount of code I’d have to rewrite in order to target all these platforms.

      One of the solutions that could be easily found is to use Xamarin - a great project based on Mono and now becoming very popular for writing apps working on iOS, Mac, Windows and Android.

      That’s the base part, but I also need a library that works fine with Xamarin and can do PDF to image conversion. In my previous post  Creatingcross-platform PDF viewer using GTK# I demonstrated how we can create a simple application that shows PDF content on Windows and Mac OS X , and now it’s Android app’s turn.


So let’s follow step-by-step approach taken in previous posts and get this working.

STEP I - Setup

      Refer to the post Creatingcross-platform PDF viewer using GTK#  on how to setup environment, download latest Apitron PDF Rasterizer for .NET package and unzip it to the desired location. 

STEP II – Getting things built

       To make things simpler we provide a sample project called AndroidSample  that can be found in Samples folder inside the downloaded package. Open it using desired IDE, I will use MS VS.
       If everything is set up correctly project opens fine and builds without errors. If build fails, make sure that correct component dll is referenced. Remove the old reference and add new one, navigating to [Package_Folder]\ Microsoft.NET v2.0Android\Apitron.PDF.Rasterizer.dll.
                Otherwise it might be something wrong with environment setup, please read Xamarin documentation or post a comment here, I’ll try to help.

STEP III - Playground

       Ok, you’ve got it compiled! And now to make it more interesting, let’s not use the default PDF file provided with the sample (you may still use it, no problem, just hit Run). I’m going to add a new file, from
       For those decided to go this way, download the file by the link above and put in into Assets directory of the project, don’t forget to include it in project with AndroidAsset build action. Also  navigate to MainActivity.cs  and change button_click handler, replacing “testfile.pdf” to “3bigpreview.pdf”.

Build and hit Run, you should see the similar screen as below:



I used physical device connected to the PC, but you may want to run it on emulator also, to do it - click Start emulator image and run desired emulator.

So click OK and application deployment begins… once it’s finished, the application starts:



You see that application has very simple UI layout with just one button that says “Click me to start rendering”, let’s click it and see what happens.

Application starts processing the file that we have placed into its Assets folder, and when it finishes the following screen appears:


Done!

THE CODE


Here is the actual code that renders and saves image to storage, it is located in MainActivity.cs

// load the linked sample pdf file
using (Stream stream = Assets.Open("3bigpreview.pdf"), ms = new MemoryStream())
{
    stream.CopyTo(ms);

    ms.Position = 0;

    // create focument from the stream and request first page
    Document doc = new Document(ms);

    Page page = doc.Pages[0];

    // render the page using default settings
    int[] bitmapData = page.RenderAsInts((int)page.Width, (int)page.Height, new RenderingSettings());

    // convert given data to an android bitmap
    Bitmap bm = Bitmap.CreateBitmap(bitmapData, (int)page.Width, (int)page.Height, Bitmap.Config.Argb8888);

    // save image to device's gallery
    string imageUri = MediaStore.Images.Media.InsertImage(this.ContentResolver, bm, "MyImage", "An image created using Apitron.PDF.Rasterizer");

    // try to open the newly created image
    StartActivity( new Intent( Intent.ActionView,  Android.Net.Uri.Parse(imageUri)) );
}

No comments:

Post a Comment