您的位置:首页 > 移动开发 > Android开发

Foxit Mobile PDF SDK for Android_添加Text Search功能(3)

2016-10-31 11:40 495 查看
这段时间比较忙,今天有空,再来一篇。哈哈…

在上一篇文章(Foxit Mobile PDF SDK for Android_渲染PDF文档(2))中用几行代码实现了PDF的渲染和显示,并且带有翻页和缩放功能。不得不说Foxit这款SDK真的很好用,程序猿只需要很少量的代码就可以完成复杂而繁琐的PDF解析,并显示PDF。

PS:为了方便大家快速下载,这里还是把下载试用的链接放上来:http://www.foxitsdk.com/products/mobile-pdf-sdk/request-trial/

今天我们来学习一下如何在上一篇创建的RenderPDF工程中添加Text Search功能。

Note: Foxit这款SDK提供了一个UI Extensions组件,该组件包含了内置的UI实现模块,我们可以直接拿来用,Foxit帮我们把界面都写好了,省了我们很多很多工作哦。而且他们把这部分的代码开源了,就是说如果我们想对内置的UI进行修改,可以直接在提供的源代码中进行修改,是不是很方便啊。

补充知识:

在下载包的libs文件夹下存放了如下的一些文件,之前忘记介绍这些文件是什么了,现在补上哈。



librdk.so (libs/armeabi-v7a, libs/arm64-v8a, libs/x86): 是Foxit Mobile PDF SDK的核心库,也可以说是低层库,主要被FoxitRDK.jar中的Java APIs调用。

FoxitRDK.jar: Java平台调用的接口,包含了Foxit Mobile PDF SDK中的所有Java APIs.

FoxitRDKUIExtensions.aar: 该aar文件是由“uiextensions_src”工程编译得到的。它包括了FoxitRDK.jar,内置的UI实现,和资源文件。

uiextensions_src 工程: 是一个开源代码库工程,包括了一些现成的UI实现模块,可以帮助开发者在Android应用程序中快速集成一个功能丰富的PDF阅读器。另外,开发者可以根据自己应用程序的需要对内置的UI进行自定义。

总结来说:如果不需要对foxit提供内置UI进行修改的话,我们可以直接使用开源工程编译生成的FoxitRDKUIExtensions.aar.如果需要自定义UI或者添加其他tool的话,就需要导入uiextensions_src源代码工程了。

补充的知识就到这里了,下面我们看看怎么在RenderPDF工程中添加Text Search 功能。

PS: Text Search相关的功能已经在UI Extensions组件中实现,所以我们也只需要很少量的代码就能完成该功能,不行?接着往下看。

为了方便查看效果,我在界面上添加一个Button按钮,通过Button点击事件触发Text Search功能。具体实现步骤如下:

1. 在主界面上面添加一个Button和为Layout添加一个id

在“app/src/main/res/layout/activity_main.xml”中添加如下代码:

<?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.test.renderpdf.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"/>

<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_alignParentRight="true"
android:onClick="Search_Click"/>
</RelativeLayout>


2. 在”MainActivity.java”中添加代码

导入下面的头文件

import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.foxit.uiextensions.UIExtensionsManager;
import com.foxit.uiextensions.modules.SearchModule;
import com.foxit.uiextensions.modules.SearchView;


将system theme设置成”No Title” 模式,以及全屏显示

// Turn off the title at the top of the screen.
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the window to Fullscreen.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


PS: 之所以要设置成“No Title”和全屏模式,是因为在UI Extensions组件中对top toolbar和bottom toolbar做了自定义,如果不设置成这种模式,内置功能的布局会受影响。

实例化一个RelativeLayout对象来获取parent layout

private RelativeLayout parent = null;
...
parent = (RelativeLayout) findViewById(R.id.parentLayout);


实例化一个UIExtensionsManager对象, 并赋给PDFViewCtrl

private UIExtensionsManager uiextensionsManager = null;
...
uiextensionsManager = new UIExtensionsManager (this, parent, pdfViewCtrl);
pdfViewCtrl.setUIExtensionsManager(uiextensionsManager);


实例化一个Button对象,并添加Button点击事件

实例化一个SearchModule对象来加载一个search module,并且实例化一个SearchView对象来start a search.

private Button btn_search = null;
private SearchModule searchModule = null;
private SearchView searchView = null;

...
btn_search = (Button) findViewById(R.id.search_button);
...
// Search button click event
public void Search_Click(View view) {

if (searchModule == null) {
searchModule = new SearchModule(this, parent, pdfViewCtrl);
searchModule.loadModule();
}
searchView = searchModule.getSearchView();
searchView.show();

}


”MainActivity.java”的完整代码如下:

package com.test.renderpdf;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.PDFException;

import com.foxit.uiextensions.UIExtensionsManager;
import com.foxit.uiextensions.modules.SearchModule;
import com.foxit.uiextensions.modules.SearchView;

import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.pdf.PDFDoc;

