1. 使用第三方库 PdfRenderer(实用于 Android 5.0 及以上)
PdfRenderer 是 Android 体系自带的一个用于渲染 PDF 文件的 API,它允许你将 PDF 页面渲染到 Bitmap 上,然后在 ImageView 中表现。
步骤:
添加权限:在 AndroidManifest.xml 中添加读取外部存储的权限(如果 PDF 文件存放在外部存储)。
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
复制代码
- 结构文件:创建一个简朴的结构文件 activity_main.xml,包含一个 ImageView 用于表现 PDF 页面。
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/pdfImageView"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
复制代码
- Java 代码实现:在 MainActivity.java 中实现 PDF 预览功能。
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.pdf.PdfRenderer;
- import android.os.Bundle;
- import android.os.ParcelFileDescriptor;
- import android.widget.ImageView;
- import androidx.appcompat.app.AppCompatActivity;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class MainActivity extends AppCompatActivity {
- private ImageView pdfImageView;
- private PdfRenderer pdfRenderer;
- private PdfRenderer.Page currentPage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- pdfImageView = findViewById(R.id.pdfImageView);
- try {
- openPdfFromAssets();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void openPdfFromAssets() throws IOException {
- // 从 assets 文件夹中复制 PDF 文件到应用的缓存目录
- AssetManager assetManager = getAssets();
- InputStream inputStream = assetManager.open("example.pdf");
- File outputFile = new File(getCacheDir(), "example.pdf");
- FileOutputStream outputStream = new FileOutputStream(outputFile);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = inputStream.read(buffer)) > 0) {
- outputStream.write(buffer, 0, length);
- }
- inputStream.close();
- outputStream.close();
- // 打开 PDF 文件
- ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(outputFile, ParcelFileDescriptor.MODE_READ_ONLY);
- pdfRenderer = new PdfRenderer(fileDescriptor);
- // 显示第一页
- showPage(0);
- }
- private void showPage(int index) {
- if (currentPage != null) {
- currentPage.close();
- }
- currentPage = pdfRenderer.openPage(index);
- Bitmap bitmap = Bitmap.createBitmap(currentPage.getWidth(), currentPage.getHeight(), Bitmap.Config.ARGB_8888);
- currentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
- pdfImageView.setImageBitmap(bitmap);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (currentPage != null) {
- currentPage.close();
- }
- if (pdfRenderer != null) {
- pdfRenderer.close();
- }
- }
- }
复制代码 2. 使用第三方库 MuPDF
MuPDF 是一个轻量级的 PDF 查看器库,支持多种文档格式,并且提供了丰富的功能。
步骤:
- 添加依赖:在 build.gradle 文件中添加 MuPDF 库的依赖。
- implementation 'com.artifex.mupdf:mupdf-android:1.19.0'
复制代码
- 结构文件:创建一个简朴的结构文件 activity_main.xml,包含一个 MuPDFReaderView 用于表现 PDF 文件。
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <com.artifex.mupdf.fitz.android.AndroidMuPDFReaderView
- android:id="@+id/pdfReaderView"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
复制代码
- Java 代码实现:在 MainActivity.java 中实现 PDF 预览功能。
- import android.os.Bundle;
- import android.os.Environment;
- import androidx.appcompat.app.AppCompatActivity;
- import com.artifex.mupdf.fitz.Document;
- import com.artifex.mupdf.fitz.MuPDFCore;
- import com.artifex.mupdf.fitz.android.AndroidMuPDFReaderView;
- import java.io.File;
- public class MainActivity extends AppCompatActivity {
- private AndroidMuPDFReaderView pdfReaderView;
- private MuPDFCore muPDFCore;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- pdfReaderView = findViewById(R.id.pdfReaderView);
- try {
- // 打开 PDF 文件
- File pdfFile = new File(Environment.getExternalStorageDirectory(), "example.pdf");
- muPDFCore = new MuPDFCore(this, pdfFile.getAbsolutePath());
- // 设置 PDF 阅读器的核心
- pdfReaderView.setCore(muPDFCore);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (muPDFCore != null) {
- muPDFCore.destroy();
- }
- }
- }
复制代码 3. 使用第三方库 PdfiumAndroid
PdfiumAndroid 是一个基于 Pdfium 库的 Android 封装,用于在 Android 应用中渲染 PDF 文件。
步骤:
- 添加依赖:在 build.gradle 文件中添加 PdfiumAndroid 库的依赖。
- implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
复制代码
- 结构文件:创建一个简朴的结构文件 activity_main.xml,包含一个 PDFView 用于表现 PDF 文件。
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <com.github.barteksc.pdfviewer.PDFView
- android:id="@+id/pdfView"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
复制代码
- Java 代码实现:在 MainActivity.java 中实现 PDF 预览功能。
- import android.os.Bundle;
- import android.os.Environment;
- import androidx.appcompat.app.AppCompatActivity;
- import com.github.barteksc.pdfviewer.PDFView;
- import java.io.File;
- public class MainActivity extends AppCompatActivity {
- private PDFView pdfView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- pdfView = findViewById(R.id.pdfView);
- // 打开 PDF 文件
- File pdfFile = new File(Environment.getExternalStorageDirectory(), "example.pdf");
- pdfView.fromFile(pdfFile)
- .defaultPage(0)
- .enableSwipe(true)
- .swipeHorizontal(false)
- .load();
- }
- }
复制代码 以上三种方法都可以在 Android 应用中实现 PDF 预览功能
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |