ComPDFKit PDF SDK: 创建Android PDF阅读器

在当今移动优先的世界中,创建 Android 应用程序是企业和开发人员的必备技能。而且,随着处理 PDF 文档的需求不断增加,使用功能强大的ComPDFKit PDF SDK 构建 Android PDF 阅读器和编辑器,能使您的app用户轻松查看和编辑 PDF。

在本w文中,我们将首先探索集成 ComPDFKit PDF SDK 的必要步骤,并使用 ComPDFKit 构建一个 Android PDF 阅读器。

ComPDFKit Android PDF SDK 入门

ComPDFKit 是一个功能强大的 PDF SDK。只需几行 Java 代码即可轻松将 ComPDFKit PDF SDK 嵌入到您的 Android 应用程序中。只需几分钟即可开始。

以下部分介绍了要求、包的结构以及如何使用 ComPDFKit PDF SDK 用 Java 语言开发一个 Android PDF 阅读器。

要求

ComPDFKit PDF SDK 在运行 API 级别 19 或更高版本,或者面向最新稳定的Android 4.4 或更高版本的 Android 设备上受支持。此外,ComPDFKit PDF SDK 需要应用程序启用 Java 8 语言功能才能构建。

Android Studio 3.2 或更新 (支持 AndroidX).

项目规格

minSdkVersion 为 19或更高

compileSdkVersion 为 30或更高

targetSdkVersion 为 30或更高

Android ABI(s): x86, x86_64, armeabi-v7a, arm64-v8a.

Android 包结构

ComPDFKit PDF SDK for Android的软件包包括以下文件:

files.png

libs - 包含 ComPDFKit.aar、ComPDFKit-UI.aar 和 ComPDFKit-Tools.aar 的文件夹。ComPDFKit.aar 是PDF核心API。ComPDFKit-UI.aar 是PDF视图。ComPDFKit-Tools.aar 提供了ComPDFKit SDK的完整实现解决方案和UI组件,可以帮助您更快地实现SDK中的功能。

Examples - 包含Android示例项目的文件夹。

Viewer - 一个基本的PDF阅读器,包括阅读PDF文件、更改主题、书签、搜索文本等功能。

Annotations - 一个具有全类型注释编辑功能的PDF阅读器,包括添加注释、修改注释、注释列表等功能。

ContentEditor - 一个具有文本和图像编辑功能的PDF阅读器,包括修改文本、替换图片等功能。

Forms - 一个具有完整类型表单编辑功能的 PDF 阅读器,包括单选按钮、组合框等。

DocsEditor - 一个具有页面编辑功能的PDF阅读器,包括插入/删除页面、提取页面、重新排序页面等功能。

PDFViewer - 一个集成了以上所有功能的多功能PDF程序。

ComPDFKit_Tools - 一个默认控件库,用于快速构建PDF阅读器的各种功能模块。

Samples - 包含直接调用接口的示例项目的文件夹

api_reference_android - API参考文档。

developer_guide_android.pdf - 开发者指南。

release_notes.txt - 发布信息。

legal.txt - 法律和版权信息。

TestFile - 包含测试文件的文件夹。

用Java语言开发一个Android PDF 阅读器

本节将帮助您快速开始使用 ComPDFKit PDF SDK,并通过分步说明使用 Java 语言开发 Android 应用程序。通过以下步骤,您将获得一个简单的应用程序,可以显示指定PDF文件的内容。

步骤 1: 创建一个新项目

使用 Android Studio 创建一个 Phone & Tablet 项目. 这里我们创建一个 No Activity 项目.

create_project.png

步骤 2: 添加 ComPDFKit PDF SDK 包

首先,我们需要导入ComPDFKit PDF SDK。 复制 ComPDFKit.aar 和 ComPDFKit-UI.aar 到app的 libs 目录中.

libs.png

将以下代码添加到app目录中的build.gradle文件中:


dependencies {
/ComPDFKit SDK/
implementation(fileTree(‘libs’))

}

将ComPDFKit PDF SDK for Android作为项目的依赖项添加进去。在app目录下的build.gradle文件中,将ComPDFKit.aar、ComPDFKit-UI.aar以及相关的支持库添加到dependencies中。为了简化操作,您可以按照以下方式更新依赖项:

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’
}

在AndroidManifest.xml中申请读写权限:

注意: 对于面向Android 6.0或更高版本的应用程序,请确保在运行时检查并请求外部存储的读写权限。

步骤 3: 应用许可证密钥

将许可证添加到主模块的AndroidManifest.xml中:


步骤 4: 显示PDF文档

1. 将PDF文档复制到Android项目的assets目录中。例如,将文件”Quick Start Guide.pdf”导入到路径src/main/assets中。

structure.png

2. 在您的包下创建一个新的 Empty Activity , 并将该Activity的名称设置为 MainActivity.

new_activity.png

Android Studio将自动生成一个名为MainActivity.java的源文件和一个名为activity_main.xml的布局文件。

源文件:

activity_java.png

布局文件:

layout.png

3. 在activity_main.xml中创建一个CPDFReaderView来显示PDF文档的内容:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<com.compdfkit.ui.reader.CPDFReaderView
android:id=”@+id/readerview”
android:layout_width=”match_parent”
android:layout_height=”match_parent” />
</androidx.constraintlayout.widget.ConstraintLayout>

4. 从布局中获取CPDFReaderView或者直接在对应的MainActivity.java文件中的代码中创建一个CPDFReaderView:

// 你的 MainActivity.java 文件
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);
// 从xml获得 CPDFReaderView.
CPDFReaderView readerView = findViewById(R.id.readerview);
// 使用代码创建 CPDFReaderView.
// CPDFDocument readerView = new CPDFReaderView(content);
}
}

5. 打开文档。这是一个耗时的过程,因此需要在子线程中执行。文档成功打开后,会初始化渲染PDF的UI:

// Your MainActivity.java file… //importspublic 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(() -> {
        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();
}}

6. 设置CPDFReaderView的基本属性:

在这个阶段,您的代码可能类似于以下内容:
// 你的 MainActivity.java 文件

… // imports

public class MainActivity extends AppCompatActivity {
// 创建一个 handler 以在主线程上运行代码。
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
// 从 assets 目录复制PDF文件到 cache 目录。
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);

   //创建document 对像.
   CPDFDocument document = new CPDFDocument(this);

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

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

       //打开文档.
       CPDFDocument.PDFDocumentError error = document.open(filePath);
       if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
           //文档已加密,需要密码才能打开
           error = document.open(filePath, "password");
       }

       if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
           //文档已成功打开,并且可以对数据进行解析和操作。
           mainThreadHandler.post(() -> {
               //将此document 设置到reader view中。
               readerView.setPDFDocument(document);
           });
       } else {
           //无法打开PDF文件。您可以参考API文件以了解特定错误
       }
   }).start();

}
}

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<com.compdfkit.ui.reader.CPDFReaderView
android:id=”@+id/readerview”
android:layout_width=”match_parent”
android:layout_height=”match_parent” />

</androidx.constraintlayout.widget.ConstraintLayout>

7. 运行应用程序。

screen_shot.png

现在,借助ComPDFKit的帮助,您获得了一个简单的应用程序来显示PDF文件。

问题排除

1. 无法打开PDF文件

我们向您提供的许可证是与您的应用程序ID绑定的,因此请确保所获取的许可证与您的应用程序ID匹配。

2. 其它问题

如果您在集成 ComPDFKit PDF SDK for Android 时遇到其他问题,请随时联系 ComPDFKit 团队。

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!