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();
});
相关推荐
傻啦嘿哟10 小时前
Python实现PDF文档高效转换为HTML文件:从基础到进阶的完整指南
python·pdf·html
WayneJoon.H10 小时前
Java反序列化 CC7链分析
java·安全·网络安全·cc链·反序列化
苦逼的搬砖工11 小时前
基于 easy_rxdart 的轻量响应式与状态管理架构实践
android·flutter
2501_9159184111 小时前
苹果上架 iOS 应用的工程实践,一次从零到上线的完整记录
android·ios·小程序·https·uni-app·iphone·webview
liu_bees11 小时前
Jenkins 中修改 admin 账号密码的正确位置与方法
java·运维·tomcat·jenkins
明洞日记11 小时前
【设计模式手册011】享元模式 - 共享细粒度对象的高效之道
java·设计模式·享元模式
G皮T11 小时前
【Java】Java 运行时数据区域(一):名词概念
java·jvm·runtime·运行时·运行时数据区域
z***y86212 小时前
Java数据挖掘开发
java·开发语言·数据挖掘
鱼锦0.012 小时前
基于spring+vue把图片文件上传至阿里云oss容器并回显
java·vue.js·spring
從南走到北12 小时前
JAVA国际版同城跑腿源码快递代取帮买帮送同城服务源码支持Android+IOS+H5
android·java·ios·微信小程序