Xamarin.Android pdf generator

casillas picture casillas · Oct 12, 2015 · Viewed 7.2k times · Source

I have been working on Xamarin.Android recently. I need to use pdf generator to send a report via email.

I have been came across to the following blog. I do not really know what to put in the FileStream fs = new FileStream (???????); In addition to that, I would like to open or see that pdf on the screen.

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;
using XamiTextSharpLGPL;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp;



namespace PDFAapp
{
    [Activity (Label = "PDFAapp", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

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

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            FileStream fs = new FileStream (???????);
            Document document = new Document (PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance (document, fs);
            document.Add(new Paragraph("Hello World"));
            document.Close();
            writer.Close();
            fs.Close();
        }
    }
}

EDIT 1:

Having the following code to open the generated Pdf, but it shows pdf format is not valid.

Java.IO.File file = new Java.IO.File(filePath);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
StartActivity(intent);

Answer

Milen picture Milen · Oct 13, 2015

First make sure in the Manifest file you allow WriteExternalStorage

In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions. For example:

 <manifest ...>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 </manifest>

FileStream constructor (one of the many) accepts string for a path and FileMode so an example solution would be.

  var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString();
  if (!Directory.Exists(directory))
  {
      Directory.CreateDirectory(directory);
  }

  var path = Path.Combine(directory, "myTestFile.pdf");
  var fs = new FileStream(path, FileMode.Create);

This creates folder called "pdf" in to external storage and then creates the pdf file. Additional code could be included to check if file exists before creating it..

Edit: complete example, just tested on my device:

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

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString();
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            var path = Path.Combine(directory, "myTestFile.pdf");

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            var fs = new FileStream(path, FileMode.Create);
            Document document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
            document.Open();
            document.Add(new Paragraph("Hello World"));
            document.Close();
            writer.Close();
            fs.Close();

            Java.IO.File file = new Java.IO.File(path);
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
            StartActivity(intent);
        }