在我的Android应用程序中显示PDF文件

kai*_*uki 15 pdf android android-activity

我无法弄清楚如何在Android应用程序中显示PDF文件.到目前为止,我发现可以Intent使用Android默认应用程序启动并打开PDF.但我想直接在我的应用程序中查看PDF文件而不退出.我的布局中有一个页眉和一个页脚 - 我想在两者之间打开PDF.我还PdfReader.jar从github.com 找到了一个文件,但它在一个新活动中打开了PDF.

Dee*_*uri 21

你可以从这里下载源代码(在我的android应用程序中显示PDF文件)

在gradle文件中添加此依赖项:

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark"
        android:text="View PDF"
        android:textColor="#ffffff"
        android:id="@+id/tv_header"
        android:textSize="18dp"
        android:gravity="center"></TextView>

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_below="@+id/tv_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

package pdfviewer.pdfviewer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.shockwave.pdfium.PdfDocument;

import java.util.List;

public class MainActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener{
    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String SAMPLE_FILE = "android_tutorial.pdf";
    PDFView pdfView;
    Integer pageNumber = 0;
    String pdfFileName;

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


        pdfView= (PDFView)findViewById(R.id.pdfView);
        displayFromAsset(SAMPLE_FILE);
    }

    private void displayFromAsset(String assetFileName) {
        pdfFileName = assetFileName;

        pdfView.fromAsset(SAMPLE_FILE)
                .defaultPage(pageNumber)
                .enableSwipe(true)

                .swipeHorizontal(false)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
    }


    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
    }


    @Override
    public void loadComplete(int nbPages) {
        PdfDocument.Meta meta = pdfView.getDocumentMeta();
        printBookmarksTree(pdfView.getTableOfContents(), "-");

    }

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {

            Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 你们确实意识到添加这个库会使你的apk大小增加16MB.对 ? (4认同)
  • 有人说@你的博客:就像一个魅力.给你+1.感谢那.只是说清楚:pdf必须在**资产**文件夹内,它必须在**主**文件夹内(而不是**res**).谢谢. (3认同)
  • @sHOLE 我也花了一段时间才弄明白。它的解决方案是拆分您的 APK,但即使拆分后平均大小也增加了 5MB,那不是很好。 (3认同)
  • 我很惊讶@legalimpurity 提到的还没有人提出图书馆规模的问题。 (2认同)

Yur*_*ury 6

也许你可以在你的应用程序中集成MuPdf.这里我已经描述了如何做到这一点:在应用程序中集成MuPDF Reader


Jay*_*dri 6

强烈建议您查看PDF.js,它能够在标准 WebView 组件中呈现 PDF 文档。

另请参阅https://github.com/loosemoose/androidpdf了解此示例的实现。