Android 从drawable获取图像并使用iText添加到PDF
我想使用iText将图像添加到android PDF。我想在不首先将图像保存到SD卡的情况下实现这一点。我将图像放入res/drawable文件夹,但证明图像路径不工作,它引发FileNotFound异常。我的路径是这样的:Android 从drawable获取图像并使用iText添加到PDF,android,pdf,itext,drawable,Android,Pdf,Itext,Drawable,我想使用iText将图像添加到android PDF。我想在不首先将图像保存到SD卡的情况下实现这一点。我将图像放入res/drawable文件夹,但证明图像路径不工作,它引发FileNotFound异常。我的路径是这样的: String path = “res/drawable/myImage.png” Image image = Image.getInstance(path); document.add(image); 现在,请建议我一个解决方案,如何将正确的文件路径添加到getInsta
String path = “res/drawable/myImage.png”
Image image = Image.getInstance(path);
document.add(image);
现在,请建议我一个解决方案,如何将正确的文件路径添加到getInstance(…)方法。谢谢,当然那样不行 将图像移动到assets文件夹,以使用getassets()方法访问它
下面是使用iText将图像添加到PDF的代码,如果图像是动态的(即,如果无法在编译时将图像添加到资产文件夹中)
public void addImage(Document document,ImageView ivPhoto) throws DocumentException {
try {
BitmapDrawable drawable = (BitmapDrawable) ivPhoto.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();
Image image = Image.getInstance(imageInByte);
document.add(image);
}
catch(IOException ex)
{
return;
}
}
我为你的问题找到了解决办法。如果您想从drawable文件夹中获取图像并使用iText将其放入PDF文件,请使用以下代码:
try {
document.open();
Drawable d = getResources().getDrawable(R.drawable.myImage);
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bmp = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
这是我的代码,将图像设置在特定位置 将图像移动到assets文件夹,通过getassets()方法获取图像。 希望这对你有帮助
try {
InputStream ims = getAssets().open("header1.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(10f,750f);
image.scaleToFit(850,78);
document.add(image);
}
catch(IOException ex)
{
ex.printStackTrace();
return;
}
我无法将位图添加到文档添加方法,不支持类型位图:(资源应该优先于资源,我可以使用静态高度和宽度来显示将在pdf上显示的图像吗?谢谢,它解决了我的问题,现在我的代码工作良好:)工作非常好!有人知道如何从PDF文件中删除这个添加的图像吗???工作得很好!太棒了。工作得很有魅力!!
try {
FileInputStream in = new FileInputStream("input file uri");
PdfReader pdfReader = new PdfReader(in);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("output file uri"));
PdfContentByte content = pdfStamper.getOverContent(1);
Image deliverImg = Image.getInstance("image URI");
deliverImg.setAbsolutePosition(420f, 100f);
content.addImage(deliverImg);
pdfStamper.close();
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
try {
InputStream ims = getAssets().open("header1.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(10f,750f);
image.scaleToFit(850,78);
document.add(image);
}
catch(IOException ex)
{
ex.printStackTrace();
return;
}