public class MainActivity extends FragmentActivity {

private PDFViewCtrl pdfViewCtrl = null;
private RelativeLayout parent = null;
private UIExtensionsManager uiextensionsManager = null;
private SearchModule searchModule = null;
private SearchView searchView = null;
private Button btn_search = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String sn = "Jm9uLlPzu31Xeg9DMkMCjTJ21fyDRcmmgsj6r1OpithuPTLmet8RLw==";
String key = "ezJvj90ntWp39JsXIV02hAneK+hiBbdSf8QJK/OtLsbugh81DetHZ2mytsJxCjTVbg84YCcakEMtqn5EtJylgi9le8qSQka+CDSUKaXx7mzfXeKeRXKUKWZo9YvjUdhJOZv4bEUVKon7M6A70Ewt+vi0EJCt8DruGsI5vzUdxjW335VDgrpdXP8WaXFHSunwmFerH/7lgYErxA/hpZK9keeo8lw/yWOVgnAByoIiaqaiQATSgyBMKRANkKp78H+oYWTdj7qNb5UNkq2sKordoCRjCmer5lSKOnEK5jPI7PETlNMi/vkWzSVndHCDNICQH7BaJWA+vwRJqW0E1m1vXXUizXeSuxmu3RtQk4QwYlgmE4cd6z2bl9RjLErHD2pi4y8je8opLN2Tziw+EgDhho1snyDbIWi3ynol2nv/1vt3x7CY7HpMZ1QrJNCv8z9OdGH0zc22RPU8hA47QE4ftH77kQvrjgWu1oZH07dseGrZxj5q+lvbMwZegKAHqXyzj7XDa7084FSQVRwE+vvA+0ZdjRCoJRf32RAmEbx4kF+cwIiDmQliFy+WphFmSKq8NYZA7c+pMRtWZaUHWk1hJ1XI7l175uQZb1WBNAIndRAXy5H3pettxGxIBrJqn4zfE7Kgyf5zvBWMr9+8KY0No1bRz5fHuds2XbiwzrVzV946uQJ1N+NPdX0Zy+u3LqXsskMXKBXseHQAO5g+b/ts4SXzLbSsHWPHCHGgv3wxH09+O2tQ3YH3sb48T6pbIj/oM6PzppRmC9YqIpfKpCZGvAcsYMU3sIHRmZOr5HxKC65VX3oX2CMlTXXJHH0wf9wFqIyxUGwFWL3EvsBo5UR282KDyquz74xgvyhM1F3FZQxD4UJgZBDJvr/mrpziFF3sUuXCjpCwDT2gC25VSng9sm2w5FIh7MjJsRlK+hPCn5tuYZcKzMJHQG9deNA5YwN6/f/KLlhxEJpzeWyiNA2PoV2CDt1hYLJeob+VVtirRNsZI447ym2hCBkjdd7uru3LweWVpPzWTdP3/2a7JkuGPJbhIKYu6LsK5I6ghCfE0vvPR3Ne+aHk97+ytZRJHI8RDGujE5yQ0BIrP0u9ZuTQvPq8xSGJFWHDLsVgxMG0b2I+6jnSBoUo/cJDFT/dNID1anbesGHL73waVHDN0U5cR34Ry97dSBjoD6wgF8UR8roPmbEYNp7GVRrSbenJHQ==";

// Load "librdk.so" library.
System.loadLibrary("rdk");
try {
// initialize the library
Library.init(sn, key);
} catch (PDFException e) {
e.printStackTrace();
return;
}

// Turn off the title at the top of the screen. this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Set the window to Fullscreen. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 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/getting_started_android.pdf";
try {
PDFDoc document = PDFDoc.createFromFilePath(path);
document.load(null);
pdfViewCtrl.setDoc(document);
} catch (Exception e) {
// 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);

btn_search = (Button) findViewById(R.id.search_button);
}

// Search button click event
public void Search_Click(View view) {

if (searchModule == null) {
searchModule = new SearchModule(this, parent, pdfViewCtrl);
searchModule.loadModule();
}
searchView = searchModule.getSearchView();
searchView.show();

}

@Override
protected void onDestroy() {
super.onDestroy();
try {
Library.release();
} catch (PDFException e) {
e.printStackTrace();
}
}

@Override
protected void onResume() {
super.onResume();
if (pdfViewCtrl != null)
pdfViewCtrl.requestLayout();
}
}


3. Run the app:

运行后,点击Search按钮,Foxit内置的Search功能面板就出来了,比如输入“sdk”, 点击Enter后,所有匹配的结果都会被搜索出来,并且在列表中陈列出来,点击任意一个匹配项,都可以跳转到其所在的页面和位置。如下列图所示。







到此,已经完成了Text Search功能的添加,是不是很酷哦。同事告诉我之前为了实现一个这样简单的功能,得花好多时间研究PDF的用法,而且写起来代码量也巨大。他们说我现在学习的这个SDK真的省心省力啊。哈哈。

本文仅限分享学习,菜鸟能力有限,如有错误之处,请大家指正哈。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android mobile sdk pdf