Search code examples
androidpdfdocx

Android native support for previewing PDF files and other documents?


Quick Version:

Is there a native class in Android, similar to WKWebView on iOS, that can show Word documents and PDFs?

Long Version:

I'm working on porting the native wrapper part of our hybrid application to Android. In iOS I can use WKWebView as a way to display previews of various kinds of documents, including specifically Word (.docx) documents and PDF files.

Using view intents I can get Android to open a PDF in Adobe's viewer, but before installing that I'd just get an exception to the effect that an app to view the file type could not be found. Is there really no native support in Android for something so common? The Files app on my phone (where I can view downloads etc.) can open and show PDFs, but clearly doesn't register the fact with the operating system.


Solution

  • Is there a native class in Android, similar to WKWebView on iOS, that can show Word documents and PDFs?

    If by "native class" you mean "a Java class built into the Android SDK", the answer is "not really". There is nothing for DOCX. Android 5.0+ has a PdfRenderer, but it is designed for print preview of PDFs generated using Android's printing framework and will fail to render many arbitrary PDFs.

    In iOS I can use WKWebView as a way to display previews of various kinds of documents, including specifically Word (.docx) documents and PDF files.

    There are open source and commercial libraries that offer this. I blogged about some open source PDF-rendering options in early 2017.

    Using view intents I can get Android to open a PDF in Adobe's viewer, but before installing that I'd just get an exception to the effect that an app to view the file type could not be found.

    That has nothing to do with "a native class". An Intent is handled by apps and their registered components (in your case, activities).

    At the present time, there is no app that is part of the Android Open Source Project (AOSP) that is a PDF viewer or a viewer of other document formats. Whether any given device will have one (e.g., Google Docs) will vary.

    Is there really no native support in Android for something so common?

    In terms of apps, there is no requirement for a device manufacturer to pre-install a PDF viewer or a DOCX viewer. Even if they have one, there is no requirement for the developers of those viewers to register an <intent-filter> that supports whatever Intent structure that you used. Since your Intent is not in your question, we cannot really comment on how common or uncommon support will be for that Intent.