How to Build an Android PDF Viewer or Editor in Java
Published: 2023-08-02 11:44:43

<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">In today's mobile-first world, creating an Android app is a must-have for businesses and developers alike. And, with the increasing need to work with PDF documents, building an Android PDF viewer and editor with ComPDFKit, a powerful PDF SDK, enable your end users to view and edit PDFs with ease.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">In this blog, we'll start by exploring the necessary steps to integrate the ComPDFKit PDF SDK, and build an Android PDF Reader with ComPDFKit.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<h2 style="line-height: 1.4;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 36px;">Get Started with ComPDFKit Android PDF SDK</span></strong></h2>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">ComPDFKit is a powerful PDF SDK. It is easy to embed ComPDFKit PDF SDK in your Android application with a few lines of Java code. Take just a few minutes and get started.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">The following sections introduce the requirements, structure of the installation package, and how to make an Android PDF Reader in Java with ComPDFKit PDF SDK.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<h3 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 24px;">Requirements</span></strong></h3>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">ComPDFKit PDF SDK is supported on Android devices running API level 19 or newer and targeting the latest stable Android version 4.4 or higher. Furthermore, ComPDFKit PDF SDK requires apps to enable Java 8 language features to build.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- Android Studio 3.2 or newer (support AndroidX).</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- Project specifications.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A<span spellcheck="false">&nbsp;<em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">minSdkVersion</code></span></span></em></span> of<span spellcheck="false">&nbsp;<em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">19</code></span></span></em></span>&nbsp;or higher. </span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A <span spellcheck="false"><em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">compileSdkVersion</code></span></span></em></span> of&nbsp;<span spellcheck="false"><em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">30</code></span></span></em></span>&nbsp;or higher. </span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A <em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">targetSdkVersion</code> </span></span></em>of <em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">30</code></span></span></em>&nbsp;or higher. </span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Android ABI(s): x86, x86_64, armeabi-v7a, arm64-v8a.</span></p>
<p style="text-align: justify; line-height: 2;">&nbsp;</p>
<h3 style="line-height: 2;"><span style="font-size: 24px;"><strong><span style="font-family: arial, helvetica, sans-serif;">Android Package Structure</span></strong></span></h3>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">The package of ComPDFKit PDF SDK for Android includes the following files:</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- <strong>libs</strong> - A folder containing <em><strong>ComPDFKit.aar</strong></em>, <em><strong>ComPDFKit-UI.aar</strong></em>, and <em><strong>ComPDFKit-Utils.aar</strong></em>. <em><strong>ComPDFKit.aar</strong></em> is PDF Core API. <em><strong>ComPDFKit-UI.aar</strong></em> is the PDF View. <em><strong>ComPDFKit-Utils.aar</strong></em> includes UI extension components based on the fundamental layout classes:&nbsp;<span spellcheck="false"><em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">ReaderFragment</code></span></span></em></span>&nbsp;or&nbsp;<span spellcheck="false"><em style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: medium;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">ReaderActivity</code></span></span></em></span>.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- <strong>PDFViewer</strong> - A folder containing Android sample projects.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- <strong>api_reference_android</strong> - API reference.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- <strong>developer_guide_android.pdf</strong> - Developer guide.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- <strong>release_notes.txt</strong> - Release information.</span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- <strong>legal.txt</strong> - Legal and copyright information.</span></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/6440902a58f6f.png" alt="Android Package Structure" width="600" height="113" /></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;">&nbsp;</p>
<h2 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 36px;">Make an Android PDF Viewer in Java</span></strong></h2>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">This section will help you to quickly get started with ComPDFKit PDF SDK to make an Android app in Java with step-by-step instructions. Through the following steps, you will get a simple application that can display the contents of a specified PDF file.</span></p>
<h3 style="text-align: justify; line-height: 2;"><span style="font-size: 16px;">&nbsp;</span></h3>
<h3 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 24px;">Step 1: Create a New Project</span></strong></h3>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">1. Use Android Studio to create a Phone &amp; Tablet project. Here we create a No Activity project.</span></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;" data-src="create_project.png"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/6440923e4adac.png" alt="Create a New Project" width="600" height="434" /></span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<h3 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 24px;">Step 2: Add ComPDFKit PDF SDK Package</span></strong></h3>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">1. The first thing we need to do is to import ComPDFKit PDF SDK. Copy <em><strong>ComPDFKit.aar</strong></em> and <em><strong>ComPDFKit-UI.aar</strong></em> to the <em><strong>libs</strong></em> directory of the <em><strong>app</strong></em>.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2;"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/644092d43378c.png" alt="Libs" width="600" height="390" /></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">2. Add the following code into the <strong>app</strong> dictionary&rsquo;s <em><strong>build.gradle</strong></em> file:</span></p>
<pre class="language-java"><code>...
dependencies {
    /*ComPDFKit SDK*/
    implementation(fileTree('libs'))
    ...
}
...</code></pre>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">3. Add ComPDFKit PDF SDK for Android as a dependency to the project. Inside the <strong>app</strong> dictionary&rsquo;s <em><strong>build.gradle</strong></em>, add <em><strong>ComPDFKit.aar</strong></em>, <em><strong>ComPDFKit-UI.aar</strong></em> and the related support libraries to the </span><em style="text-align: justify;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">dependencies</code></span></span></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">. For simplicity, update the </span><em style="text-align: justify;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">dependencies</code> </span></span></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">as follows:</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<pre class="language-java"><code>dependencies {
    ...
    //glide
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    implementation 'androidx.documentfile:documentfile:1.0.1'
}</code></pre>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">4. Apply for read and write permissions in&nbsp;</span><strong style="font-family: arial, helvetica, sans-serif; font-size: 18px; text-align: justify;">AndroidManifest.xml</strong><span style="font-family: arial, helvetica, sans-serif; font-size: 18px; text-align: justify;">:</span></p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52r8kkgjdszn4jrbw43k.png)

