10

I want to implement a PDF reader in the application that I am doing, I have found several APIs, but none of them were open source.

Does any of you guys know a good free alternative?


Slight adaptation of Dipak Keshariya's solution made by the OP

First Class

package android.pdf.reader;

import java.io.File;
import java.io.FilenameFilter;

import net.sf.andpdf.pdfviewer.PdfViewerActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class First extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        File images = Environment.getExternalStorageDirectory();
        File[] imagelist = images.listFiles(new FilenameFilter()
        {  
                public boolean accept(File dir, String name)  
                {  
                        return ((name.endsWith(".pdf")));
                }  
        }); 
        String[] pdflist = new String[imagelist.length]; 
        for(int i = 0;i<imagelist.length;i++)
        {
                pdflist[i] = imagelist[i].getName();
        }
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
            super.onListItemClick(l, v, position, id);
            Object[] imagelist;
            String path = ((File) imagelist[(int)id]).getAbsolutePath();
            openPdfIntent(path);
    }

    private void openPdfIntent(String path) 
    {
        try
        {
          final Intent intent = new Intent(First.this, Second.class);
          intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
          startActivity(intent);
        }
        catch (Exception e) 
        {
          e.printStackTrace();
        }
    }

}

Second Class

package android.pdf.reader;

import net.sf.andpdf.pdfviewer.PdfViewerActivity;
import android.os.Bundle;

public class Second extends PdfViewerActivity 
{

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

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;
}
}
Boken
  • 4,825
  • 10
  • 32
  • 42

3 Answers3

26

Use below code for that.

First.java

public class First extends ListActivity {

