首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android PDF Viewer库

Android PDF Viewer库
EN

Stack Overflow用户
提问于 2012-06-28 19:00:21
回答 3查看 37.8K关注 0票数 3

我知道这个问题已经被问了很多次了,但我仍然不清楚是否有一个现有的正常工作的库来原生显示PDF文档。

我只想查看存储在我的应用程序中的PDF文档。打开一个新的活动对我来说是可以的,我不需要在现有的视图中显示它。我已经构建了一段代码来启动读取本地PDF文件的活动意图,但当然,如果设备上还没有安装PDF Viewer应用程序,那么什么也不会发生。

我听说过APV,VuDroid,droidreader等,但它们似乎都是app,而不是可以在我的应用程序代码中使用的库。

那么,有没有真正的Android库可以做到这一点呢?

提前谢谢。

EN

回答 3

Stack Overflow用户

发布于 2013-10-24 15:06:06

这个你可以试一下,它可以在脱机模式下工作https://github.com/bitfield66/PdfViewerAndroid_Offline

它只接受pdf路径。

票数 2
EN

Stack Overflow用户

发布于 2013-10-04 20:09:30

首先,要在android中查看pdf,你必须将pdf转换为图像,然后将它们显示给用户。(我将使用webview)

因此,为此,我们需要此library。这是我编辑过的git版本。

将库导入到项目中后,您需要创建活动。

XML:

代码语言:javascript
复制
<?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="match_parent">

    <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</LinearLayout>

java onCreate:

代码语言:javascript
复制
//Imports:
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.webkit.WebView;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFImage;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFPaint;
import net.sf.andpdf.nio.ByteBuffer;
import net.sf.andpdf.refs.HardReference;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

//Globals:
private WebView wv;
private int ViewSize = 0;

//OnCreate Method:
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Settings
    PDFImage.sShowImages = true; // show images
    PDFPaint.s_doAntiAlias = true; // make text smooth
    HardReference.sKeepCaches = true; // save images in cache

    //Setup webview
    wv = (WebView)findViewById(R.id.webView1);
    wv.getSettings().setBuiltInZoomControls(true);//show zoom buttons
    wv.getSettings().setSupportZoom(true);//allow zoom
    //get the width of the webview
    wv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            ViewSize = wv.getWidth();
            wv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

    pdfLoadImages();//load images
}

加载图像:

代码语言:javascript
复制
private void pdfLoadImages()
{
    try
    {
        // run async
        new AsyncTask<Void, Void, Void>()
                {
                    // create and show a progress dialog
                    ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "", "Opening...");

                    @Override
                    protected void onPostExecute(Void result)
                    {
                        //after async close progress dialog
                        progressDialog.dismiss();
                    }

                    @Override
                    protected Void doInBackground(Void... params)
                    {
                        try
                        {
                            // select a document and get bytes
                            File file = new File(Environment.getExternalStorageDirectory().getPath()+"/randompdf.pdf");
                            RandomAccessFile raf = new RandomAccessFile(file, "r");
                            FileChannel channel = raf.getChannel();
                            ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
                            raf.close();
                            // create a pdf doc
                            PDFFile pdf = new PDFFile(bb);
                            //Get the first page from the pdf doc
                            PDFPage PDFpage = pdf.getPage(1, true);
                            //create a scaling value according to the WebView Width
                            final float scale = ViewSize / PDFpage.getWidth() * 0.95f;
                            //convert the page into a bitmap with a scaling value
                            Bitmap page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);
                            //save the bitmap to a byte array
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            stream.close();
                            byte[] byteArray = stream.toByteArray();
                            //convert the byte array to a base64 string
                            String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                            //create the html + add the first image to the html
                            String html = "<!DOCTYPE html><html><body bgcolor=\"#7f7f7f\"><img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                            //loop through the rest of the pages and repeat the above
                            for(int i = 2; i <= pdf.getNumPages(); i++)
                            {
                                PDFpage = pdf.getPage(i, true);
                                page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);
                                stream = new ByteArrayOutputStream();
                                page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                stream.close();
                                byteArray = stream.toByteArray();
                                base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                                html += "<img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                            }
                            html += "</body></html>";
                            //load the html in the webview
                            wv.loadDataWithBaseURL("", html, "text/html","UTF-8", "");
                    }
                    catch (Exception e)
                    {
                        Log.d("CounterA", e.toString());
                    }
                        return null;
                    }
                }.execute();
                System.gc();// run GC
    }
    catch (Exception e)
    {
        Log.d("error", e.toString());
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-02-25 19:29:14

我喜欢MuPDF Adnroid lib,因为它是用C++/NDK编写的,它有独特的功能,比如可点击的图片(我的意思是链接到图片的网址)-没有其他库有这个功能,我真的需要它。

但我不喜欢这种方式,因为IC是需要的所有时间,而使用MuPDF,我可以DL pdf文件和自由打开它在任何时候离线。此外,WebView方式对于设备来说更“难”,这意味着电池耗尽+滞后+ CPU加热,并且它使用更多的传输(如果与DL&show方式相比)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11243178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档