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();
});
相关推荐
谢白羽1 分钟前
vllm实践
android·vllm
daidaidaiyu19 分钟前
Spring IOC 源码学习 事务相关的 BeanDefinition 解析过程 (XML)
java·spring
电子云与长程纠缠42 分钟前
Godot学习03 - 实例化、层级访问、Export
android·学习·godot
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 基于Android的便民系统的设计与实现为例,包含答辩的问题和答案
android
鬼蛟1 小时前
Spring————事务
android·java·spring
西门吹-禅2 小时前
【sap fiori cds up error】
java·服务器·sap cap cds
敲代码的嘎仔2 小时前
Java后端面试——SSM框架面试题
java·面试·职场和发展·mybatis·ssm·springboot·八股
大傻^2 小时前
Spring AI Alibaba RAG实战:基于向量存储的检索增强生成
java·人工智能·spring
大傻^2 小时前
Spring AI Alibaba 快速入门:基于通义千问的AI应用开发环境搭建
java·人工智能·后端·spring·springai·springaialibaba
伯恩bourne3 小时前
Google Guava:Java 核心工具库的卓越之选
java·开发语言·guava