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

k4ymrczo  于 2023-03-21  发布在  Android
关注(0)|答案(8)|浏览(143)

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

tvokkenx

tvokkenx1#

您可以从此处下载源代码(Display PDF file inside my android application
在您的gradle文件中添加此依赖关系:

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

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

}
up9lanfz

up9lanfz2#

也许你可以把MuPdf集成到你的应用程序中。下面是我描述的如何做到这一点:在应用程序中集成MuPDF Reader

muk1a3rh

muk1a3rh3#

强烈建议您查看PDF.js,它能够在标准的WebView组件中呈现PDF文档。
另请参见https://github.com/loosemoose/androidpdf,以获取其示例实现。

ibps3vxo

ibps3vxo4#

我不认为你可以轻易做到这一点。你应该考虑这个答案在这里:
How can I display a pdf document into a Webview?
基本上你可以看到一个pdf文件,如果它是通过谷歌文档在线托管的,但如果你有它在你的设备(你需要一个独立的阅读器)

sy5wg1nm

sy5wg1nm5#

下面是一个示例代码:

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

    }
}
p5fdfcr1

p5fdfcr16#

这是一个完美的解决方案,为我工作,没有任何第3方库。
Rendering a PDF Document in Android Activity/Fragment (Using PdfRenderer)

9gm1akwq

9gm1akwq8#

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

相关问题