Search code examples
c#androidpdfxamarin.androidmupdf

Xamarin Android view pdf in layout using Mupdf


I want to use MuPDF reader for my Xamarin Android project . I am attempting to view a PDF in my relative layout

Here is my Relative layout code

  <RelativeLayout
   android:id="@+id/mupdf_wrapper"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
  </RelativeLayout>

and here is main acitivity

SetContentView(Resource.Layout.Main);

        RelativeLayout mupdfWrapper = FindViewById<RelativeLayout>(Resource.Id.mupdf_wrapper);



        MuPDFCore core = new MuPDFCore(this, "test.pdf");
        MuPDFReaderView reader = new MuPDFReaderView(this);
        reader.Adapter = new MuPDFPageAdapter(this, new FilePicker.IFilePickerSupport() , core);
        mupdfWrapper.AddView(reader);

        mupdfWrapper.AddView(reader);

But i am getting Error here

"cannot create an istance of the abstract class or interface 'File picker .iflepickersupport"

Can anyone help me resolve this issue please.

Thanks in advance.


Solution

  • If you don't have use of FilePicker.IFilePickerSupport() then set it null like

    reader.Adapter = new MuPDFPageAdapter(this, null , core);
    

    and second thing is that your code is very helpful for me there you are facing problem but your problem is my solution in my project so thank you. and try it it will work and i am using it in my code it's work for me. and last this thing is that sorry for my english.

         protected override void OnCreate(Bundle savedInstanceState)
         {
                    base.OnCreate(savedInstanceState);
                    File fileToDisplay = (File)fileFromAsset(this, "test.pdf");
                    fileToDisplay.SetWritable(true);
                    RelativeLayout mupdfWrapper = FindViewById<RelativeLayout>(Resource.Id.mupdf_wrapper);
                    MuPDFCore core = new MuPDFCore(this, fileToDisplay.AbsolutePath);
    
                    MuPDFReaderView reader = new MuPDFReaderView(this);
                    MuPDFPageAdapter adapter = new MuPDFPageAdapter(this, null, core);
                    reader.SetAdapter(adapter);
                    mupdfWrapper.AddView(reader);
        }
    
        private object fileFromAsset(Context context, string assetName)
        {
            File outFile = new File(context.CacheDir, assetName);
            copy(context.Assets.Open(assetName), outFile);
            return outFile;
        }
    
        private void copy(Stream inputStream, File output)
        {
            OutputStream outputStream = null;
            var bufferedInputStream = new BufferedInputStream(inputStream);
            try
            {
                outputStream = new FileOutputStream(output);
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = bufferedInputStream.Read(bytes)) != -1)
                {
                    outputStream.Write(bytes, 0, read);
                }
            }
            finally
            {
                try
                {
                    if (inputStream != null)
                    {
                        inputStream.Close();                  
                        inputStream.Dispose();
                        inputStream = null;
                    }
                }
                finally
                {
                    if (outputStream != null)
                    {
                        outputStream.Close();                        
                        outputStream.Dispose();
                        outputStream = null;
                    }
                }
            }
        }