<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;"><strong>Note</strong>: <em>On your apps that target Android 6.0 or higher, make sure to check for and request read and write permissions to external storage at runtime.</em></span></p>
<p style="text-align: justify; line-height: 2;">&nbsp;</p>
<h3 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 24px;">Step 3: Apply the License Key</span></strong></h3>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">Add this license in the <strong>AndroidManifest.xml</strong> of the main module:</span></p>



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqo41nt4g87rge3wcva9.png)

<p style="line-height: 2;">&nbsp;</p>
<h3 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 24px;">Step 4: Display a PDF Document</span></strong></h3>
<p style="line-height: 2;">&nbsp;</p>
<p style="text-align: justify; line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">1. Copy a PDF document into the <strong>assets</strong> directory of your Android project. For example, import the file <em><strong>Quick Start Guide.pdf</strong></em> to the path <strong>src/main/assets</strong>.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;" data-src="structure.png"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/644096084db9b.png" alt="Display a PDF Document" width="600" height="641" /></span></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">2. Create a new <strong>Empty Activity</strong> under your package, and set the activity name to <strong>MainActivity</strong>.</span></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/644096c0d25e0.png" alt="Empty Activity" width="600" height="609" /></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">Android Studio will automatically generate a source file called <strong>MainActivity.java</strong> and a layout file called <strong>activity_main.xml</strong>.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">The source file:</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;" data-src="activity_java.png"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/6440972e336e1.png" alt="Activity Java" width="600" height="212" /></span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">The layout file:</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;" data-src="layout.png"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/6440976164559.png" alt="Layout" width="600" height="478" /></span></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="text-align: justify; line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">3. Create a </span><em style="text-align: justify;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">CPDFReaderView</code></span></span></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp;in your </span><em style="font-family: arial, helvetica, sans-serif; font-size: 18px;"><strong>activity_main.xml</strong></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;"> to display the contents of the PDF document:</span></p>
<p style="text-align: justify; line-height: 2;">&nbsp;</p>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oahe29ahgggi11usnfvl.png)

