Using a Fragment

PdfFragment is meant for users who want to fully customize and control the PDF display. It shows the document only and does not feature additional views like the thumbnail bar, search, outline, etc. These are available via the ready-to-use PDF viewer activities.

Setting Up the Manifest

The first step before using PdfFragment 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:

AndroidManifest.xml
1
2
3
<application android:largeHeap="true">
    ...
</application>

Configuring the Fragment

Just like PdfActivity can be configured, PdfFragment allows passing in a configuration object that defines various options. Create the PdfConfiguration and pass the desired settings, e.g. scroll orientation, scroll mode, and rendering settings:

1
2
3
val configuration = PdfConfiguration.Builder()
    .scrollDirection(PageScrollDirection.HORIZONTAL)
    .build()
1
2
3
final PdfConfiguration configuration = new PdfConfiguration.Builder()
    .scrollDirection(PageScrollDirection.HORIZONTAL)
    .build();

Adding PdfFragment to Your Activity

Add 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:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_custom_fragment)

    // First, try to restore a previously created fragment. If no fragment exists, create a new one.
    val fragment = supportFragmentManager.findFragmentById(R.id.fragmentContainer) as PdfFragment
        ?: createFragment(documentUri, configuration)
}

private fun createFragment(documentUri: Uri, configuration: PdfConfiguration): PdfFragment {
    val fragment = PdfFragment.newInstance(documentUri, configuration)
    supportFragmentManager.beginTransaction()
        .replace(R.id.fragmentContainer, fragment)
        .commit()
    return fragment
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_fragment);

    // First, try to restore a previously created fragment.
    PdfFragment fragment = (PdfFragment) getSupportFragmentManager()
        .findFragmentById(R.id.fragmentContainer);

    // If no fragment exists, create a new one.
    if (fragment == null) {
        fragment = PdfFragment.newInstance(documentUri, configuration);
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.fragmentContainer, fragment)
            .commit();
    }
}

Note: Since PdfFragment extends android.support.v4.app.Fragment, your activity needs to subclass FragmentActivity, which is part of the Android Support Library v4. Once this is done, you can then access the fragment manager using getSupportFragmentManager().

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 PdfFragment guide.