安卓PDF查看器库

3
我知道这个问题已经被问了很多次,但我仍然不清楚是否有一个现有且正常工作的库可以本地显示PDF文档。
我只想查看存储在我的应用程序内部的PDF文档。对于我来说,在新的Activity中打开它是可以的,我不需要在现有视图中显示它。我已经编写了一段代码来启动Activity intent以阅读我的本地PDF文件,但如果设备上还没有安装PDF Viewer应用程序,则什么也不会发生。
我听说过APV、VuDroid、droidreader等,但似乎它们都是APK,而不是可以在我的应用程序代码中使用的库。
那么,是否有任何真正的Android库可以实现这一点?
提前感谢。

我不明白。这个库在哪里可以找到? - thomaus
从此链接下载pdfviewer.jar。http://www.ziddu.com/download/19248664/PdfViewer.jar.html - Dipak Keshariya
但是你的库有没有文档?我甚至不知道如何使用它... - thomaus
复制完PDF文件后,请关闭您的设备,然后再次打开并检查。 - Dipak Keshariya
我认为这是最好的:https://github.com/barteksc/AndroidPdfViewer/ - Pratik Butani
显示剩余9条评论
3个回答

2

你知道如何实现这个吗? - Kuls
没有教程? - gumuruh

0

首先,在Android中查看PDF文件,您需要将PDF转换为图像,然后将其显示给用户(我将使用WebView)。

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

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

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="match_parent">

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

</LinearLayout>

Java的onCreate方法:

//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
}

加载图片:

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());
    }
}

我在这两行代码中遇到了错误: ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())); PDFFile pdf = new PDFFile(bb); NEW关键字和PDFFile(bb)都出现了错误,那么该如何解决呢? - Faiz Anwar

0

我喜欢MuPDF Adnroid lib,因为它是用C++/NDK编写的,并且具有独特的功能,如可点击的图像(我的意思是与图像相关联的URL)- 我没有找到其他具备此功能的库,而我真的需要它。
实际上,您可以完全不使用库来打开PDF:通过Google文档使用WebView,但我不喜欢这种方式,因为每次使用时都需要IC。而使用MuPDF,我可以下载PDF文件并随时离线打开它。此外,WebView方式对设备来说更加“困难”,会导致电池耗尽、卡顿和CPU过热,并且使用的流量更多(与下载和显示方式相比)。


你能提供关于如何使用MuDroid的信息吗? - Arda Kara
我相信他们提供了一个示例应用程序。我确定他们这样做了,因为我记得我修改了一个示例应用程序来完成我的特定任务。 - Stan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接