<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">Get the&nbsp;</span><em><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">CPDFReaderView</code></span></span></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp;from the layout or create a&nbsp;</span><em><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">CPDFReaderView</code></span></span></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">&nbsp;directly in the code in the corresponding </span><em style="font-family: arial, helvetica, sans-serif; font-size: 18px;"><strong>MainActivity.java</strong></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;"> file:</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<pre class="language-java"><code>// Your MainActivity.java file
package com.compdfkit.pdfviewer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.compdfkit.ui.reader.CPDFReaderView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get CPDFReaderView from xml.
        CPDFReaderView readerView = findViewById(R.id.readerview);
        // Code to create CPDFReaderView.
        // CPDFDocument readerView = new CPDFReaderView(content);
    }
}</code></pre>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">4. Open the document. This is a time-consuming process, so it needs to be executed in a <strong>sub-thread</strong>. After the document is opened successfully, the UI that renders the PDF is initiated:</span></p>
<p style="line-height: 2;">&nbsp;</p>
<pre class="language-java"><code>// Your MainActivity.java file
... //imports
public class MainActivity extends AppCompatActivity {
    // Copy the PDF file from the assets folder to the cache folder.
    private void copyPdfFromAssetsToCache(String fileName) {
        try {
            InputStream inputStream = getAssets().open(fileName);
            File outputFile = new File(getCacheDir(), fileName);
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CPDFReaderView readerView = findViewById(R.id.readerview);
        // Code to create CPDFReaderView.
        // CPDFDocument readerView = new CPDFReaderView(content);
        //Create a document object.
        CPDFDocument document = new CPDFDocument(this);
        
        new Thread(() -&gt; {
            String fileName = "Quick Start Guide.pdf";
            copyPdfFromAssetsToCache(fileName);
            File file = new File(getCacheDir(), fileName);
            String filePath = file.getAbsolutePath();
            //Open document.
            CPDFDocument.PDFDocumentError error = document.open(filePath);
            if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
                //The document is encrypted and requires a password to open.
                error = document.open(filePath, "password");
            }
            if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
                //The document is opened successfully and data can be parsed and manipulated.
            } else {
                //The PDF file failed to open. You can refer to the API file for specific error
            }
        }).start();
    }
}</code></pre>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">5. Set the basic properties of </span><em style="text-align: justify;"><span style="box-sizing: border-box; border: 0px solid #e5e7eb; margin: 0px; color: #6a6f77; font-size: 16px; orphans: 4; text-align: start; white-space: pre-wrap; word-spacing: 1px; background-color: #f8faff;"><span class="md-pair-s" style="box-sizing: border-box; border: 0px solid #e5e7eb; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px;" spellcheck="false"><code style="box-sizing: border-box; border: 1px solid #e7eaed; --tw-ring-inset: var(--tw-empty, ); --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; margin: 0px; font-size: 1em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; vertical-align: initial; background-color: #f3f4f4; padding: 0px 2px;">CPDFReaderView</code></span></span></em><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">:</span></p>
<pre class="language-java"><code>// Your MainActivity.java file
... // imports
public class MainActivity extends AppCompatActivity {
    // Create a handler to run the code on the main thread.
    private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
...
    if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
        // The document is opened successfully and data can be parsed and manipulated.
        mainThreadHandler.post(() -&gt; {
            // Set the document content for UI.
            readerView.setPDFDocument(document);
        });
    } else {
        // The PDF file failed to open. You can refer to the API file for specific error
    }
...
}</code></pre>

<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">6. Your code may resemble the following at this stage: </span></p>
<pre class="language-java"><code>// Your MainActivity.java file

... // imports

public class MainActivity extends AppCompatActivity {
    // Create a handler to run the code on the main thread.
    private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
    // Copy the PDF file from the assets folder to the cache folder.
    private void copyPdfFromAssetsToCache(String fileName) {
        try {
            InputStream inputStream = getAssets().open(fileName);
            File outputFile = new File(getCacheDir(), fileName);
            FileOutputStream outputStream = new FileOutputStream(outputFile);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CPDFReaderView readerView = findViewById(R.id.readerview);

        //Create a document object.
        CPDFDocument document = new CPDFDocument(this);

        new Thread(() -&gt; {
            String fileName = "Quick Start Guide.pdf";
            copyPdfFromAssetsToCache(fileName);

            File file = new File(getCacheDir(), fileName);
            String filePath = file.getAbsolutePath();

            //Open document.
            CPDFDocument.PDFDocumentError error = document.open(filePath);
            if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
                //The document is encrypted and requires a password to open.
                error = document.open(filePath, "password");
            }

            if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
                //The document is opened successfully and data can be parsed and manipulated.
                mainThreadHandler.post(() -&gt; {
                    //Set the document to the reader view.
                    readerView.setPDFDocument(document);
                });
            } else {
                //The PDF file failed to open. You can refer to the API file for specific error
            }
        }).start();
    }
}</code></pre>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc20zk6blm5n1xzunmlj.png)

<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">7. Run the application.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>

<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;" data-src="screen_shot.png"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://compdf.oss-us-east-1.aliyuncs.com/blog/files/2023-04/644099ba86073.png" alt="demo" width="600" height="1286" /></span></p>

<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">Now, with the help of ComPDFKit, you can get a simple application to display a PDF file.</span></p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;">&nbsp;</p>
<h2 style="line-height: 2;"><strong><span style="font-family: arial, helvetica, sans-serif; font-size: 36px;">Troubleshooting</span></strong></h2>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">1. Unable to Open the PDF Files </span></p>
<p style="line-height: 2;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">The license we provide to you is tied to your application ID, so ensure that the license acquired matches your application ID.</span></p>
<p style="line-height: 2;">&nbsp;</p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">2. Other Problems</span></p>
<p style="line-height: 2; text-align: justify;"><span style="font-family: arial, helvetica, sans-serif; font-size: 18px;">If you meet some other problems when integrating our ComPDFKit PDF SDK for Android, feel free to <span style="color: #3598db;"><a style="color: #3598db;" href="https://www.compdf.com/contact-us" target="_blank" rel="noopener">contact</a></span> ComPDFKit team.</span></p>