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();
});
相关推荐
微露清风19 小时前
系统性学习C++-第十六讲-AVL树实现
java·c++·学习
Hui Baby19 小时前
saga json文件阅读
java·前端·数据库
工具罗某人19 小时前
docker快速部署minio
java·nginx·docker
2501_9418771320 小时前
大规模系统稳定性建设方法论与工程实践分享
java·开发语言
学习在路上ing20 小时前
ollama部署模型
java·ollama
浩瀚地学20 小时前
【Java】面向对象进阶-接口
java·开发语言·经验分享·笔记·学习
沛沛老爹20 小时前
用 Web 开发思维理解 Agent 的三大支柱——Tools + Memory + LLM
java·人工智能·llm·llama·rag
组合缺一20 小时前
灵动如画 —— 初识 Solon Graph Fluent API 编排
java·solon·graph·flow·langgraph·liquor
强子感冒了20 小时前
Java Map学习笔记:HashMap、LinkedHashMap 与 TreeMap 的核心使用与区别
java·笔记·学习
洛枫偃月20 小时前
SpringBoot3.5.x集成Nacos 2.x全流程详解
java·spring boot·后端