How to Add Form Filling Features to Your Android App with Foxit Mobile PDF SDK
This tutorial picks up where we left off with our “How to Display a PDF on Android with Foxit MobilePDF SDK” tutorial. If you haven’t seen that tutorial yet, take a moment to view it.
Foxit MobilePDF SDK comes with built-in support for features such as annotations, text search, bookmarks, and form filling. These visual features are implemented using Foxit MobilePDF SDK’s API and are shipped in the UI Extensions Component. That may sound complex, but as you’ve likely already seen in our first tutorial, it’s all incredibly simple.
To add support for form filling features, it requires less than ten lines of code. Let’s get started:
- First, prepare a PDF Form file, and add it to the Android device’s or emulator’s SD card.
Then, follow the steps below:
- Step 1) In the “activity_main.xml” file, add an “id” to the layout like below:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.foxit.test_android.MainActivity"
android:id="@+id/parentLayout">
<com.foxit.sdk.PDFViewCtrl
android:id="@+id/pdfviewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical|horizontal"/>
</RelativeLayout>
- Step 2) Update the “activity_main.xml” file as follows:
package com.foxit.test_android;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.PDFException;
import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.uiextensions.UIExtensionsManager;
public
class MainActivity extends FragmentActivity
{
private PDFViewCtrl pdfViewCtrl = null;
private RelativeLayout parent = null;
private UIExtensionsManager uiextensionsManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The value of "sn" can be found in the “rdk_sn.txt”.
// The value of "key" can be found in the “rdk_key.txt”.
String sn = " ";
String key = " ";
// Load "librdk.so"library.
System.loadLibrary("rdk");
try {
// initialize the library.
Library.init(sn, key);
} catch (PDFExceptione) {
e.printStackTrace();
return;
}
// Inflate the view and get a reference to PDFViewCtrl.
setContentView(R.layout.activity_main);
pdfViewCtrl = (PDFViewCtrl) findViewById(R.id.pdfviewer);
// Load a document.
String path = "/mnt/sdcard/FoxitForm.pdf";
try {
PDFDoc document = PDFDoc.createFromFilePath(path);
document.load(null);
pdfViewCtrl.setDoc(document);
} catch (Exceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parent = (RelativeLayout) findViewById(R.id.parentLayout);
// Initialize a UIExtensionsManager object and set it to PDFViewCtrl.
uiextensionsManager = new UIExtensionsManager(this, parent, pdfViewCtrl);
pdfViewCtrl.setUIExtensionsManager(uiextensionsManager);
}
@Override
protected void onDestroy(){
super.onDestroy();
try {
Library.release();
} catch (PDFExceptione) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
if (pdfViewCtrl!= null)
pdfViewCtrl.requestLayout();
}
}
Then rebuild and run the project. A PDF Form file will be displayed.
You should now be able to edit the form fields. If so, congrats! You’ve completed this tutorial.
Summary
The key to adding form filling support is to instantiate a UIExtensionsManager object and set it to PDFViewCtrl. It’s that easy!
Download MobilePDF SDK for free now!