    String[] pdflist;
    File[] imagelist;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        File images = Environment.getExternalStorageDirectory();
        imagelist = images.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return ((name.endsWith(".pdf")));
            }
        });
        pdflist = new String[imagelist.length];
        for (int i = 0; i < imagelist.length; i++) {
            pdflist[i] = imagelist[i].getName();
        }
        this.setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, pdflist));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String path = imagelist[(int) id].getAbsolutePath();
        openPdfIntent(path);
    }

    private void openPdfIntent(String path) {
        try {
            final Intent intent = new Intent(First.this, Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Second.java

public class Second extends PdfViewerActivity {

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

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

And declared both activities into your manifest file.

bluish
  • 26,356
  • 27
  • 122
  • 180
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • I'm trying it, the entire project, but does not work https://github.com/jblough/Android-Pdf-Viewer-Library/zipball/master –  Jun 22 '12 at 10:48
  • don't use this, only use above link's answer because it is useful in my case and if u have any query then tell me i will give you source code of that and the above link is for read the pdf file from sdcard. – Dipak Keshariya Jun 22 '12 at 10:51
  • First thank you for answering so quickly. Do I think you say to look at the code which put # SPK? But it is incomplete, missing the main activity declares, etc... –  Jun 22 '12 at 10:56
  • In Above link, the First.java is main launcher activity and another one is default activity file – Dipak Keshariya Jun 22 '12 at 10:57
  • final Intent intent = new Intent(First.this, Second.class); intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path); startActivity(intent); But here i see PdfViewerActivity.class, first.class and Second.class? –  Jun 22 '12 at 11:01
  • Are you add the PDFViewer.jar file in your java build path? – Dipak Keshariya Jun 22 '12 at 11:03
  • yes, but for example the method "setListAdapter(ArrayAdapter)" is undefined :S –  Jun 22 '12 at 11:09
  • are you writing this line into click event or onCreate method? – Dipak Keshariya Jun 22 '12 at 11:11
  • Yes, i put in the post my 2 class –  Jun 22 '12 at 11:13
  • i don't understand your last comment, please give some more information. – Dipak Keshariya Jun 22 '12 at 11:14
  • I have edited the main post, so you can see the status of my project. I'm sorry if I explained bad or my English doesn't make much sense –  Jun 22 '12 at 11:17
  • Please see my edited answer and your code is perfect but in your main activity you are extends activity instead of listactivity. – Dipak Keshariya Jun 22 '12 at 11:29
  • Thank you, now I have no errors! Can I read pdf's resources or also from a url? –  Jun 22 '12 at 11:36
  • using above code you are read pdf from sdcard only, it is possible to change some code and read pdf from resources and url also but i don't know. – Dipak Keshariya Jun 22 '12 at 11:38
  • I'll have to find a way to read pdf's from a url. Finally you was very patient with me and I gave a great help, thanks! –  Jun 22 '12 at 11:42
  • 4
    This library is extremely slow for me - do you have this problem. I am not able to even open a pdf file - it shows pdf loading for a long time – Vrashabh Irde Jul 25 '12 at 10:25
  • @DipakKeshariya is it possible to set page to view before it loads the PDF File to screen? – Christian Eric Paran Jan 13 '13 at 00:46
  • @DipakKeshariya can you provide me pdfreading code on jolapara.pankaj@gmail.com i reqiured it if possible then pls mail me. Thanks – PankajAndroid Jan 27 '13 at 09:50
  • 1
    @DipakKeshariya to display pdf our PDF file should be in Sd card ? or we can display from assets folder? .. in my case logcat show me that 03-06 11:02:10.695: I/PDFVIEWER(5712): ST='file 'file:///android_asset/publicatios/rapportActivite.pdf' not found' – Swap-IOS-Android Mar 06 '13 at 10:04
  • 1
    @DipakKeshariya It display RestoreInstance in Red line.. i think problem there. – Swap-IOS-Android Mar 06 '13 at 10:24
  • @DipakKeshariya is it possible to set pinch to zoom functionality? – Krunal Desai Jul 29 '13 at 07:48
  • @DipakKeshariya only include of jar file an error shown at PdfViewerActivity – Bald bcs of IT Jul 31 '13 at 10:29
  • 1
    @Swap-IOS-Android If you are testing this code using emulator and reading the file from sdcard, then simply push the pdf file into the mnt/sdcard folder in the file explorer and run the app again, it will start working. – Sarvan Sep 19 '13 at 06:27
  • this is working fine,but it doesn't provide pinch zoom facility.Can we archive pinch zoom ficility for this example(pdf viewer) – asliyanage Oct 07 '14 at 10:24
  • Hi dipak, i implimented this,but my problem is how to impliment pinch zoom facility for this? – asliyanage Oct 08 '14 at 13:54
  • @DipakKeshariya from where we can get `PDFViewer.jar` ? – Mehul Joisar Oct 30 '14 at 14:44
  • How can we extract text from that PDF file? – Shailendra Madda Apr 23 '15 at 13:37
3

There is a very good post about this on SO. In particular check out the answer given by Commons.Ware, it answers your question.

Following your comments I have added the links here from the SO post I mentioned above (source for projects you could not find):

So "checkout" or clone the repositories to your local file system to browse the code. As I mentioned in my comment check the licence of each library before you go any further to see if what you can and cannot do with the code.

Community
  • 1
  • 1
Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • Thanks for response, But these links only see to download apk's. I need the code –  Jun 22 '12 at 09:19
  • read that post I can see atleast 2 if not 3 links to projects hosted on google code and the source being available. – Orlymee Jun 22 '12 at 09:21
  • http://www.dataviz.com/products/documentstogo/android/ http://www.cerience.com/products/reader/android http://andpdf.sourceforge.net/ http://code.google.com/p/apv/ These are all the links that I found, but I don't see any code to download on these websites –  Jun 22 '12 at 09:25
  • try the source tab on the ones hosted on google code. You can clone the repository to your machine. Check the licence to see what limitations are there for use of the library. so for apv (hosted at google code) you need to use Mercurial. For AndPdf hosted at sourceforge it is using SVN. again try reading through the project pages. – Orlymee Jun 22 '12 at 09:37
  • There's no answer by Commons.Ware.. please refer to it with a link to the answer. – bluish May 02 '14 at 07:49
  • Is there any method by which this can be integrated inside our project it self without using any third part project? – Nitesh Verma Nov 11 '14 at 08:44
3

This one works for me.

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; }
enter code here

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

And you can download the source code from this link. LINK

Hope this helps :)

Ankit Arora
  • 117
  • 4