This tutorial demonstrates how to use Debenu Quick PDF Library to create an Android app using Android Studio. If you haven’t already downloaded the Android trial then you can do that from the trial here.

Setup Android Studio project with Debenu Quick PDF Library

  1. Open Android Studio. If Quick Start window is displayed then click on “Start a new Android Project” or if not displayed go to “File > New > New Project” and then you will be shown the below window.
    step1_dqpl_android_setup
  2. Give your application a name, modify the company domain if necessary and choose the project location. Click the “Next” button.
    step2_dqpl_android_setup
  3. The “Target Android Devices” window will be shown, choose which platforms you want to target. In this case we will target “Phone and Tablet”. The Minimum SDK required for Debenu Quick PDF Library is Android API 15. Click the “Next” button.
    step3_dqpl_android_setup
  4. The wizard will now ask you to “Add an activity to Mobile”, for this tutorial we will choose the “Blank Activity” option, now click on the “Next” button.
    step4_dqpl_android_setup
  5. You can change the name of launcher Activity of your application here, however, it is optional and you can just use the default name if you wish i.e. MainActivity. Click on the “Finish” button. The project will now be created.
    step5_dqpl_android_setup
  6. After clicking on the “Finish” button the Android application will be created and the application hierarchy will be shown as below.
    step6_dqpl_android_setup
  7. Now select “Project” option from the drop down menu in top corner of the left pane. See screenshot below for precise location.
    step7_dqpl_android_setup
  8. The project hierarchy will now be changed as below.
  9. Now lets explore the project by going to “your_project_name > app > src > main”.
    step9_dqpl_android_setup
  10. Right-click on “main” directory and then click on the “New” option from popup menu then select “Directory” option.
    step10_dqpl_android_setup
  11. Create a directory with name of “jniLibs\armeabi”.
    step11_dqpl_android_setup
  12. Two new directories will be created as shown below.
    step12_dqpl_android_setup
  13. If you haven’t already, download Debenu Quick PDF Library for Android, and then go to the folder where the library has been downloaded to (in this case we have placed the ZIP file for the library here: E:\download). Right-click on the “debenu_quick_pdf_library_android.zip” file and extract its contents to a new folder as shown below.
    step13a_dqpl_android_setup
    step13b_dqpl_android_setup
  14. Now go to “debenu_quick_pdf_library_android > Android > ARMv7” then copy the “libDebenuPDFLibraryAndroidArm1115.so” file. This file is required in the Android application because it contains the actual implementation of all the native methods which are declared in the JNI Classes (for JNI classes please check with Step 18).
    step14_dqpl_android_setup
  15. Paste the “libDebenuPDFLibraryAndroidArm1115.so” file into the “jniLibs\armeabi” directory as shown below.
    step15_dqpl_android_setup
  16. Now right-click on the “java” directory select the “New” option then select “Package” as shown in the following screen.
    step16_dqpl_android_setup
  17. Now enter the package name as “com.debenu.pdflibray”, the same as shown below and click on the “OK” button to create the package under the “java” directory. Note: the package name should be the same as shown in following screen so the application can map the native methods from the .so file according to this package name. This package contains all of the required JNI files and the JNI files are the files that act as an intermediator for calling native methods.
    step17_dqpl_android_setup
  18. Now go back to the location where you downloaded the Debenu Quick PDF Library for Android ZIP file as discussed in step 14. Navigate to the “java” folder located here “debenu_quick_pdf_library_android\Android\Import\Java” and copy all files located in this directory.
    step18_dqpl_android_setup
  19. Back in Android Studio, paste the files in the newly created package “com.debenu.pdflibrary” as shown below.
    step19_dqpl_android_setup
  20. Now under the “app” directory open the build.gradle file.
    step20_dqpl_android_setup
  21. Add the highlighted lines of code in the build.gradle file. The script will create the .jar file and make it available to the Android application at run-time.
    step21_dqpl_android_setup

     

    The code for the two areas in the above screenshot that need to be updated are here:

    task nativeLibsToJar(type: Zip) {
        destinationDir file("$buildDir/native-libs")
        baseName 'native-libs'
        extension 'jar'
        from fileTree(dir: 'libs', include: '**/*.so')
        into 'lib/'
    }
     
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn(nativeLibsToJar)
    }

    tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
    }

    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
  22. Good job! Now you are ready to start using Debenu Quick PDF Library for Android to start adding PDF functionality to your app.

