How to display the pdf in Android ?

Quick start incorporating a PDF viewing activity into your project:

1) Add PdfViewer.jar into your project’s build path

2) Copy the following drawable resources from PdfViewer/res/drawable into YourProject/res/drawable left_arrow.png right_arrow.png zoom_in.png zoom_out.png

3) Copy the following layout resources from PdfViewer/res/layout into YourProject/res/layout dialog_pagenumber.xml pdf_file_password.xml

4) Derive your PDF activity from net.sf.andpdf.pdfviewer.PdfViewerActivity

5) Using the default drawables and layouts:


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

6) Invoke your PdfViewActivity derived with the following code:


Intent intent = new Intent(this, YourPdfViewerActivity.class);

intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");

startActivity(intent);

Download the source code and demo code from below link here

 

 

EDIT:  Another pdf viewer source for free  at github.

https://github.com/rameshakulapc/PDFViewer

Find the reference  Post by Andhradroid.

Advertisements

19 thoughts on “How to display the pdf in Android ?

  1. Can u give me the more explanation where u s trucked ? I have tested it in 2.3 emulator, its working fine for me.Had u tested with my source code. I have added library and demo projects.

  2. same i have got error here log …plz share whole code for view PDF
    07-30 13:46:33.421: E/AndroidRuntime(6092): FATAL EXCEPTION: main
    07-30 13:46:33.421: E/AndroidRuntime(6092): java.lang.RuntimeException: Failed to register input channel. Check logs for details.
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.view.InputQueue.nativeRegisterInputChannel(Native Method)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.view.InputQueue.registerInputChannel(InputQueue.java:92)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.view.ViewRoot.setView(ViewRoot.java:572)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:207)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:121)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.view.Window$LocalWindowManager.addView(Window.java:424)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2174)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.os.Handler.dispatchMessage(Handler.java:99)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.os.Looper.loop(Looper.java:123)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at android.app.ActivityThread.main(ActivityThread.java:3687)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at java.lang.reflect.Method.invokeNative(Native Method)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at java.lang.reflect.Method.invoke(Method.java:507)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    07-30 13:46:33.421: E/AndroidRuntime(6092): at dalvik.system.NativeStart.main(Native Method)

  3. Is it possible to use it with pdfs stored inside assets folder? because I’ve tried to replace “/mnt/…” with “file:///android_asset/…” and didn’t work.

  4. jus right click pdfviewer demo and click on properties…there click on android and on the right side bottom click on add button to add the library project…once the library project is added click apply and then ok…and bingo…the project runs successfully….
    But there is one problem…when i load a pdf file inmany of the files there is an error of ttf fonts showing due to which pdf becomes corrupted and cat be viewed completely…any solution for that….???

  5. In your code, the path to PDF is specified as below:
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
    “mnt/sdcard/sample.pdf”);

    I want to embed PDF in the application code itself. My question is where should I copy sample.pdf file in my project? What should be the path in the above statement?

  6. have some issue. on Android 4.4.2 i have “Loading PDF Page” all the time. where on Android 2.3.3 everything OK.

  7. Below is what I did to load, from the assets folder but it only say Loading PDF Page and do nothing, Pls someone help

    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    File fileBrochure = new File(Environment.getExternalStorageDirectory().getPath() + “/” + “abcd.pdf”);
    if (!fileBrochure.exists())
    {
    CopyAssetsbrochure();
    }
    Intent intent = new Intent(this, MyPdfViewerActivity.class);
    /** PDF reader code */
    File file = new File(Environment.getExternalStorageDirectory().getPath() + “/” + “abcd.pdf”);

    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, file);

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try
    {
    getApplicationContext().startActivity(intent);
    }
    catch (ActivityNotFoundException e)
    {
    Toast.makeText(this, “NO Pdf Viewer”, Toast.LENGTH_SHORT).show();
    }
    }

    //method to write the PDFs file to sd card
    private void CopyAssetsbrochure() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try
    {
    files = assetManager.list(“”);
    }
    catch (IOException e)
    {
    Log.e(“tag”, e.getMessage());
    }
    for(int i=0; i<files.length; i++)
    {
    String fStr = files[i];
    if(fStr.equalsIgnoreCase("abc.pdf"))
    {
    InputStream in = null;
    OutputStream out = null;
    try
    {
    in = assetManager.open(files[i]);
    out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
    copyFile(in, out);
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
    break;
    }
    catch(Exception e)
    {
    Log.e("tag", e.getMessage());
    }
    }
    }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
    out.write(buffer, 0, read);
    }
    }
    }

  8. I tried the below to load from assets folder but I get only Loading PDF Page and nothing else pls someone help.

    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    File fileBrochure = new File(Environment.getExternalStorageDirectory().getPath() + “/” + “abc.pdf”);
    if (!fileBrochure.exists())
    {
    CopyAssetsbrochure();
    }
    Intent intent = new Intent(this, MyPdfViewerActivity.class);
    /** PDF reader code */
    File file = new File(Environment.getExternalStorageDirectory().getPath() + “/” + “abc.pdf”);

    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, file);

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try
    {
    getApplicationContext().startActivity(intent);
    }
    catch (ActivityNotFoundException e)
    {
    Toast.makeText(this, “NO Pdf Viewer”, Toast.LENGTH_SHORT).show();
    }
    }

    //method to write the PDFs file to sd card
    private void CopyAssetsbrochure() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try
    {
    files = assetManager.list(“”);
    }
    catch (IOException e)
    {
    Log.e(“tag”, e.getMessage());
    }
    for(int i=0; i<files.length; i++)
    {
    String fStr = files[i];
    if(fStr.equalsIgnoreCase("abc.pdf"))
    {
    InputStream in = null;
    OutputStream out = null;
    try
    {
    in = assetManager.open(files[i]);
    out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
    copyFile(in, out);
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
    break;
    }
    catch(Exception e)
    {
    Log.e("tag", e.getMessage());
    }
    }
    }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
    out.write(buffer, 0, read);
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s