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();
});
相关推荐
用户83352502537856 小时前
RecyclerView设置边缘渐变失效
android
2501_938791226 小时前
服务器恶意进程排查:从 top 命令定位到病毒文件删除的实战步骤
java·linux·服务器
不见长安在6 小时前
HashMap的源码学习
java·hashmap
星光一影6 小时前
基于Jdk17+SpringBoot3AI智慧教育平台,告别低效学习,AI精准导学 + 新架构稳跑
java·学习·mysql
SimonKing6 小时前
Spring Boot全局异常处理的背后的故事
java·后端·程序员
专家大圣6 小时前
5分钟启动标准化安卓环境:Docker-Android让模拟器配置不再踩坑
android·网络·docker·容器·内网穿透
ac.char6 小时前
编辑 JAR 包内嵌套的 TXT 文件(Vim 操作)
java·pycharm·vim·jar
我想进大厂6 小时前
Mybatis中# 和 $的区别
java·sql·tomcat
命运之光6 小时前
让 Jar 程序在云服务器上持续后台运行,不受终端界面关闭的影响
java·服务器·jar