Android PDF 操作 - AndroidPdfViewer 弹出框显示 PDF

一、弹出框显示 PDF

1、简单显示
(1)Dialog
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfv_container"
        android:layout_width="300dp"
        android:layout_height="300dp" />
</LinearLayout>
(2)Test
java 复制代码
File pdfFile = new File(getFilesDir(), "test.pdf");

InputStream inputStream;
try {
    inputStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();

    Toast.makeText(this, "PDF 文件不存在", Toast.LENGTH_SHORT).show();

    return;
}

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_simple_show_pdf);

PDFView pdfvContainer = dialog.findViewById(R.id.pdfv_container);

pdfvContainer.fromStream(inputStream)
        .enableSwipe(true) // 启用手势滑动翻页
        .swipeHorizontal(false) // 设置滑动方向为垂直滑动
        .enableDoubletap(true) // 启用双击缩放
        .defaultPage(0) // 设置初始显示的页面
        .load();

dialog.show();
2、标准显示
(1)Dialog
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="title"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="message"
        android:textSize="20dp" />

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfv_container"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <android.widget.Button
            android:id="@+id/btn_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="confirm"
            android:textColor="@color/black" />
    </LinearLayout>
</LinearLayout>
(2)Test
java 复制代码
File pdfFile = new File(getFilesDir(), "test.pdf");

InputStream inputStream;
try {
    inputStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();

    Toast.makeText(this, "PDF 文件不存在", Toast.LENGTH_SHORT).show();

    return;
}

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_show_pdf);
dialog.setCancelable(false);

TextView tvTitle = dialog.findViewById(R.id.tv_title);
TextView tvMessage = dialog.findViewById(R.id.tv_message);
PDFView pdfvContainer = dialog.findViewById(R.id.pdfv_container);
Button btnConfirm = dialog.findViewById(R.id.btn_confirm);

tvTitle.setText("系统提示");
tvMessage.setText("请详细阅读文档内容");

pdfvContainer.fromStream(inputStream)
        .enableSwipe(true) // 启用手势滑动翻页
        .swipeHorizontal(false) // 设置滑动方向为垂直滑动
        .enableDoubletap(true) // 启用双击缩放
        .defaultPage(0) // 设置初始显示的页面
        .load();

btnConfirm.setText("确定");
btnConfirm.setOnClickListener(v -> {
    dialog.dismiss();
});

dialog.show();

二、弹出框显示 PDF 封装

1、简单显示
(1)Dialog
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfv_container"
        android:layout_width="300dp"
        android:layout_height="300dp" />
</LinearLayout>
java 复制代码
public static Dialog createSimpleShowPdfDialog(Context context, byte[] data) {
    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.dialog_simple_show_pdf);

    PDFView pdfvContainer = dialog.findViewById(R.id.pdfv_container);

    pdfvContainer.fromBytes(data)
            .enableSwipe(true) // 启用手势滑动翻页
            .swipeHorizontal(false) // 设置滑动方向为垂直滑动
            .enableDoubletap(true) // 启用双击缩放
            .defaultPage(0) // 设置初始显示的页面
            .load();

    return dialog;
}
(2)Test
java 复制代码
File pdfFile = new File(getFilesDir(), "test.pdf");

InputStream inputStream;
try {
    inputStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();

    Toast.makeText(this, "PDF 文件不存在", Toast.LENGTH_SHORT).show();

    return;
}

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
try {
    while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
} catch (IOException e) {
    e.printStackTrace();

    Toast.makeText(this, "PDF 文件读取失败", Toast.LENGTH_SHORT).show();

    return;
}

byte[] bytes = buffer.toByteArray();

Dialog dialog = DialogManager.createSimpleShowPdfDialog(this, bytes);

dialog.show();
2、标准显示
(1)Dialog
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="title"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="message"
        android:textSize="20dp" />

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfv_container"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <android.widget.Button
            android:id="@+id/btn_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="confirm"
            android:textColor="@color/black" />
    </LinearLayout>
</LinearLayout>
java 复制代码
public interface ConfirmObserver {
    void onConfirm();
}
java 复制代码
public static Dialog createShowPdfDialog(Context context,
                                            String title,
                                            String message,
                                            byte[] data,
                                            String confirmText, ConfirmObserver confirmObserver) {
    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.dialog_show_pdf);
    dialog.setCancelable(false);

    TextView tvTitle = dialog.findViewById(R.id.tv_title);
    TextView tvMessage = dialog.findViewById(R.id.tv_message);
    PDFView pdfvContainer = dialog.findViewById(R.id.pdfv_container);
    Button btnConfirm = dialog.findViewById(R.id.btn_confirm);

    tvTitle.setText(title);
    tvMessage.setText(message);

    pdfvContainer.fromBytes(data)
            .enableSwipe(true) // 启用手势滑动翻页
            .swipeHorizontal(false) // 设置滑动方向为垂直滑动
            .enableDoubletap(true) // 启用双击缩放
            .defaultPage(0) // 设置初始显示的页面
            .load();

    btnConfirm.setText(confirmText);
    btnConfirm.setOnClickListener(v -> {
        if (confirmObserver != null) {
            confirmObserver.onConfirm();
        }
        dialog.dismiss();
    });

    return dialog;
}
(2)Test
java 复制代码
Button btnOpenPdf = findViewById(R.id.btn_open_pdf);

btnOpenPdf.setOnClickListener(view -> {
    File pdfFile = new File(getFilesDir(), "test.pdf");

    InputStream inputStream;
    try {
        inputStream = new FileInputStream(pdfFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();

        Toast.makeText(this, "PDF 文件不存在", Toast.LENGTH_SHORT).show();

        return;
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[1024];
    try {
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
    } catch (IOException e) {
        e.printStackTrace();

        Toast.makeText(this, "PDF 文件读取失败", Toast.LENGTH_SHORT).show();

        return;
    }

    byte[] bytes = buffer.toByteArray();

    Dialog dialog = DialogManager.createShowPdfDialog(this,
            "系统提示",
            "请详细阅读文档内容",
            bytes,
            "确定",
            null);

    dialog.show();
});
相关推荐
摇滚侠15 小时前
macbook shell 客户端推荐 Electerm macbook 版本下载链接
java·开发语言
程序员布吉岛15 小时前
Java 后端定时任务怎么选:@Scheduled、Quartz 还是 XXL-Job?(对比 + 避坑 + 选型)
java·开发语言
知无不研16 小时前
lambda表达式的原理和由来
java·开发语言·c++·lambda表达式
逍遥德16 小时前
Sring事务详解之02.如何使用编程式事务?
java·服务器·数据库·后端·sql·spring
笨蛋不要掉眼泪16 小时前
Redis哨兵机制全解析:原理、配置与实战故障转移演示
java·数据库·redis·缓存·bootstrap
Coder_Boy_16 小时前
基于SpringAI的在线考试系统-整体架构优化设计方案
java·数据库·人工智能·spring boot·架构·ddd
草履虫建模1 天前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
Rainman博1 天前
WMS-窗口relayout&FinishDrawing
android
qq_297574671 天前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚1 天前
MyBatis插件原理及Spring集成
java·spring·mybatis