> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/open-save-document/open/view.md).

# Display documents using PDFViewCTRL with Apryse Android SDK

Learn how to open a document with PDFViewCTRL using Apryse Android SDK. View, annotate, sign, and edit PDFs with fragment in tabs. Follow Material design guidelines for a seamless experience. The Apry

{% hint style="info" %}
If you are looking for a quick start on displaying documents in your application, please first take a look at [Show a document in an Activity](/android/open-save-document/open/activity.md) or [Show a document in a Fragment](/android/open-save-document/open/fragment.md) as they are easier to setup and ready to launch from any activity or fragment. Continue reading this article if you are looking to embed [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) in your own layout.
{% endhint %}

{% hint style="info" %}
**Before beginning, make sure the Apryse library is initialized prior to inflating the layout or calling setContentView in your activity.**
{% endhint %}

[`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) is a [`ViewGroup`](https://developer.android.com/reference/android/view/ViewGroup.html) that can be embedded in any layout. It encapsulates a rich set of functionalities for interactive viewing of PDF documents, including multi-threaded rendering, PDF rendering settings, scrolling, zooming, page navigation, different page viewing modes, coordinates conversion, text selection, text search, etc.

## View documents using PDFViewCtrl

In this tutorial you will display a PDF file in your activity by using [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html).

1. In your `AndroidManifest.xml`, make sure you enable `largeHeap` in the `<application>` tag. Also, add a custom theme and set the `android:windowSoftInputMode:"adjustPan"` attribute in the `<activity>` tag as follow:

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<!-- Include existing attributes in application -->
<application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="false">
    <!-- Include existing attributes in activity -->
    <activity android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
</application>
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
**If your app is targeting Android SDK version 28 or higher, please also set the android:usesCleartextTraffic="true" attribute in your application tag to open HTTP files in the viewer. If you are only working with HTTPS files, this is not required.**
{% endhint %}

1. If you would like to customize the appearance of the viewer activity, define `PDFTronAppTheme` for your activity in `res/values/styles.xml`:You can learn more about this in the [customize the viewer's theme guide](/android/ui-customization/custom-theme-a.md).

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<resources>
<style name="PDFTronAppTheme" parent="PDFTronAppThemeBase"> <item name="colorPrimary">#3F51B5</item> <item name="colorPrimaryDark">#303F9F</item> <item name="colorAccent">#FF4081</item> <!-- Action bar --> <item name="actionModeBackground">?attr/colorPrimary</item> <item name="windowActionModeOverlay">true</item> </style>
</resources>
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
`PDFViewCtrl` uses the `AppCompat` theme for material colors. Make sure that the value of `android:theme` in your `<activity>` tag also extends the `AppCompat` theme.
{% endhint %}

1. Now, add [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) to your activity's XML layout. For example:

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

    <com.pdftron.pdf.PDFViewCtrl android:id="@+id/pdfviewctrl" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical|horizontal"/>

</FrameLayout>
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. In your activity, get a reference to [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) after inflating the layout and call [`AppUtils.setupPDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/utils/AppUtils.html#setupPDFViewCtrl\(PDFViewCtrl\)).

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
private PDFViewCtrl mPdfViewCtrl;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity_layout);
    
    mPdfViewCtrl = findViewById(R.id.pdfviewctrl);
    try {
        AppUtils.setupPDFViewCtrl(mPdfViewCtrl);
    } catch (PDFNetException e) {
        // Handle exception
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
private var mPdfViewCtrl: PDFViewCtrl? = null
// ...
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.my_activity_layout)

    mPdfViewCtrl = findViewById(R.id.pdfviewctrl)
    AppUtils.setupPDFViewCtrl(mPdfViewCtrl!!)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Next, choose a document to display by using the following options:Add a sample PDF to `src/main/res/raw` folder, then call:

### View from resource

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
import com.pdftron.pdf.utils.Utils;
// ...
private PDFDoc mPdfDoc;
// ...
public void viewFromResource(int resourceId, String fileName) throws PDFNetException {
    File file = Utils.copyResourceToLocal(this, resourceId, fileName, ".pdf");
    mPdfDoc = new PDFDoc(file.getAbsolutePath());
    mPdfViewCtrl.setDoc(mPdfDoc);
    // Alternatively, you can open the document using Uri:
    // Uri fileUri = Uri.fromFile(file);
    // mPdfDoc = mPdfViewCtrl.openPDFUri(fileUri, null);
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
import com.pdftron.pdf.utils.Utils
// ...
private var mPdfDoc: PDFDoc? = null
// ...
fun viewFromResource(resourceId: Int, fileName: String) {
    val file = Utils.copyResourceToLocal(this, resourceId, fileName, ".pdf")
    mPdfDoc = PDFDoc(file.absolutePath)
    mPdfViewCtrl?.doc = mPdfDoc
    // Alternatively, you can open the document using Uri:
    // val fileUri = Uri.fromFile(file)
    // mPdfDoc = mPdfViewCtrl?.openPDFUri(fileUri, null)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### View from local device storage

{% hint style="warning" %}
**Storage Permission**

Please follow the latest Android best practices and guidelines outlined [here](https://developer.android.com/training/permissions/usage-notes/)
{% endhint %}

## Managing lifecycle

It is extremely important that you follow the Android activity/fragment lifecycle and clean up [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) and [`PDFDoc`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFDoc.html) properly. Make sure you have the following in lifecycle callbacks:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
// ...
@Override
public void onPause() {
    super.onPause();
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.pause();
        mPdfViewCtrl.purgeMemory();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.resume();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.destroy();
        mPdfViewCtrl = null;
    }

    if (mPdfDoc != null) {
        try {
            mPdfDoc.close();
        } catch (Exception e) {
            // handle exception
        } finally {
            mPdfDoc = null;
        }
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
// ...
override fun onPause() {
  super.onPause()
  mPdfViewCtrl?.pause()
  mPdfViewCtrl?.purgeMemory()
}

override fun onResume() {
  super.onResume()
  mPdfViewCtrl?.resume()
}

override fun onDestroy() {
  super.onDestroy()
  mPdfViewCtrl?.destroy()
  mPdfViewCtrl = null
  mPdfDoc?.close()
  mPdfDoc = null
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Next steps

* Want to annotate on PDF files in [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html)? Check out the [Setup ToolManager](/android/annotation/toolmanager-config.md) guide.
* Want to display non-PDF files in [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html)? Check out the [viewing other document types](/android/ms-office/non-pdf.md) guide.

Check out the [diagram of the overall view hierarchy](/android/viewer/viewer-overview.md) for more.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/open-save-document/open/view.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
