PSPDFKit provides two kinds of fragments for displaying PDF documents:
PdfFragment is meant for users who want to fully customize and control the PDF display. It shows only the document and does not feature additional views like the thumbnail bar, search, outline, etc.
PdfUiFragment works like the ready-to-use PDF viewer activities and provides all the features the
PdfActivity provides. You can use this if using a
PdfActivity is not possible because you have to use a specific activity subclass already. However, if at all possible, we still recommend using
PdfActivity over using
Setting Up the Manifest
No matter which kind of fragment you end up using, the first step before adding either in your activity is to ensure that your application is prepared for displaying and editing PDFs.
Make sure you have the
android:largeHeap="true" property in your
<application> tag in
AndroidManifest.xml. Rendering PDF files can be memory intensive, and this property will ensure your app has enough heap allocated to avoid hitting out-of-memory errors:
Configuring the Fragment
Just like the
PdfUiFragment allow passing in a configuration object that defines various options.
PdfFragment, create the
PdfConfiguration and pass the desired settings, e.g. scroll orientation, scroll mode, and rendering settings:
PdfUiFragment use a
PdfActivityConfiguration instead of
PdfConfiguration. Otherwise, the configuration works the same.
Adding Fragment to Your Activity
PdfFragment to your activity just like you would any other Android fragment. Create the fragment using
PdfFragment.newInstance() and attach it to your activity. This is usually done within the
onCreate() method of your activity. Since fragments are retained over configuration changes, make sure you don’t recreate the fragment if it already exists:
PdfUiFragment works similar to how
PdfFragment does, but you should use
PdfUiFragmentBuilder to obtain an instance of
ℹ️ Note: Since
androidx.fragment.app.Fragment, your activity needs to subclass
FragmentActivity, which is part of the AndroidX libraries. Once this is done, you can then access the fragment manager using
Building an Activity around PdfFragment
PSPDFKit ships with all the views used in
PdfActivity as standalone widgets. If you wish to use these existing PSPDFKit widgets in your custom activities, follow the Building an Activity around