繁体   English   中英

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

[英]Display PDF file inside my android application

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

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

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

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'

活动_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>

主活动.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 + "-");
            }
        }
    }

}

也许您可以将 MuPdf 集成到您的应用程序中。 这是我描述的如何做到这一点: 在应用程序中集成 MuPDF 阅读器

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

另请参阅https://github.com/loosemoose/androidpdf以获取示例实现。

我不认为你可以轻松做到这一点。 你应该在这里考虑这个答案:

如何将 pdf 文档显示到 Webview 中?

基本上,如果它是通过 Google 文档在线托管的,则您将能够看到 pdf,但如果您的设备中有它,则无法看到(为此您需要一个独立的阅读器)

Uri path = Uri.fromFile(file );
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path , "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
    startActivity(pdfIntent ); 
    }
catch (ActivityNotFoundException e) {
    Toast.makeText(EmptyBlindDocumentShow.this,
            "No Application available to viewPDF",
            Toast.LENGTH_SHORT).show();
    }  
}  
 public class MainActivity extends AppCompatActivity {
   WebView webview;
   ProgressBar progressbar;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webview = (WebView)findViewById(R.id.webview);
    progressbar = (ProgressBar) findViewById(R.id.progressbar);
    webview.getSettings().setJavaScriptEnabled(true);
    String filename ="http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
    webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

    webview.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            progressbar.setVisibility(View.GONE);
        }
    });

}

}

这是在没有任何 3rd 方库的情况下对我有用的完美解决方案。

在 Android Activity/Fragment 中渲染 PDF 文档(使用 PdfRenderer)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM