Syncfusion PDF:将图像添加到 pdf 在 iOS 中有效,但在 android 中无效

Syncfusion PDF : Adding image to pdf works in iOS but not in android

我正在使用 Syncfusion 和 Xamarin Forms 即时创建 PDF。在 iOS 中一切顺利,但在 Android 中无法找到图像(徽标)。

关注了 syncfusion PDF 入门,但即便如此它也无法正常工作。图片位于 android 的 resource/drawable。我也确实在资产中添加了它。

这是 xamarin 表单中的工作代码 iOS

//Load the image 
Stream imageStream = System.IO.File.OpenRead("logo.jpg");
//Load the image from the stream 
PdfBitmap image = new PdfBitmap(imageStream); 

不幸的是,即使这样在 Android

上也不起作用
//Load the image 
Stream imageStream = DependencyService.Get<ISave>().LoadFromFile(@"logo.jpg");
//Load the image from the stream 
PdfBitmap image = new PdfBitmap(imageStream); 

非常感谢您的帮助,因为我已经坚持了 3 天了。

我刚刚关注了 the syncfusion PDF getting started,我认为你把图片添加到了错误的地方。该图像应保存在 Xamarin.Forms 项目 中,可以使用以下代码片段访问它:

 Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("GettingStarted.Assets.logo.jpg");

GettingStarted.Assets.logo.jpg

GettingStarted 表示您的项目名称。

Assets表示这个flooder里面的图片

logo.jpg表示图片名称

确保将您的图像设置为 "embedded resource" 和 "Always copy"

我这里加个scrrenshot说明一下:

进一步分析Xamarin中的“从drawable文件夹加载图像”Android,我们发现我们无法直接从drawable文件夹加载图像。而不是我们可以从资源中获取。请找到下面的代码片段以获取更多详细信息,

Xamarin.Android:

var context = Android.App.Application.Context;
using (var drawable = 
Xamarin.Forms.Platform.Android.ResourceManager.GetDrawable(context, fileName))
using (var bitmap = ((BitmapDrawable)drawable).Bitmap)
{
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
bitmap.Recycle();
return stream.ToArray();
}

我们已经创建了相同的示例,可以从下面下载 link,

Sample for loading images from resource