我成功地使用iTextSharp并在其中创建了一个表格,成功创建了一个pdf文件。我正在尝试从可绘制文件夹的顶部添加信头图像
我尝试使用添加它
iTextSharp.text.Image _image = iTextSharp.text.Image.GetInstance(Resource.Drawable.LetterHead);
但是我遇到一个错误,无法将'int'转换为'iTextSharp.text.Image'
谢谢你...
这是因为Resource.Drawable.LetterHead是一个int。实际上,所有Resource。“ something”都是整数。您需要使用以下方法获取drawable:
ContextCompat.GetDrawable(this, Resource.Drawable.LetterHead);
现在,我假设iTextSharp.text.Image是一个图像,例如。 png或jpg。这意味着您需要将其转换为可接受的格式,可能是位图。
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
((代码由kabuko撰写,原始文章:How to convert a Drawable to a Bitmap?代码是Java语言,但转换到Xamarin的时间不会太长]
另一个选择是直接进入位图。
Resources res = getContext().getResources();
int id = R.drawable.image;
Bitmap b = BitmapFactory.decodeResource(res, id);
(再次java)