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.
            Asked
            
        
        
            Active
            
        
            Viewed 1,779 times
        
    2
            
            
        - 
                    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 Answers
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
 
- 
                    Are you sure you can open a file with **.doc /.docx** extension in `webView`? – Rahul Sharma Jun 05 '18 at 05:32
 - 
                    yes its working fine if you don't want to use any third party view than you have no choice – Milan Pansuriya Jun 05 '18 at 05:34
 - 
                    
 - 
                    Thank you for your response but for some I am getting an error saying Refusing to load invalid URL – DRayen Jun 05 '18 at 05:55
 - 
                    
 - 
                    I actually tried the sample url in the answer String doc = ""; **This is the response I get** Refusing to load invalid URL: %3Ciframe%20src%3D%27http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true'width='100%' height='100%' style='border: none;'> – DRayen Jun 05 '18 at 05:59
 - 
                    set this url https://docs.google.com/gview?embedded=true&url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt – Milan Pansuriya Jun 05 '18 at 06:03
 - 
                    
 - 
                    Thanks Milan. That works. But unfortunately my actual document url is an aws url. So it is not displaying the document form load url. – DRayen Jun 05 '18 at 06:23
 - 
                    Milan. How do I do that. Is it from the server side or within the application ? Sorry I am new to android – DRayen Jun 05 '18 at 09:49
 - 
                    
 - 
                    other than that is there a way ? Because I only get the url from the server side. – DRayen Jun 05 '18 at 09:56
 - 
                    
 
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