It's possible to display Office documents in a Xamarin app without using a server, Microsoft Office license, or other third party software. The PDFTron PDF SDKwill convert Office files to PDF directly in the client, with no dependencies.

This tutorial will walk you through how to do this on Xamarin.Android, Xamarin.iOS, and Xamarin.UWP.

If you are interested in trying this out for yourself you can download our trial.

Let's begin!

linkPart 1 - Xamarin.Android

Office files are converted to PDF before viewing, but we can stream the file as the conversion happens so users can see and interact with the document almost instantly:

public void StreamNonPDFUri(Context context, Uri inUri)
{
    DocumentActivity.OpenDocument(context, inUri);
}

Here's what it looks like:

If you need to perform batch conversions without displaying the PDF, this can be separated into two discrete steps. Here's how to convert Office files to PDF without displaying/streaming:

public void ConvertOfficeFile(string inPath, string outPath, ConversionOptions options)
{
    using (PDFDoc pdfDoc = new PDFDoc())
    {
        Convert.OfficeToPdf(pdfDoc, inPath, options);
        pdfDoc.Save(outPath, SDFDoc.SaveOptions.e_remove_unused);
    }
}

And then if you just want to display the PDF:

// Open a local document given a path
private void OpenLocalDocument(Context context, String localFilePath) {
    var localFile = Uri.FromFile(new File(localFilePath));
    DocumentActivity.OpenDocument(context, localFile);
}

The PDFTron SDK also supports converting many other formats to PDF for display, like images (.jpeg, .jpg, .gif, .png, .bmp etc.), markdown (.md) or comic book archive (.cbz). Here's how:

public void ConvertNonPDFFile(string inPath, string outPath, ConversionOptions options)
{
    var documentConversion = Convert.StreamingPDFConversion(inPath, options);
    documentConversion.Convert();
    var pdfDoc = documentConversion.GetDoc();
    pdfDoc.Save(outPath, SDFDoc.SaveOptions.e_remove_unused);
}

These converted files can now be viewed in the same way as the PDF.

You can read more in our Xamarin.Android documentation, or you can give it a try by downloading a trial.

linkPart 2 - Xamarin.iOS

Next we'll show you how to do this on Xamarin.iOS. The first thing we need to do is to convert the Office document to PDF, like so:

public void ConvertOfficeFile(string inPath, string outPath, ConversionOptions options)
{
    using (PDFDoc pdfDoc = new PDFDoc())
    {
        Convert.officeToPdf(pdfDoc, inPath, options);
        pdfDoc.Save(outPath, SDFDoc.SaveOptions.e_remove_unused);
    }
}

Once converted to PDF, we can display it:

PTDocumentViewController documentViewController = new PTDocumentViewController();
UINavigationController navigationController = new UINavigationController(documentViewController);
NSUrl fileURL = NSBundle.MainBundle.GetUrlForResource("sample", "pdf");
NSError error = null;
bool success = documentViewController.OpenDocumentWithURL(fileURL, out error);
if (!success)
{
    Console.WriteLine("OpenDocumentWithURL failed...");
}
documentViewController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => {
    documentViewController.DismissViewController(true, null);
});
this.PresentViewController(navigationController, true, null);

Here's what it looks like:

If you'd like to support other file formats, you can convert them:

public void ConvertNonPDFFile(string inPath, string outPath, ConversionOptions options)
{
    var documentConversion = Convert.StreamingPDFConversion(inPath, options);
    documentConversion.Convert();
    var pdfDoc = documentConversion.GetDoc();
    pdfDoc.Save(outPath, SDFDoc.SaveOptions.e_remove_unused);
}

These converted files can now be viewed in the same way as the PDF.

Read more in our Xamarin.iOS documentation, or download a trial.

linkPart 3 - Xamarin.UWP

Just like with Xamarin.Android, the Office-to-PDF conversion process on UWP can be streamed so users can see and interact with the document almost instantly:

public void StreamNonPDFFile(IRandomAccessStream inStream, OfficeToPDFOptions options)
{
    IFilter filter = new RandomAccessStreamFilter(inStream);
    options.SetLayoutResourcesPluginPath(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));
    options.SetSmartSubstitutionPluginPath(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));

    DocumentConversion conversion = pdftron.PDF.Convert.StreamingPDFConversion(filter, options);
    _PDFViewCtrl.OpenUniversalDocument(conversion);
}

Here's what it looks like:

Here's how to convert Office documents to PDF without the display step:

public async void ConvertOfficeFile(IRandomAccessStream inStream, StorageFile outFile, OfficeToPDFOptions options)
{
    PDFDoc pdfDoc = new PDFDoc();
    IFilter filter = new RandomAccessStreamFilter(inStream);
    options.SetLayoutResourcesPluginPath(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));
    options.SetSmartSubstitutionPluginPath(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));

    pdftron.PDF.Convert.OfficeToPDF(pdfDoc, filter, options);
    await pdfDoc.SaveAsync(outFile, pdftron.SDF.SDFDocSaveOptions.e_remove_unused);
}

If you just want to display the PDF:

public void OpenStorageFile(StorageFile outFile)
{
    PDFDoc pdfDoc = new PDFDoc(outFile);
    _PDFViewCtrl.SetDoc(pdfDoc);
}

And here's how to convert other file formats to PDF:

public async void ConvertNonPDFFile(IRandomAccessStream inStream, StorageFile outFile, OfficeToPDFOptions options)
{
    IFilter filter = new RandomAccessStreamFilter(inStream);
    options.SetLayoutResourcesPluginPath(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));
    options.SetSmartSubstitutionPluginPath(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));
    DocumentConversion documentConversion = pdftron.PDF.Convert.StreamingPDFConversion(filter, options);
    documentConversion.Convert();
    PDFDoc pdfDoc = documentConversion.GetDoc();
    await pdfDoc.SaveAsync(outFile, pdftron.SDF.SDFDocSaveOptions.e_remove_unused);
}

These converted files can now be viewed in the same way as the PDF.

You can read more in our Xamarin.UWP documentation, or you can give it a try by downloading a trial.

linkAdding More Functionality

For a richer document experience, the PDFTron SDK makes it easy to add more functionality to the viewer:

  • Annotation creation and editing
  • Form filling
  • Text search
  • Page manipulation
  • Customizable UI controls
  • Multi-tabbed PDF viewing
  • Reflow
  • Night and custom viewing modes
  • And many more...

linkConclusion

Adding an Office document viewer to your Xamarin app is simple with the PDFTron SDK. To give it a try, download a trial and check out our Xamarin.Android, Xamarin.iOS, and Xamarin.UWP documentation.

For further guidance, please get in touch. The developers who helped build our SDK from the ground up would be happy to walk you through your options.