Android PDF reading framework/Android PDF framework is simple to use, simple and fast to integrate a simple PDF reader, AndroidPdfViewer framework is simple to use.


1 Introduction

Because of the project presentation some time ago, our team originally planned to do the TXT reading framework, but we found many TXT demos and frameworks, and found that the difficulty was a bit'billion', so I switched to PDF reading without red heart. .

ps: PDF reading is really difficult, more difficult than txt reading, after all, there are many types of formats. But no matter how github has a great god, it is directly integrated, and we will use it directly later. txt is different. There are too many processing categories. Most of the found are open source projects, which are not suitable for direct integration into their own projects, and some also use a database to store processed data. . . . . . I am a rookie, I admit it.

What I brought this time is the simple use of the AndroidPdfViewer framework and some of my heart, I hope it will be helpful to everyone.

Don't talk nonsense, first take a picture to prove your innocence.

Insert picture description here

Insert picture description here

Insert picture description here

		项目地址:https://github.com/barteksc/AndroidPdfViewer

Project address: https://github.com/barteksc/AndroidPdfViewer

Because my purpose is simply to be simple and practical, which can be displayed, and I haven't studied it deeply, so I can only bring you some enlightenment.

Steps for usage

Step 1 Import package / import reference / add dependency

First of all, you have to have AndroidStudio, and will create a new project, and then you can continue to read the blog.

Insert picture description here

Add a line of code at this location: the
purpose is to import the project package and add dependencies.

    implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

Step 2 Change the xml layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">



	下面这个控件才是核心,外面的布局随意,
    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>





</LinearLayout>

Step 3 java file processing

package com.example.pdf3;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;



public class MainActivity extends AppCompatActivity {
    
    
    private int  defaultPage=1;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PDFView pdfView=findViewById(R.id.pdfView);

		//pdfView.fromAsset(String)  文件的数据源(数据来源 / pdf文件地址)
		//pdfView.fromSource(DocumentSource)
		//pdfView.fromStream(InputStream
		//pdfView.fromBytes(byte[])
		//pdfView.fromFile(File)
		//pdfView.fromUri(Uri)
        pdfView.fromAsset("test.pdf")
//                .pages(0, 2, 1, 3, 3, 3) //限制能显示的页面为那些页,不写的话默认展示所有页面,写的话就只会展示你写的哪些页面,例如这行代码规定了只能展示0,2,1,3,3,3这六页。
                .enableSwipe(true) // 允许使用刷卡阻止更改页面
                .swipeHorizontal(false)//是否水平翻页,默认竖直翻页
                .enableDoubletap(false)//是否可以双击方法页面
                .defaultPage(defaultPage)//打开时候的默认页面,这里是上面我自定义的第一页,建议链接数据库,数据放在数据库里好一些。
                .onPageChange(new OnPageChangeListener() {
    
    //设置翻页监听
                    @Override
                    public void onPageChanged(int page, int pageCount) {
    
    
                        //这里面写当监听器监听到对应改变是的反应
                        //例如展示数据与处理数据。
                        //Toast.makeText(MainActivity.this, page + " / " + pageCount, Toast.LENGTH_SHORT).show();
                    }
                }
                .onDraw(onDrawListener) //允许借鉴的东西当前页面,通常在屏幕中间可见
                .onDrawAll(onDrawListener)//允许在所有页面上分别为每个页面绘制内容。仅针对可见页面调用
                .onLoad(onLoadCompleteListener) // 在文档加载并开始呈现之后.设置加载监听
                .onPageScroll(onPageScrollListener)//设置页面滑动监听
                .onError(onErrorListener)
                .onPageError(onPageErrorListener)
                .onRender(onRenderListener) //首次呈现文档后,首次提交文档后调用。
                //调用轻按一次即可返回true(如果已处理),则返回false以切换滚动柄可见性
                .onTap(onTapListener)
                .onLongPress(onLongPressListener)
                .enableAnnotationRendering(true)//呈现注释(例如注释,颜色或表单)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true)//改善低分辨率屏幕上的渲染
               // dp中页面之间的间距。以限定间隔颜色,组视图背景
                .spacing(0)
                .autoSpacing(false) //添加动态间距以适合在屏幕上在其自己的每一页
                .linkHandler(DefaultLinkHandler)
                .pageFitPolicy(FitPolicy.WIDTH) //模式,以适应视图中的页面
                .fitEachPage(false) //使每个页面适合视图,否则较小页面相对于最大页面缩放。
                .pageSnap(false) //将页面捕捉到屏幕边界
                .pageFling(false) //仅更改单个页面,例如ViewPager
                .nightMode(false) //切换夜间模式
                .enableSwipe(true)///是否允许翻页,默认是允许翻
                .onPageChange(null)//当pdf翻页 / 当阅读页数改变时
                .load();//开始加载pdf文件
    }
}

You're done, now you can start displaying the pdf file in your project normally.
Remember to give you the project local file read permission
Remember to give you the project local file read permission
Remember to give you the project local file read permission

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/111477808