在米多财富的时候遇到PDF阅读的问题,搞了很久,显示效果都不怎么样,后来放弃在APP中显示

在世纪保众又遇到了这个PDF阅读的需求,通过查阅Android官方的API发现了

显示的效果

主要代码:

/** Copyright (C) 2014 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.example.android.pdfrendererbasic;import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;import java.io.IOException;/*** This fragment has a big {@ImageView} that shows PDF pages, and 2 {@link android.widget.Button}s to move between* pages. We use a {@link android.graphics.pdf.PdfRenderer} to render PDF pages as {@link android.graphics.Bitmap}s.*/
public class PdfRendererBasicFragment extends Fragment implements View.OnClickListener {/*** Key string for saving the state of current page index.*/private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index";/*** File descriptor of the PDF.*/private ParcelFileDescriptor mFileDescriptor;/*** {@link android.graphics.pdf.PdfRenderer} to render the PDF.*/private PdfRenderer mPdfRenderer;/*** Page that is currently shown on the screen.*/private PdfRenderer.Page mCurrentPage;/*** {@link android.widget.ImageView} that shows a PDF page as a {@link android.graphics.Bitmap}*/private ImageView mImageView;/*** {@link android.widget.Button} to move to the previous page.*/private Button mButtonPrevious;/*** {@link android.widget.Button} to move to the next page.*/private Button mButtonNext;public PdfRendererBasicFragment() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_pdf_renderer_basic, container, false);}@Overridepublic void onViewCreated(View view, Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);// Retain view references.mImageView = (ImageView) view.findViewById(R.id.image);mButtonPrevious = (Button) view.findViewById(R.id.previous);mButtonNext = (Button) view.findViewById(R.id.next);// Bind events.mButtonPrevious.setOnClickListener(this);mButtonNext.setOnClickListener(this);// Show the first page by default.int index = 0;// If there is a savedInstanceState (screen orientations, etc.), we restore the page index.if (null != savedInstanceState) {index = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);}showPage(index);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);try {openRenderer(activity);} catch (IOException e) {e.printStackTrace();Toast.makeText(activity, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();activity.finish();}}@Overridepublic void onDetach() {try {closeRenderer();} catch (IOException e) {e.printStackTrace();}super.onDetach();}@Overridepublic void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);if (null != mCurrentPage) {outState.putInt(STATE_CURRENT_PAGE_INDEX, mCurrentPage.getIndex());}}/*** Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.*/private void openRenderer(Context context) throws IOException {// In this sample, we read a PDF from the assets directory.mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();// This is the PdfRenderer we use to render the PDF.mPdfRenderer = new PdfRenderer(mFileDescriptor);}/*** Closes the {@link android.graphics.pdf.PdfRenderer} and related resources.** @throws java.io.IOException When the PDF file cannot be closed.*/private void closeRenderer() throws IOException {if (null != mCurrentPage) {mCurrentPage.close();}mPdfRenderer.close();mFileDescriptor.close();}/*** Shows the specified page of PDF to the screen.** @param index The page index.*/private void showPage(int index) {if (mPdfRenderer.getPageCount() <= index) {return;}// Make sure to close the current page before opening another one.if (null != mCurrentPage) {mCurrentPage.close();}// Use `openPage` to open a specific page in PDF.mCurrentPage = mPdfRenderer.openPage(index);// Important: the destination bitmap must be ARGB (not RGB).Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),Bitmap.Config.ARGB_8888);// Here, we render the page onto the Bitmap.// To render a portion of the page, use the second and third parameter. Pass nulls to get// the default result.// Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);// We are ready to show the Bitmap to user.mImageView.setImageBitmap(bitmap);updateUi();}/*** Updates the state of 2 control buttons in response to the current page index.*/private void updateUi() {int index = mCurrentPage.getIndex();int pageCount = mPdfRenderer.getPageCount();mButtonPrevious.setEnabled(0 != index);mButtonNext.setEnabled(index + 1 < pageCount);getActivity().setTitle(getString(R.string.app_name_with_index, index + 1, pageCount));}/*** Gets the number of pages in the PDF. This method is marked as public for testing.** @return The number of pages.*/public int getPageCount() {return mPdfRenderer.getPageCount();}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.previous: {// Move to the previous pageshowPage(mCurrentPage.getIndex() - 1);break;}case R.id.next: {// Move to the next pageshowPage(mCurrentPage.getIndex() + 1);break;}}}}

资源下载地址:http://download.csdn.net/detail/qq_18833399/9522929

Android PDF阅读相关推荐

  1. chrome pdf android,Android PDF阅读器 (基于谷歌平台的Adobe Reader)

