我在android studio (Java)中做了一个应用程序,它可以在文本框中填充一些简短的数据。此外,应用程序有两个按钮,一个用于从图库加载图像,另一个用于将完整数据保存到pdf。我可以成功保存所有文本数据,但加载图像有问题。图片是成功加载到应用程序,但我不知道热保存到pdf。图像作为ImageView对象加载。为了提到pdf部分,我使用了itext。
请帮助将ImageVIew对象保存到pdf文件的提示或代码。
发布于 2020-08-08 11:30:47
我也有同样的问题,但我可以解决。首先,我改变了拾取图像的方式,并将其替换为以下代码:
Intent getImage = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(getImage, GALLERY_REQUEST_CODE);然后,在类onActivityResult中,我使用以下代码将uri转换为位图:
if(requestCode==GALLERY_REQUEST_CODE && resultCode== RESULT_OK && data!=null){
Uri imageData = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(imageData,filePath,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePath[0]);
String myPath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(myPath);然后将位图传递给pdf。
pdf.addFoto(bitmap);最后,在pdf模板中,我使用此命令将图像放入表中:
public void addFoto (Bitmap u) {
try{
PdfPTable tabla = new PdfPTable(1);
tabla.setWidthPercentage(60);
tabla.setSpacingBefore(10);
tabla.setSpacingAfter(10);
Bitmap bmp = u;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
PdfPCell imageCell = new PdfPCell();
imageCell.addElement(image);
tabla.addCell(imageCell);
document.add(tabla);发布于 2020-05-14 19:52:01
文档中的示例
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();https://stackoverflow.com/questions/61796378
复制相似问题