Abhishek Tamta
Abhishek Tamta

Reputation: 463

How can I read and view PDF in android?

I am working on an application which require to open, read and view pdf in Android Application. I am having a pdf files in assets folder. By default android does not support pdf.

Is there any API which is used to open, read and view pdf files. These files are in Assets folder.

After two days hard work and doing a lot of R&D I have got little bit solution but few Queries still remaining.

I have used PdfViewer its a Library to display PDF in android. And below is my code

MainActivity

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, MyPdfViewerActivity.class);
        intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
                "mnt/sdcard/sample.pdf");
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

MyPdfViewerActivity

public class MyPdfViewerActivity extends PdfViewerActivity {

    public int getPreviousPageImageResource() {
        return R.drawable.left_arrow;
    }

    public int getNextPageImageResource() {
        return R.drawable.right_arrow;
    }

    public int getZoomInImageResource() {
        return R.drawable.zoom_in;
    }

    public int getZoomOutImageResource() {
        return R.drawable.zoom_out;
    }

    public int getPdfPasswordLayoutResource() {
        return R.layout.pdf_file_password;
    }

    public int getPdfPageNumberResource() {
        return R.layout.dialog_pagenumber;
    }

    public int getPdfPasswordEditField() {
        return R.id.etPassword;
    }

    public int getPdfPasswordOkButton() {
        return R.id.btOK;
    }

    public int getPdfPasswordExitButton() {
        return R.id.btExit;
    }

    public int getPdfPageNumberEditField() {
        return R.id.pagenum_edit;
    }
}

There is a query It is taking time in loading Pdf page and there is no output coming. Screenshot of Emulator enter image description here

Is there any solution how to resolve this problem?

Thanks!!

Upvotes: 0

Views: 6159

Answers (3)

H.A.H.
H.A.H.

Reputation: 3897

You can do this with PDFBOX port for anroid, extremely easy. Pseudo code:

PDDocument.load(byteArray);

new PDFRenderer(document);

After that for each page:

PDFRrenderer.renderImage(page, scale)

scale is important, if you are on phone, 1 is ok, (72dp default), on tablet you need higher value.

And then add all images to vertical recyclerview, or (the simplier version) vertical linear layout, nested into vertical scrollview.

No 3rd party applications, no googledocs, no random libraries...

Upvotes: 1

fida1989
fida1989

Reputation: 3249

Try android-pdfview library. It works very well.

Include PDFView in your layout:

<com.joanzapata.pdfview.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Load a PDF file:

public class PDFViewActivity extends Activity implements OnPageChangeListener {

    public static final String SAMPLE_FILE = "sample.pdf";

    public static final String ABOUT_FILE = "about.pdf";


    PDFView pdfView;


    String pdfName = SAMPLE_FILE;


    Integer pageNumber = 1;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        pdfView  =(PDFView)findViewById(R.id.pdfView);

        pdfView.fromAsset(pdfName)
        .defaultPage(1)
        .showMinimap(false)
        .enableSwipe(true)
        .load();



    }    

    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(format("%s %s / %s", pdfName, page, pageCount));
    }




}

Upvotes: 2

Jitsu
Jitsu

Reputation: 769

That's quite a problem.

You should by default rely on user applications and just simply send an intent to check if there is anything that can handle PDF file (like for example Adobe reader).

If for some reason you want to render PDF in your application, there are two major libraries for that:

Upvotes: 1

Related Questions