Laire
Laire

Reputation: 1330

Show PDF file in App

I found this two possibilities to show a pdf file.

  1. Open a webView with:

    webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+uri);

  2. Open the pdf File with a extern App:

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(outFile),"application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent);

Both of them work. But my problem is that the pdf is for internal use only and in both examples the user could download it or save it in another folder.

I know frameworks for this in iOS dev, I looking for a solution that works with Android.

Upvotes: 14

Views: 38792

Answers (3)

GvSharma
GvSharma

Reputation: 2670

Android provides PDF API now with which it is easy to present pdf content inside application.

you can find details here

Below is the sample snippet to render from a pdf file in assets folder.

    private void openRenderer(Context context) throws IOException {
    // In this sample, we read a PDF from the assets directory.
    File file = new File(context.getCacheDir(), FILENAME);
    if (!file.exists()) {
        // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
        // the cache directory.
        InputStream asset = context.getAssets().open(FILENAME);
        FileOutputStream output = new FileOutputStream(file);
        final byte[] buffer = new byte[1024];
        int size;
        while ((size = asset.read(buffer)) != -1) {
            output.write(buffer, 0, size);
        }
        asset.close();
        output.close();
    }
    mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    // This is the PdfRenderer we use to render the PDF.
    if (mFileDescriptor != null) {
        mPdfRenderer = new PdfRenderer(mFileDescriptor);
    }
}

update: This snippet is from google developers provided samples.

Upvotes: 19

Jaydev
Jaydev

Reputation: 1812

Many libraries are available for showing pdfs within your own app.

For a working example using android-pdfView, see this blog post. It demonstrates the basic usage of the library to display pdf onto the view with vertical and horizontal swipe.

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(new File("/storage/sdcard0/Download/pdf.pdf")).defaultPage(1).enableSwipe(true).onPageChange(this).load();

Upvotes: 5

AnkitSomani
AnkitSomani

Reputation: 1192

You can show the PDF in your own viewer

Tier are few open source pdf viewers you should look at: http://androiddeveloperspot.blogspot.com/2013/05/android-pdf-reader-open-source-code.html

You can encrypt the pdf and make sure that your viewer only will be able to decrypt it.

Upvotes: 1

Related Questions