当前在Android应用浏览pdf大概几种方式

1、直接打开各种浏览器或者软件(缺点用户无法在应用内直接看到pdf,效果不好)

2、android-pdf-viewer在应用中可以直接看到,通过一系列封装可能实现大部分要求(缺点将pdf转成图片加载,文本性质的pdf无法选择复制)

3、腾讯X5内核腾讯浏览服务-SDK下载;(缺点集成麻烦,部分情况x5内核还需要在应用内下载)

4、mupdf(集成超级麻烦,我连稀得看没稀得看)

5、pdf.js (满足大部分要求了,缺点需要js、h5稍微知道点)

本文重点实现pdf.js

提前把权限获取好什么sd卡读写啊  什么网络啊之类的。

第一步:把pdf.js依赖放入assets;在这里下一下,Getting StartedA general-purpose, web standards-based platform for parsing and rendering PDFs.http://mozilla.github.io/pdf.js/getting_started/#download

第二步:重写个webview运行


public class PDFView extends WebView {public boolean isTop = true, isBottom = false;//用于判断滑动位置private final static String PDFJS = "file:///android_asset/pdf_js/web/viewer.html?file=";public PDFView(Context context) {super(context);init();}public PDFView(Context context, AttributeSet attrs) {super(context, attrs);init();}public PDFView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {return false;}private void init() {WebSettings settings = getSettings();settings.setJavaScriptEnabled(true);settings.setAllowUniversalAccessFromFileURLs(true);settings.setSupportZoom(true);settings.setUseWideViewPort(true);settings.setBuiltInZoomControls(true);settings.setDisplayZoomControls(false);setWebViewClient(new WebViewClient() {// 注册滑动监听方法viewerContainer为加载pdf的viewid@Overridepublic void onPageFinished(WebView webView, String s) {super.onPageFinished(webView, s);//滑动监听String startSave = "\n" +"document.getElementById(\"viewerContainer\").addEventListener('scroll',function () {\n" +"if(this.scrollHeight-this.scrollTop - this.clientHeight < 50){\n" +"window.java.bottom(); \n" +"}\n" +"else if(this.scrollTop==0){\n" +"window.java.top(); \n" +"}\n" +"else {\n" +"window.java.scrolling(); \n" +"}\n" +"});";webView.loadUrl("javascript:" + startSave);}});addJavascriptInterface(new Object() {@JavascriptInterfacepublic void bottom() {Log.e("msg+++++++", "到了低端");isBottom = true;isTop = false;}@JavascriptInterfacepublic void scrolling() {Log.e("msg+++++++", "滑动中");isBottom = false;isTop = false;}@JavascriptInterfacepublic void top() {Log.e("msg+++++++", "到了顶端");isBottom = false;isTop = true;}}, "java");}@Overrideprotected void onCreateContextMenu(ContextMenu menu) {super.onCreateContextMenu(menu);}@Overridepublic ActionMode startActionMode(ActionMode.Callback callback) {return super.startActionMode(callback);}//加载本地的pdfpublic void loadLocalPDF(String path) {loadUrl(PDFJS + "file://" + path);}//加载url的pdfpublic void loadOnlinePDF(String url) {loadUrl(PDFJS + url);}
}

第三步:引用+使用

<xxxxxxxxxxx.PDFViewandroid:id="@+id/pdfview"android:layout_width="match_parent"android:layout_height="400dp"android:visibility="gone" />
findViewById(R.id.pdfview).loadOnlinePDF(url)

怎么样是不是很简单?

在自定义webview中大家可以看到有一段交互注册代码,解决什么问题呢,滑动冲突!!!

当父view是scrollview时候  嵌套这个webview就会出现上下滑动冲突

这个时候写个事件分发

