The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
The Portable Document Format — better known as PDF — has been around for over two decades, and it is still going strong today. As a result, we often have a need to show PDF files to users, whether those files are:
This is another one of those topics that seems fairly simple on the surface, but can get complicated based on your requirements. In particular, if you want to try to present the PDF to the user in your app, as opposed to launching some external PDF viewing app, while you have a few options, they all have their issues.
In this chapter, we will review several ways of displaying a PDF to the user, so you can choose what approach (or approaches) are the best fit for your requirements.
Understanding this chapter requires that you have read the core chapters of this book.
If there are several ways of displaying a PDF, we need some criteria by which to evaluate those options and make our choice. Here are some likely criteria to use, though you may have others that are more specific to your app’s requirements.
Most of the PDF-viewing options assume that the PDF is on the device already. However, even within that, there are a few possibilities as to where the PDF might be:
getFilesDir()) of your app
getExternalFilesDir()) associated with your app
Urithat you obtained via
And, occasionally, developers get boxed into a corner, with a requirement to show a PDF without downloading the PDF.
The opposite problem is the offline viewing experience. If the PDF is on the device already, ideally, the PDF-viewing technology can show that PDF without having to have an active Internet connection. After all, many users do not have continuous Internet access.
While the PDF file format has been around for years and years, it has expanded over that time. In the beginning, PDF did not offer things like form fields, annotations, and the like. The more feature-rich your PDF is, the more likely it is that some PDF viewers may be incapable of handling all of those features.
This is not restricted to in-app viewing solutions. PDF viewing apps vary in capability, and there will be some that either do not support your desired features (e.g., allow the user to fill in forms) or only will if the user unlocks the feature through some purchase.
Many PDF-viewing apps are continuously updated. If the user happens to choose one that is not, that is the user’s fault, not yours.
However, when you build in PDF viewing into your app, now you need to ensure that your solution is up to date and likely to stay stable for the duration of your app’s use.
The big one, in terms of the major options for viewing the PDF, is the degree of privacy that is tied to that PDF.
If the document comes from the user — for example, it came in an email attachment to the user’s inbox that you are managing — you should assume a modest level of privacy. Do not ship that PDF to other servers without the user’s approval. However, if the user wants to view the PDF, you should not be afraid of allowing the user to do so using the user’s chosen PDF viewing app.
If the document is more tightly controlled by your app — for example, it came as an attachment to some secure messaging client that you are writing — you may be nervous about granting other apps access to that PDF. After all, you do not necessarily know what those other apps might do with the PDF.
Where things get very messy is when the developers think that they “own” the PDF, and therefore are trying to restrict that PDF’s access, much as how a DRM solution tries to restrict access to videos. Many PDF-viewing apps offer printing, sharing, and other things that the user might want, but that the developers do not want. In truth, it is rather likely that the user can get to the PDF anyway, whether the developers like it or not, by various means (e.g., extracting PDFs packaged in the APK using an ordinary ZIP file utility). But, this seems to be the #1 reason why some developers are trying to view PDF files within their own app: treating the user as the enemy.
The preview of this section is in an invisible, microscopic font.
The preview of this section was whisked away by a shark-infested tornado.
The preview of this section apparently resembled a Pokémon.
The preview of this section was traded for a bag of magic beans.
The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.
The preview of this section was lost due to a rupture in the space-time continuum.