AVAILABLE NOW: Spring 2025 Release
By Shirley Gong | 2018 Oct 31
2 min
Tags
office
view
android
Adding a seamless Word, Excel, and PowerPoint document viewer to Android apps is easy with the Apryse PDF SDK. No need for a server, Microsoft Office license, or other third-party software.
With Apryse'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 users see and interact with the document while it's being converted to provide a fast user experience. If you are interested in trying this out for yourself, download our free trial.
Although this tutorial explains how to display Office files in Android (Java and Kotlin), the capability is available on all major platforms through a single unified API, including iOS Office viewer and Xamarin Office viewer.
For the best user experience, stream conversion so users see the first few pages almost instantly, and can interact with the viewer while the remaining document is converted.
Here's how to stream and display a local .doc
, .docx
, .pptx
and .xlsx
file:
//@data {"m":true}//
public void streamNonPDFUri(Context context, @NonNull Uri inUri) {
DocumentActivity.openDocument(context, inUri);
}
//@data {"m":true}//
private fun streamNonPDFUri(context: Context, inUri: Uri) {
DocumentActivity.openDocument(context, inUri)
}
And that's it! Read more about the DocumentActivity
class in our documentation.
Here's what it looks like:
You can also convert Office files directly to PDF separately from viewing, which is useful for batch conversions. Here's how to do that:
//@data {"m":true}//
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);
}
//@data {"m":true}//
@Throws(PDFNetException::class, FileNotFoundException::class)
fun convertOfficeUri(@NonNull inUri: Uri, @NonNull outPath: String, options: ConversionOptions) {
val pdfDoc = PDFDoc()
if (ContentResolver.SCHEME_CONTENT.equals(inUri.getScheme())) {
val fileFilter = SecondaryFileFilter(getContext(), inUri)
Convert.officeToPdf(pdfDoc, fileFilter, options)
} else {
val inPath = inUri.getPath()
Convert.officeToPdf(pdfDoc, inPath, options)
}
pdfDoc.save(outPath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
}
Then display the file saved in outPath
like this:
//@data {"m":true}//
// 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);
}
//@data {"m":true}//
private fun openLocalDocument(context: Context, localFilePath: String) {
val localFile = Uri.fromFile(File(localFilePath))
DocumentActivity.openDocument(context, localFile)
}
Apryse SDK also converts various other file formats to PDF for viewing, like images (.jpeg, .jpg, .gif, .png, .bmp etc.), markdown (.md) or comic book archive (.cbz). Here's how:
//@data {"m":true}//
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);
}
//@data {"m":true}//
@Throws(PDFNetException::class, FileNotFoundException::class)
fun convertNonPDFUri(@NonNull inUri: Uri, @NonNull outPath: String, options: ConversionOptions) {
val documentConversion: DocumentConversion
if (ContentResolver.SCHEME_CONTENT.equals(inUri.getScheme())) {
val fileFilter = SecondaryFileFilter(getContext(), inUri)
documentConversion = Convert.streamingPdfConversion(fileFilter, options)
} else {
val inPath = inUri.getPath()
documentConversion = Convert.streamingPdfConversion(inPath, options)
}
documentConversion.convert()
val pdfDoc = documentConversion.getDoc()
pdfDoc.save(outPath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
}
View these converted files in the same way as the PDF.
For a richer document experience, Apryse’s Android PDF library makes it easy to add more functionality to the viewer:
Building an Office document viewer in Android is straightforward with the Apryse SDK. To give it a try, download a trial and check out our 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
view
android
Shirley Gong
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content