/*** scrollview 与 封装的pdfview  滑动冲突解决*/
public class DispatchScrollview extends ScrollView {private List<View> views = new ArrayList<>();private int touchY;public DispatchScrollview(Context context) {super(context);}public DispatchScrollview(Context context, AttributeSet attrs) {super(context, attrs);}public DispatchScrollview(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public void setDispatchView(View view) {views.add(view);}public void clear() {views.clear();}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {if (ev.getAction() == MotionEvent.ACTION_DOWN) {touchY = (int) ev.getY();Log.d("test", "touchY--->" + touchY);}for (View view : views) {int[] viewlocation = new int[2];view.getLocationOnScreen(viewlocation);int thenx = viewlocation[0];int theny = viewlocation[1];Log.d("test", "Screenx--->" + thenx + "  " + "Screeny--->" + theny);if (touchY > theny && touchY < theny + view.getHeight()) {if (ev.getAction() == MotionEvent.ACTION_MOVE && view instanceof PDFView) {int yPath = (int) (ev.getY() - touchY);if (yPath > 0 && ((PDFView) view).isTop) {return super.onInterceptTouchEvent(ev);}if (yPath < 0 && ((PDFView) view).isBottom) {return super.onInterceptTouchEvent(ev);}if (view.getVisibility() == GONE)return super.onInterceptTouchEvent(ev);int[] scrolllocation = new int[2];getLocationInWindow(scrolllocation);int scrolly = scrolllocation[1]; // view距离window 顶边的距离(即y轴方向int[] location = new int[2];view.getLocationInWindow(location);int y = location[1] - scrolly; // view距离window 顶边的距离(即y轴方向)int yh = y + view.getHeight();int touchY = (int) ev.getY();if (view != null && touchY >= y && touchY < yh) {//如果是recyclerview的范围时 不允许接收事件return false;}}}}return super.onInterceptTouchEvent(ev);}
}

这个时候用这个 替换scrollview,有几个pdfview set进几个这个时候你会发现在pdf区域往上拉的时候pdf不到底先滑pdf ,到底之后整个scrollview上行,在pdf区域往下拉的时候pdf不到顶先滑pdf,到顶之后整个scrollview下行;完美解决

 scrollView.setDispatchView(pdfView);

付上效果,感觉相当丝滑,而且如果pdf不是图片转还可以长按复制之类的,因为人家是网页的

补充:

集成之后,又发现没有双指缩放,用user-scalable=yes的话 又发现带着ui一起缩放,那么没招只能自己加了,进pdf.js源码里翻出点击加减号放大缩小的代码,用java调用

webView.setOnTouchListener(new OnTouchListener() {private float OldX1, OldY1, OldX2, OldY2;private float NewX1, NewY1, NewX2, NewY2;@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_POINTER_2_DOWN:if (event.getPointerCount() == 2) {OldX1 = event.getX(0);OldY1 = event.getY(0);OldX2 = event.getX(1);OldY2 = event.getY(1);}break;case MotionEvent.ACTION_MOVE:if (event.getPointerCount() == 2) {if(OldX1 == -1 && OldX2 == -1)break;NewX1 = event.getX(0);NewY1 = event.getY(0);NewX2 = event.getX(1);NewY2 = event.getY(1);float disOld = (float) Math.sqrt((Math.pow(OldX2- OldX1, 2) + Math.pow(OldY2 - OldY1, 2)));float disNew = (float) Math.sqrt((Math.pow(NewX2- NewX1, 2) + Math.pow(NewY2 - NewY1, 2)));Log.e("onTouch", "disOld=" + disOld + "|disNew="+ disNew);if (disOld - disNew >= 25) {// 缩小
//                          wv.zoomOut();webView.loadUrl("javascript:PDFViewerApplication.zoomOut()");Log.e("onTouch", "zoomOut");} else if (disNew - disOld >= 25) {// 放大
//                          wv.zoomIn();webView.loadUrl("javascript:PDFViewerApplication.zoomIn()");Log.e("onTouch", "zoomIn");}OldX1 = NewX1;OldX2 = NewX2;OldY1 = NewY1;OldY2 = NewY2;}break;case MotionEvent.ACTION_UP:if (event.getPointerCount() < 2) {OldX1 = -1;OldY1 = -1;OldX2 = -1;OldY2 = -1;}break;}return false;}});

ok缩放也有了,最后 有点问题  pdf元素多的时候展示页有点卡,在想解决方法

Android应用内加载pdf 使用pdf.js相关推荐

  1. Android原生加载显示在线PDF链接

    工作需求,需要加载一个在线pdf,链接以".pdf"结尾.通过查找,大都需要先把pdf文件下载下来,然后再加载,有以下几种实现方式: 方法一 :参考谷歌demo(android-P ...

  2. Android应用内加载pdf的方法?

    [可行] 最直接的方式下载到本地, 然后调用相关应用打开 [可行,有局限] 使用 webview 加载, 需要在原url前面加上http://docs.google.com/gview?url=,即使 ...

  3. Android加载预览PDF文件

    从去年9月份进入新公司后,项目中需要在APP中展示PDF,这个之前没有接触过,因此也查了好多的资料和demo,这里我说明下我用过的pdf展示比较好点View或者第三方,本人亲测. 1.使用Google ...

  4. Android webview加载pdf实验成功

    pdf.js可以实现在html下直接浏览pdf文档,是一款开源的pdf文档读取解析插件 pdf.js主要包含两个库文件,一个pdf.js和一个pdf.worker.js,,一个负责API解析,一个负责 ...

  5. 手机端 阅读 pdf 文件 touchPDF.js

    touchpdf 中文API 下载地址: http://github.com/loicminghetti/touchpdf/archive/master.zip 一款在手机端 阅读pdf 文件的 js ...

  6. Android调用WPS第三方App打开PDF文档,一直停留在首页,提示正在加载文档类型

    Android 7.0 以后对Uri的访问进行了限制,需要在manifest项目清单文件里面添加 provider,具体怎么写这个就不说了. 解决此问题只需添加以下代码即可: intent.addFl ...

  7. android 加载静态网页,React Native:如何在WebView内加载SPA或本地静态HTML页面?

    React Native:如何在WebView内加载SPA或本地静态HTML页面? React Native WebView允许你使用uri属性加载可公开访问的资源,就像程序内的一个浏览器.但是,当你 ...

  8. img pdf 展示_pdf.js实现图片在线预览

    项目需求 前段时间项目中遇到了一个模块,是关于在线预览word文档(PDF文件)的,所以,找了很多插件,例如,pdf.js,pdfobject.js框架,但是pdfobject.js框架对于IE浏览器 ...

  9. android pdf编辑,PDF Reader Pro 功能强大的 PDF 阅读编辑器

    原标题:PDF Reader Pro 功能强大的 PDF 阅读编辑器 是一款操作简单,功能强大的处理软件,提供了 PDF..PDF创建.页面.转换.表单创建填写与签名,添加水印,页脚等功能.Mac, ...

  10. android pdf 插件,Pdf文件查看 android原生插件

    更新记录 1.0.7(2021-05-10) 1.修复若干bug 2.优化用户体验 1.0.6(2021-03-31) 1.全新UI设计 2.增加目录设置功能 3.增加全屏切换功能 4.增加跳转功能 ...

最新文章

  1. tensorflow电子书(附下载链接)
  2. 浅谈wcscpy_s之用法
  3. python每行输出5个数_python打印杨辉三角及输出第m行第k个数
  4. jpa mysql存储过程_Jpa调用存储过程及参数
  5. WPF 如何在代码中使用自定义的鼠标资源
  6. mongoDB学习——第一天
  7. Django:(02)项目配置
  8. sprintf 、vsprintf 、_vsntprintf
  9. fastreport实现动态列_excel中如何实现聚光灯效果?
  10. 将beyond compare设置为svn的代码比较工具
  11. 用报初会的照片报计算机,会计考试报名倒计时,手把手教你一次通过审核工具...
  12. 0x80070079信号灯超时_onedrive下载文件时,出现”0x80070079信号灯超时时间已到”...
  13. 计算机英语摘要,跪求计算机论文摘要英文翻译
  14. 计算摄影——自动构图
  15. 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
  16. java 地牢猎手,地牢猎手5新手必看速成攻略
  17. 原生js高仿浏览器ctrf+f
  18. mac 安装typescript
  19. Feign原理:current list of Servers哪里来的
  20. Eureka 服务注册中心的探究

热门文章

  1. 《信号与系统》解读 第1章 信号与系统概述-1:信号与系统的描述和分析方法
  2. gerrit常见问题及解决方法
  3. 999. Available Captures for Rook
  4. Java动态代理机制原理详解(JDK 和CGLIB,Javassist,ASM)
  5. 台式计算机蓝牙完成配对,电脑蓝牙已配对但连不上 带你快速解决一下
  6. cout输出中加入双引号
  7. GIS招聘 | 辽宁省省直事业单位(含测绘、地信等专业岗位)
  8. 南阳oj The Triangle
  9. 在c语言中 根据数据的组织形式,文件工作组织形式 文件的基本组织方式有哪几种?...
  10. Calendar根据日期获取年份和周、当前周的所有日期