2

I have looked all possible options on this. I have a requirement to display pdf & word document inside my android application. For certain security reasons I am not allowed to open the document in 3rd party application. But unfortunately the inbuilt android does not have inbuilt components to display word documents & the default inbuilt components to display pdf supports from API 21 & above (My application starts from API 19). Can any one shed some light on this issue, Because I couldn't find any solutions for this. Please note that I am looking for open source solutions.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
DRayen
  • 87
  • 1
  • 12
  • Possible duplicate of this https://stackoverflow.com/questions/14578530/how-to-open-display-documents-pdf-doc-without-external-app – Ashish Kumar Jun 05 '18 at 05:30

2 Answers2

3

open pdf/Doc in webview

     String pdf = "http://www.pc-hardware.hu/PDF/konfig.pdf";
   String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
              width='100%' height='100%' 
              style='border: none;'></iframe>";
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.loadUrl(doc);
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
0

You could use possibly a third party library to show it inside a view like this

<RelativeLayout android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/colorPrimaryDark"
    android:text="View PDF"
    android:textColor="#ffffff"
    android:id="@+id/tv_header"
    android:textSize="18dp"
    android:gravity="center"></TextView>

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_below="@+id/tv_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


</RelativeLayout>

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    pdfView= (PDFView)findViewById(R.id.pdfView);
    displayFromAsset(SAMPLE_FILE);
}

private void displayFromAsset(String assetFileName) {
    pdfFileName = assetFileName;

    pdfView.fromAsset(SAMPLE_FILE)
            .defaultPage(pageNumber)
            .enableSwipe(true)

            .swipeHorizontal(false)
            .onPageChange(this)
            .enableAnnotationRendering(true)
            .onLoad(this)
            .scrollHandle(new DefaultScrollHandle(this))
            .load();
}


@Override
public void onPageChanged(int page, int pageCount) {
    pageNumber = page;
    setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
}


@Override
public void loadComplete(int nbPages) {
    PdfDocument.Meta meta = pdfView.getDocumentMeta();
    printBookmarksTree(pdfView.getTableOfContents(), "-");

}

public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}
Kashif K.
  • 88
  • 1
  • 9
  • I guess he already said he doesn't want to use any 3rd party library. – Rahul Sharma Jun 05 '18 at 05:37
  • not want to open in third party application, but could use a third party library to open inside his app – Kashif K. Jun 05 '18 at 05:44
  • Thanks for the response Kashif. I already checked on this. The issue is it only supports pdf & I have word documents as well. So I thought of going for a common solution where I can use it for both – DRayen Jun 05 '18 at 05:57