50

I am making an application which require to open pdf.

I also have some pdf in asset folder so i am not able to open it in webview directly.

By default android does not support pdf.

Is there any API that works on android(except MuPdf) ??

My device does not have any pdf reader installed so ACTION VIEW is not helpful for me

Following is not working.......

How to render a PDF file in Android

Open asset file pdf in application

Can you suggest me any good api?

MAC
  • 15,799
  • 8
  • 54
  • 95

3 Answers3

62

I've simply done that using PdfViewer.jar (download it with the blue button) and making a code like below.

First.java

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

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

Hope this helps you lot. Try this. Don't forgot to add your Second.java in your manifest. Add some drawables whatever it requires in second.java with your drawables. And, Refer the example from GitHub

bluish
  • 26,356
  • 27
  • 122
  • 180
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Thanks @SpK my file is in asset folder and i want to read it from asset --- file:///android_asset/test.pdf.... i have tried this....... openPdfIntent("file:///android_asset/test.pdf"); ......logcat.... ST='file 'file:///android_asset/test.pdf' not found' – MAC Apr 27 '12 at 14:43
  • 3
    +1 but its also very slow and displays JUNK characters.... – MAC Apr 27 '12 at 14:51
  • @spk hello sir I am using the solution you mentioned but getting this error E/AndroidRuntime(23933): java.lang.NoClassDefFoundError: com.pdf.view.Second can you please help me – Nitin May 25 '12 at 10:53
  • @Nitin Click your project -> right click -> properties -> Java build path -> Order and export tab -> here the library should be first and should checked. – Praveenkumar Jul 31 '12 at 09:21
  • Hi Praveen I have tried this, but is not working efficiently. Almost 80 precent time it fails to load the file. Can you suggest any other good library? – Chandra Sekhar Dec 04 '12 at 10:35
  • 1
    I tried this but it just keep on loading or show nothing. Any idea what's wrong...??? – Khawar Raza Dec 27 '12 at 07:58
  • @KhawarRaza i guess there's a problem on your file location – Cjames May 08 '13 at 03:09
  • 1
    For Zoom out problem please reffer this post http://stackoverflow.com/questions/11259131/adjust-the-screen-in-pdfviewer/16354094#16354094 – Cjames May 08 '13 at 03:11
  • I cannot get this jar file to work properly. I have imported and added it to the path. But when i try to make a first and second class, eclipse cannot find the reference to PdfViewerActivity? – John Baum Jun 04 '13 at 14:08
  • @JohnBaum Try with Github link. And, restart your eclipse then try it out! – Praveenkumar Jun 06 '13 at 04:14
  • It is working but problem with images in pdf and yes processing slow.. still 1 vote up – Mohd Mufiz Sep 13 '13 at 06:01
  • 1
    @MohdMufiz See, its just sample. You have to implement it with your requirements and speed enhancement is with your hands :) – Praveenkumar Sep 13 '13 at 06:59
  • @Praveen when I am trying this sample I got java.nio.BufferUnderflowException why is it so?could you help me – poojagupta Feb 04 '14 at 12:53
  • what is imagelist and pdflist in this code ? – JayDeep Nimavat Feb 19 '14 at 09:58
  • @Praveen I appreciate your work but still not getting significant answer as it is taking time in loading Pdf page. One more thing I have put PDF file in assets folder. I have to view pdf file from Asset folder. Any solution? – Abhishek Tamta Sep 09 '14 at 13:43
  • Me too tried this but it just keep on loading or showing nothing. – Shailendra Madda Apr 21 '15 at 07:28
  • After I changing file path can able to read it but How can I get text from that PDF? – Shailendra Madda Apr 21 '15 at 07:59
  • **Please Help me to solve this issue : http://stackoverflow.com/questions/30045927/how-to-open-a-pdf-file-using-pdfviewer-in-android** – SHIDHIN TS May 06 '15 at 06:42
  • Hi @Praveen , I used the same library, but I'm having problem when my pdf file has scanned images. It displays broken pixelated images. Is there a solution on this? or is there any modification i need to do? thank you – Kairi San Apr 12 '16 at 09:57
  • Hello @KairiSan This is a simple & basic library which will just show the PDF files. You may have to implement this library as per your needs or you have to go for any other library. – Praveenkumar Apr 12 '16 at 10:06
  • thank you @Praveen for answering. I used other library instead. – Kairi San Apr 15 '16 at 08:08
9

Some phones (like the Nexus One) come with a version of Quickoffice pre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.

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

        Button button = (Button) findViewById(R.id.OpenPdfButton); 
        button.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                File file = new File("/sdcard/example.pdf"); 

                if (file.exists()) { 
                    Uri path = Uri.fromFile(file); 
                    Intent intent = new Intent(Intent.ACTION_VIEW); 
                    intent.setDataAndType(path, "application/pdf"); 
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

                    try { 
                        startActivity(intent); 
                    }  
                    catch (ActivityNotFoundException e) { 
                        Toast.makeText(OpenPdf.this,  
                            "No Application Available to View PDF",  
                            Toast.LENGTH_SHORT).show(); 
                    } 
                } 
            } 
        }); 
    } 
} 
bluish
  • 26,356
  • 27
  • 122
  • 180
Gautam Vasoya
  • 881
  • 1
  • 8
  • 16
1

I have never needed to do this, but you could probably use a library like iText to access the PDF file programmatically, and then display the PDF.

glen3b
  • 693
  • 1
  • 8
  • 22