    Android PDF阅读器 (基于谷歌平台的Adobe Reader):Adobe Reader 是用于打开和使用在 Adobe Acrobat 中创建的 Adobe PDF 的工具. 虽然无法在 ...

  2. Android PDF阅读框架/Android PDF框架简单使用,简单快速集成简易的PDF阅读器 ,AndroidPdfViewer框架简单使用。

    文章目录 1:前言 使用步骤 步骤1 导包 / 导引用 / 添加依赖 步骤2 更改xml布局文件 步骤3 java文件处理 1:前言 因为前段时间项目展示,我们小组本打算做的是TXT阅读框架,但是找了 ...

  3. Android PDF 阅读器源码

    2019独角兽企业重金招聘Python工程师标准>>> 一个开源的PDF阅读器源码,代码挺多,有兴趣的可以看看. 转载:http://www.adobex.com/android/s ...

  4. android pdf阅读器开发_如何在 Windows 10 中将 Firefox 设置为默认 PDF 阅读器

    PDF 作为办公一族中的必备文档,很多人都会编辑或者创建 PDF 文档,而在 Windows 系统中并没有默认的 PDF 阅读器.而在新版的 Microsoft Edge微软已在开始提供有真正的高级 ...

  5. 开源 Android pdf 阅读器开发总结

    前段时间项目涉及到 pdf 阅读,因此我开始找了些源码阅读比较,现在贴出各实现方案的对比.希望对大家有帮助.方便大家的阅读,我将自己认为最好的排在最前. Vudroid Google code:htt ...

  6. android pdf阅读器开发_PDF to EPUB Converter Mac(PDF转EPUB转换器)

    EPUB to PDF Converter Mac特别版是专为Mac用户设计的一款PDF转EPUB转换器,尽管PDF是使用最广泛的电子文档格式,并且具有很多优点,但由于PDF不可重排,因此无法很好地适 ...

  7. android标记 pdf阅读器,Android pdf阅读器

    我想使用 Android pdf库 http://andpdf.sourceforge.net/,但我有同样的错误.日志: ST='file 'no file selected' not found' ...

  8. android pdf阅读工具,android pdf 阅读器开发, pdf demo, pdf第三方控件

    Activity调用的方法 public class PDFActivity extends Activity implements OnPageChangeListener { public sta ...

  9. android开发 pdf阅读器 第三方可,android pdf 阅读器开发, pdf demo, pdf第三方控件

    demo library 下载 Activity调用的方法 public class PDFActivity extends Activity implements OnPageChangeListe ...

  10. android pdf阅读器 ,这里介绍两个

    http://download.csdn.net/detail/sun6223508/7967771 http://download.csdn.net/detail/sun6223508/796783 ...

最新文章

  1. Ubuntu网卡地址配置、设置 DNS和主机名
  2. 基于邮件通道的WCF通信系统
  3. mysql install语句_mysql8 参考手册--INSTALL COMPONENT语句
  4. NIO详解(九):Channel详解
  5. 深入理解Openstack自动化部署
  6. 提高BSEG处理效率
  7. tf.truncated_normal
  8. 本地计算机端口流量,计算机和防火墙上的端口及其用途-101问题
  9. Blender建模与游戏换装(转载文)
  10. 手机来电秀怎么开启_360手机卫士怎么设置来电秀 360手机卫士来电秀设置方法...
  11. hadoop eclipse plugin windows下载集合
  12. 池化技术及jdk的线程池讲解
  13. 面试记录:题都没答就走了
  14. php 开源留言板,PHP开源多功能留言板(SyGuestBook)
  15. Android 11 存储权限适配指南
  16. Delphi 10.3 Rio实现FMX应用APP增加Android“应用程序快捷方式”
  17. 宁宛 机器人_全文阅读 .001 忠犬机器人
  18. 3D游戏编程与设计作业五
  19. Docker中安装并配置redis
  20. POI DataValidation 删除数据有效性验证

热门文章

  1. 简单的学生管理系统详解(附源码)
  2. 计算机win7卡顿如何解决方法,win7系统运行卡顿的解决方法
  3. 各大影视、资源、技术论坛地址及简介2006版!
  4. CSDN新版下载频道改版上线了
  5. 产业互联网周报:滴滴被处以80亿元巨额罚款;消息称中国正启动欧洲企业到中国上市计划;字节跳动确认自研专用芯片...
  6. JAVA Excel下载学习
  7. VC++可视化编程——创建空白窗口
  8. PMP助力!让你成功转型项目管理
  9. Unity3d发布WebPlayer版本遇到的问题的解决方法
  10. Spring源码全解