gpt4 book ai didi

android图像没有出现在iText pdf中

转载 作者:行者123 更新时间:2023-11-29 01:29:58 25 4
gpt4 key购买 nike

我正在尝试通过 android 平台中的 iText 库将图像转换为 Pdf 格式。我能够在所需位置生成 Pdf,但图像不在 pdf 中,即生成空白 pdf。下面是我的代码。如果您发现任何错误,请纠正我。我将“itextg-5.5.4”包含在“libs”文件夹中并作为库包含在内。代码中没有编译时错误。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
photo = extras.getParcelable("data");
File outFile = new File(pdfFolder,imageName);//pdfFolder exists at a location and imageName=timestamp
FileOutputStream fos = new FileOutputStream(outFile);
photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);

Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);

File myFile = new File(pdfFolder , timeStamp + ".pdf");

OutputStream output = new FileOutputStream(myFile);

//Step 1
Document document = new Document(PageSize.A4, 25, 25, 30, 30);;

//Step 2
PdfWriter writer=PdfWriter.getInstance(document, output);

//Step 3
document.open();

//Step 4 Add content

document.add(new Paragraph("Simple Image"));

ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image image=Image.getInstance(stream.toByteArray());

document.add(image);
document.close();
output.close();

最佳答案

我自己找到了如下解决方案。已替换

 Image image=Image.getInstance(stream.toByteArray());

 Image image=Image.getInstance(outFile.toString());

原因:Image.getInstance("ImageName")。我使用的是无法识别图像的 ByteArray。

关于android图像没有出现在iText pdf中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com