How to render a PDF file in Android

后端 未结 9 942
猫巷女王i
猫巷女王i 2020-11-22 02:09

Android does not have PDF support in its libraries. Is there any way to render PDF files in the Android applications?

9条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:47

    There is no anyway to preview pdf document in Android webview.If you want to preview base64 pdf. It requires to third-party library.

    build.Gradle

    compile 'com.github.barteksc:android-pdf-viewer:2.7.0'
    

    dialog_pdf_viewer

    
    
    
    
    
    
        
    
        
    
            
    
        
    
        
    
        
    
    
    

    DailogPDFViewer.java

    public class DialogPdfViewer extends Dialog {
        PDFView pdfView;
        byte[] decodedString;
    
        public interface OnDialogPdfViewerListener {
            void onAgreeClick(DialogPdfViewer dialogFullEula);
    
            void onCloseClick(DialogPdfViewer dialogFullEula);
        }
    
        public DialogPdfViewer(Context context, String base64, final DialogPdfViewer.OnDialogPdfViewerListener onDialogPdfViewerListener) {
            super(context);
    
            setContentView(R.layout.dialog_pdf_viewer);
            findViewById(R.id.dialog_pdf_viewer_close).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDialogPdfViewerListener.onCloseClick(DialogPdfViewer.this);
                }
            });
    
            findViewById(R.id.dialog_pdf_viewer_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDialogPdfViewerListener.onAgreeClick(DialogPdfViewer.this);
                }
            });
    
            decodedString = Base64.decode(base64.toString(), Base64.DEFAULT);
    
            pdfView = ((PDFView) findViewById(R.id.pdfView));
            pdfView.fromBytes(decodedString).load();
    
            setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                        onDialogPdfViewerListener.onCloseClick(DialogPdfViewer.this);
                    }
                    return true;
                }
            });
    
        }
    }
    

提交回复
热议问题