How to render PDF in Android

JavaAndroidPdf

Java Problem Overview


In my application I will receive a byte stream and convert it to a pdf file in the phone memory. How do I render that to a pdf? And show it on an activity?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

Open pdf file in webview.

public class MyPdfViewActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView mWebView=new WebView(MyPdfViewActivity.this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);
    setContentView(mWebView);
  }
}

Solution 3 - Java

Android-Lollipop (api 21) introduce a new API : PdfRenderer

This API allows you to create a Bitmap from a page in a PDF document.

Shortly :

  • get a seekable file descriptor from your pdf document :

      ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
    
  • create the PdfRenderer

      PdfRenderer renderer = new PdfRenderer(fd);
    
  • prepare the Bitmap

      Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_4444);
    
  • get the PdfRenderer.Page to render

      PdfRenderer.Page page = renderer.openPage(pageIndex);
    
  • render the page on the prepared bitmap

      page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    
  • now you can do what you want with the bitmap.

  • note that the 2 null args may allow you to clip some portion in the page and perform a transformation (using a Matrix) of the clip

  • there is another rendering mode : RENDER_MODE_FOR_PRINT. If you need this mode there are some guidelines to use it properly : here are the details.

Solution 4 - Java

This library is simple and works well: Android Pdf Viewer https://github.com/barteksc/AndroidPdfViewer

Old Reply...

> I think that Joan Zapata give us the better and simple solution: > > https://github.com/JoanZapata/android-pdfview > > I assure you that it works! > > > 1: https://github.com/JoanZapata/android-pdfview

Solution 5 - Java

For the local pdf files, you can render them through the third party libraries. for example, use the MuPDF library, its supported file types include PDF, PNG and JPEG.

One shortcoming of MuPDF is that it uses native library to fulfill the target, so it won't be easy to port the application on BlackBerry platform later.

Solution 6 - Java

To open a pdf from a byte array, you can use RadaeePDF, you can do the following into your activity:

private PDFReader m_vPDF = null;
private Document doc = new Document();

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Global.Init(this);

    m_vPDF = new PDFReader(this);
    doc.Close();

    int ret = m_doc.OpenMem(data, password);
        switch( ret )
        {
            case -1://need input password
                finish();
                break;
            case -2://unknown encryption
                finish();
                break;
            case -3://damaged or invalid format
                finish();
                break;
            case -10://access denied or invalid file path
                finish();
                break;
            case 0://succeeded, and continue
                break;
            default://unknown error
                finish();
                break;
        }

    m_vPDF.open(doc);
    
    setContentView( m_vPDF );
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeyView Question on Stackoverflow
Solution 1 - JavaTim KrygerView Answer on Stackoverflow
Solution 2 - JavaShankar AgarwalView Answer on Stackoverflow
Solution 3 - Javaben75View Answer on Stackoverflow
Solution 4 - JavaAlecsView Answer on Stackoverflow
Solution 5 - JavaZephyrView Answer on Stackoverflow
Solution 6 - JavaNermeenView Answer on Stackoverflow