【问题标题】:Xamarin Android view pdf in layout using MupdfXamarin Android 使用 Mupdf 在布局中查看 pdf
【发布时间】:2017-04-10 21:13:31
【问题描述】:

我想在我的 Xamarin Android 项目中使用 MuPDF 阅读器。 我正在尝试在我的相对布局中查看 PDF

这是我的相对布局代码

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

这里是主要活动

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

但我在这里遇到错误

“无法创建抽象类或接口'文件选择器.iflepickersupport”的实例

谁能帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 你遇到了什么错误?
  • 谢谢,这行错误 reader.Adapter = new MuPDFPageAdapter(this, new FilePicker.IFilePickerSupport() , core);它说“无法创建抽象类或接口'文件选择器.iflepickersupport'的实例”

标签: c# android pdf xamarin.android mupdf


【解决方案1】:

如果您没有使用 FilePicker.IFilePickerSupport() 则将其设置为 null 喜欢

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

第二件事是您的代码对我非常有帮助,您遇到了问题,但您的问题是我在项目中的解决方案,所以谢谢。 并尝试它会起作用,我在我的代码中使用它对我有用。 最后这件事对我的英语很抱歉。

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

【讨论】:

  • 谢谢 vishal,当我部署它时说“无法打开 test.pdf 文件”,所以你可以用另一种方式,这段代码对我不起作用
  • 所以你是从资产打开这个文件然后使用代码。我编辑我的答案。
  • 谢谢你,真的你帮了很多忙,但我仍然收到错误消息“MuPDFReaderView'不包含'SetAdapter'的定义,并且没有扩展方法'SetAdapter'接受类型的第一个参数'可以找到 MuPDFReaderView'(您是否缺少 using 指令或程序集引用?)"
  • 哦,对不起兄弟,对不起,我忘记提及使用 com.Artifex.MuPdf 版本只需从 nuget 包管理器添加新的最新版本即可。然后它就像使用 Artifex.MuPdf;
  • 当我尝试安装最新版本时,它显示“对象引用未设置为对象的实例”,所以我无法安装最新版本,但版本 1 对我有用,请提供任何帮助
猜你喜欢
  • 2016-12-24
  • 2016-09-09
  • 2012-04-21
  • 1970-01-01
  • 2016-11-03
  • 2016-12-24
  • 2019-08-09
  • 2014-12-21
  • 1970-01-01
相关资源
最近更新 更多