Adding a seamless Word, Excel, and PowerPoint document viewer to Android apps is easy with the PDFTron PDF SDK. No need for a server, Microsoft Office license, or other third party software.
With PDFTron's dependency-free Office-to-PDF conversion, Office files are converted directly to PDF in the client , and then displayed with a PDF viewer. Conversions are accurate and fast, and can be streamed so that users can see and interact with the document while it's being converted, providing a fast user experience. If you are interested in trying this out for yourself you can download our free trial .
Although this tutorial will explain how to display Office files in Android (Java and Kotlin ), this capability is available on all major platforms through a single unified API, including iOS Office viewer and Xamarin Office viewer .
Streaming Conversion For Fast Time To Interactive
For the best user experience, you can stream conversion so that the user sees the first few pages almost instantly, and is able to interact with the viewer while the rest of the document is being converted.
Here's how to stream and display a local .doc
, .docx
, .pptx
and .xlsx
file:
public void streamNonPDFUri(Context context, @NonNull Uri inUri) {
DocumentActivity.openDocument(context, inUri);
}
And that's it! You can read more about the DocumentActivity
class in our documentation.
Here's what it looks like:
Converting to PDF Independently of Viewing
Office files can also be converted directly to PDF separately from viewing, which may be useful if you want to do batch conversions. Here's how to do that:
public void convertOfficeUri(@NonNull Uri inUri, @NonNull String outPath, ConversionOptions options) throws PDFNetException, FileNotFoundException {
PDFDoc pdfDoc = new PDFDoc();
if (ContentResolver.SCHEME_CONTENT.equals(inUri.getScheme())) {
SecondaryFileFilter fileFilter = new SecondaryFileFilter(getContext(), inUri);
Convert.officeToPdf(pdfDoc, fileFilter, options);
} else {
String inPath = inUri.getPath();
Convert.officeToPdf(pdfDoc, inPath, options);
}
pdfDoc.save(outPath, SDFDoc.SaveMode.REMOVE_UNUSED, null);
}
You can now display the file saved in outPath
like this:
// Open a local document given a path
private void openLocalDocument(Context context, String localFilePath) {
final Uri localFile = Uri.fromFile(new File(localFilePath));
DocumentActivity.openDocument(context, localFile);
}
Converting Other File Formats
PDFTron SDK can also convert a variety of other file formats to PDF for viewing, such as images (.jpeg, .jpg, .gif, .png, .bmp etc.), markdown (.md) or comic book archive (.cbz). Here's how:
public void convertNonPDFUri(@NonNull Uri inUri, @NonNull String outPath, ConversionOptions options) throws PDFNetException, FileNotFoundException {
DocumentConversion documentConversion;
if (ContentResolver.SCHEME_CONTENT.equals(inUri.getScheme())) {
SecondaryFileFilter fileFilter = new SecondaryFileFilter(getContext(), inUri);
documentConversion = Convert.streamingPdfConversion(fileFilter, options);
} else {
String inPath = inUri.getPath();
documentConversion = Convert.streamingPdfConversion(inPath, options);
}
documentConversion.convert();
PDFDoc pdfDoc = documentConversion.getDoc();
pdfDoc.save(outPath, SDFDoc.SaveMode.REMOVE_UNUSED, null);
}
These converted files can now be viewed in the same way as the PDF.
Adding More Functionality
For a richer document experience, PDFTron’s Android PDF library makes it easy to add more functionality to the viewer:
- Annotation creation & editing
- Multi-tab viewing makes it easy to flip between documents
- A collection of different viewing modes such as one page horizontal swiping, two page horizontal swiping, one page vertical swiping, two page vertical swiping and magazine mode
- Form filling
- Text search
- Advanced navigation to find information quickly through a thumbnail slider , document outline , annotations list , and thumbnail grid
- Page manipulation , including page deletion and rearranging
- Night mode for a more enjoyable viewing experience in dark environments
- System file sharing (such as email or to another app)
- And much more...
Conclusion
Building an Office document viewer in Android is straightforward with the PDFTron SDK. To give it a try, download a trial and check out our 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.