How to use Debenu Quick PDF Library for the first time

Now that we’ve setup Debenu Quick PDF Library in an Android Studio project lets do something with the library to demonstrate that it is working.

  1. In the MainActivity.java file for your app (app > src > main > java > com.debenu > name of your app > MainActivity) copy and paste this code in. Replacing everything except for the package line at the very top.
    package com.debenu.debenutestapp; // Don't replace your version of this, it needs to match your project
     
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Environment;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    import com.debenu.pdflibrary.FlatAPI1115;
    import java.io.File;
    import java.io.IOException;
     
    public class MainActivity extends Activity {
     
        static {
            // Load Debenu Quick PDF Library
            System.loadLibrary("DebenuPDFLibraryAndroidArm1115");
        }
     
        private int mDQPL = 0;
        private int mUnlocked = 0;
        private String mPath;
     
        private Button btnCreatePdf = null;
        private ImageView imgText = null;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            try {
                // Fetch id of the imageView
                imgText = (ImageView) findViewById(R.id.imgText);
                btnCreatePdf = (Button) findViewById(R.id.btnCreatePDF);
     
                // Initialize Debenu Quick PDF Library
                openDQPL(this);
     
                btnCreatePdf.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
     
                        // Set the bitmap path
                        mPath = Environment.getExternalStorageDirectory().toString();
                        mPath += File.separator;
                        mPath += "test.bmp";
     
                        // Time to create the PDF and draw
                        // some text on it
     
                        // When Debenu Quick PDF Library is initialized
                        // a blank document is automatically added into memory
                        // and page 1 is selected by default
     
                        // Specify top left corner of page for
                        // where 0,0 coordinates should start from
                        FlatAPI1115.DPLSetOrigin(mDQPL, 1);
     
                        // Text to be drawn on the page
                        String txt = "Hello World...!!!";
     
                        // Draw the text onto the page, left edge of
                        // text will be drawn 25 points from left of page
                        // and 25 points down from top of page
                        FlatAPI1115.DPLDrawTextA(mDQPL, 25, 25, txt);
                        showPage(1);
                    }
                });
     
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(this, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
                return;
            }
        }
     
        /**
         * Create the Debenu Quick PDF Library instance and
         * unlock it.
         *
         * If you don't unlock then all of the functions will
         * appear to work except the functions that have an
         * output such as SaveToFile, RenderPageToFile, GetPageText, etc.
         */
        private void openDQPL(Context context) throws IOException {
            try {
                //mPath = context.getCacheDir().toString();
                //mPath += "/test.bmp";
                mDQPL = FlatAPI1115.DPLCreateLibrary();
                String key = "...license_key_goes_here..."; // Insert your trial or commercial license key here
                mUnlocked = FlatAPI1115.DPLUnlockKeyA(mDQPL, key);
                Log.i("DQPL", "mUnlocked: " + mUnlocked);
                if (mUnlocked == 0)
                    Log.e("ERROR", "Could not open DQPL");
     
            }   catch (Exception e) {
                Log.e("ERROR", "Could not open DQPL", e);
            }
        }
     
        /**
         * Show the page created at index
         * @param index
         */
        private void showPage(int index) {
            if (mUnlocked == 0){
                return;
            }
            try {
                FlatAPI1115.DPLRenderPageToFileA(mDQPL, 72, index, 0, mPath);
                Bitmap bitmap = BitmapFactory.decodeFile(mPath);
                imgText.setImageBitmap(bitmap);
            }   catch (Exception e) {
                Log.e("ERROR", "Could not render page",e);
            }
        }
     
    }

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Environment;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    import com.debenu.pdflibrary.FlatAPI1115;
    import java.io.File;
    import java.io.IOException;

    public class MainActivity extends Activity {

    static {
    // Load Debenu Quick PDF Library
    System.loadLibrary("DebenuPDFLibraryAndroidArm1115");
    }

    private int mDQPL = 0;
    private int mUnlocked = 0;
    private String mPath;

    private Button btnCreatePdf = null;
    private ImageView imgText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
    // Fetch id of the imageView
    imgText = (ImageView) findViewById(R.id.imgText);
    btnCreatePdf = (Button) findViewById(R.id.btnCreatePDF);

    // Initialize Debenu Quick PDF Library
    openDQPL(this);

    btnCreatePdf.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    // Set the bitmap path
    mPath = Environment.getExternalStorageDirectory().toString();
    mPath += File.separator;
    mPath += "test.bmp";

    // Time to create the PDF and draw
    // some text on it

    // When Debenu Quick PDF Library is initialized
    // a blank document is automatically added into memory
    // and page 1 is selected by default

    // Specify top left corner of page for
    // where 0,0 coordinates should start from
    FlatAPI1115.DPLSetOrigin(mDQPL, 1);

    // Text to be drawn on the page
    String txt = "Hello World...!!!";

    // Draw the text onto the page, left edge of
    // text will be drawn 25 points from left of page
    // and 25 points down from top of page
    FlatAPI1115.DPLDrawTextA(mDQPL, 25, 25, txt);
    showPage(1);
    }
    });

    } catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(this, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
    return;
    }
    }

    /**
    * Create the Debenu Quick PDF Library instance and
    * unlock it.
    *
    * If you don't unlock then all of the functions will
    * appear to work except the functions that have an
    * output such as SaveToFile, RenderPageToFile, GetPageText, etc.
    */
    private void openDQPL(Context context) throws IOException {
    try {
    //mPath = context.getCacheDir().toString();
    //mPath += "/test.bmp";
    mDQPL = FlatAPI1115.DPLCreateLibrary();
    String key = "...license_key_goes_here..."; // Insert your trial or commercial license key here
    mUnlocked = FlatAPI1115.DPLUnlockKeyA(mDQPL, key);
    Log.i("DQPL", "mUnlocked: " + mUnlocked);
    if (mUnlocked == 0)
    Log.e("ERROR", "Could not open DQPL");

    } catch (Exception e) {
    Log.e("ERROR", "Could not open DQPL", e);
    }
    }

    /**
    * Show the page created at index
    * @param index
    */
    private void showPage(int index) {
    if (mUnlocked == 0){
    return;
    }
    try {
    FlatAPI1115.DPLRenderPageToFileA(mDQPL, 72, index, 0, mPath);
    Bitmap bitmap = BitmapFactory.decodeFile(mPath);
    imgText.setImageBitmap(bitmap);
    } catch (Exception e) {
    Log.e("ERROR", "Could not render page",e);
    }
    }

    }

  2. Now we need to update visual layout to include a button and an imageView. Replace the contents of the activity_main.xml file (app > src > main > res > layout > activity_main.xml) with the code from below:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
     
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create PDF"
            android:id="@+id/btnCreatePDF"/>
     
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imgText"
            android:layout_below="@+id/btnCreatePDF"/>
     
    </RelativeLayout>

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Create PDF"
    android:id="@+id/btnCreatePDF"/>

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imgText"
    android:layout_below="@+id/btnCreatePDF"/>

    </RelativeLayout>

  3. Now we need to update the Android manifest file (app > src > main > AndroidManifest.xml) to give the app some read/write privileges. Add this after the section:

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  4. Finally, we’re ready to test our new app. If you aren’t familiar with using the Android Studio Emulator, then we have a knowledge base article which provides some tips on using the built-in Android Studio Emulator. You can of course also test this directly on a device. Once the emulator has fired up and the homescreen is being seen then you can run your app.
  5. That’s all! You should be up and running now, if you have any questions please .