AVAILABLE NOW: Spring 2025 Release
By Shirley Gong | 2018 Nov 02
3 min
Tags
office
xamarin
view
tutorial
It's possible to display Office documents in a Xamarin app without using a server, Microsoft Office license, or other third-party software. The Apryse Office PDF SDK converts Office files to PDF directly in the client, with no dependencies.
This tutorial walks 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!
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 Apryse Xamarin PDF library 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);
}
You can then view these converted files in the same way as the PDF.
Learn more about converting Excel, Word, PPT to PDF in Xamarin.Android, or get started with our SDK by downloading a free trial.
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");
documentViewController.OpenDocumentWithURL(fileURL);
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 in Xamarin.iOS, 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.
Learn more about converting Excel, Word, PPT to PDF in Xamarin.iOS, or get started with our SDK by downloading a free trial.
Just like with Xamarin.Android, the UWP Office-to-PDF conversion process on 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 in Xamarin.UWP 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.
Learn more about converting and viewing Microsoft Office files in UWP, or get started with our Office PDF SDK by downloading a free trial.
For a richer document experience, the Apryse Xamarin PDF library makes it easy to add more functionality to the viewer:
Adding an Office document viewer to your Xamarin app is simple with the Apryse SDK. To give it a try, download a free trial and check out our Xamarin.Android, Xamarin.iOS, and Xamarin.UWP documentation.
For further guidance, please feel free to check out our comprehensive SDK Buying Guide, or get in touch. The developers who helped build our SDK from the ground up would be happy to walk you through your options
Tags
office
xamarin
view
tutorial
Shirley Gong
Related Products
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content