我正在创建一个应用程序,按钮点击我打印pdf上的所有内容为此目的我使用itextpdf就字符串而言它被打印但是我是无法打印图像我做了一些研究,但没有得到一个适当的解决方案,除了图像一切都工作正常 这是我制作pdf的代码
private Image addImageToPdf(Uri uri) {
Image image = null;
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
image = Image.getInstance(stream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} catch (BadElementException e) {
e.printStackTrace();
} finally {
return image;
}
}
public void addTitlePage(Document document) throws DocumentException
{
// Font Style for Document
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD| Font.UNDERLINE, BaseColor.GRAY);
Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
// Start New Paragraph
Paragraph prHead = new Paragraph();
// Set Font in this Paragraph
prHead.setFont(titleFont);
// Add item into Paragraph
prHead.add("Infomedia\n");
//prHead.add("\n");
prHead.setAlignment(Element.ALIGN_CENTER);
Paragraph cat = new Paragraph();
cat.setFont(catFont);
cat.add("\n");
cat.add("Report\n");
cat.add("\n");
cat.setAlignment(Element.ALIGN_CENTER);
// Add all above details into Document
document.add(prHead);
document.add(cat);
document.add(table);
/* Header values*/
table = new PdfPTable(2);
cell1 = new PdfPCell(new Phrase("Category"));
cell2 = new PdfPCell(new Phrase("Values"));
cell1.setVerticalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_LEFT);
cell1.setBorder(Rectangle.NO_BORDER);
cell1.setPadding(5);
cell2.setBorder(Rectangle.NO_BORDER);
cell2.setPadding(5);
cell1.setBackgroundColor(BaseColor.GRAY);
cell2.setBackgroundColor(BaseColor.GRAY);
/*Table values*/
cell3 = new PdfPCell(new Phrase("From"));
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setBorder(Rectangle.NO_BORDER);
cell3.setPadding(5);
cell4 = new PdfPCell(new Phrase(arrival));
cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
cell4.setBorder(Rectangle.NO_BORDER);
cell4.setPadding(5);
cell5 = new PdfPCell(new Phrase("To"));
cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5.setBorder(Rectangle.NO_BORDER);
cell5.setPadding(5);
cell6 = new PdfPCell(new Phrase(destination));
cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6.setBorder(Rectangle.NO_BORDER);
cell6.setPadding(5);
cell7 = new PdfPCell(new Phrase("Travel Purpose"));
cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
cell7.setBorder(Rectangle.NO_BORDER);
cell7.setPadding(5);
cell8 = new PdfPCell(new Phrase(travel_type));
cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
cell8.setBorder(Rectangle.NO_BORDER);
cell8.setPadding(5);
cell9 = new PdfPCell(new Phrase("Work Description"));
cell9.setHorizontalAlignment(Element.ALIGN_LEFT);
cell9.setBorder(Rectangle.NO_BORDER);
cell9.setPadding(5);
cell10 = new PdfPCell(new Phrase(workdescription));
cell10.setHorizontalAlignment(Element.ALIGN_LEFT);
cell10.setBorder(Rectangle.NO_BORDER);
cell10.setPadding(5);
cell11 = new PdfPCell(new Phrase("Expense Given"));
cell11.setHorizontalAlignment(Element.ALIGN_LEFT);
cell11.setBorder(Rectangle.NO_BORDER);
cell11.setPadding(5);
cell12 = new PdfPCell(new Phrase(expense_amount));
cell12.setHorizontalAlignment(Element.ALIGN_LEFT);
cell12.setBorder(Rectangle.NO_BORDER);
cell12.setPadding(5);
cell13 = new PdfPCell(new Phrase("Pass"));
cell13.setHorizontalAlignment(Element.ALIGN_LEFT);
cell13.setBorder(Rectangle.NO_BORDER);
cell13.setPadding(5);
cell14 = new PdfPCell(new Phrase(addImageToPdf(imageView)));
cell14.setHorizontalAlignment(Element.ALIGN_LEFT);
cell14.setBorder(Rectangle.NO_BORDER);
cell14.setPadding(5);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
table.addCell(cell12);
// add table into document
document.add(table);
// Create new Page in PDF
document.newPage();
//Toast.makeText(this, "PDF File is Created.", Toast.LENGTH_LONG).show();
}
Here is my code getting image to imageview
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY && resultCode == RESULT_OK) {
if (data != null) {
Uri path = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Work.this, "Failed!", Toast.LENGTH_SHORT).show();
}
}
}
I'm also converting it to string for uploading it to database i don't know if it is useful for saving it in pdf
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
byte[] imgBytes = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgBytes,Base64.DEFAULT);
}
答案 0 :(得分:0)
首先使用下面给出的代码从uri检索图像
private Image addImageToPdf(Uri uri) {
Image image = null;
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), uri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
image = Image.getInstance(stream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} catch (BadElementException e) {
e.printStackTrace();
} finally {
return image;
}
}
现在进一步传递此图像以添加到PDF Cell
public PdfPCell createCellWithImage(Image content, int colspan, int rowspan, int border, int alignment) {
PdfPCell cell = new PdfPCell(new Phrase("", font));
cell.setPadding(10);
cell.addElement(content);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setBorder(border);
cell.setHorizontalAlignment(alignment);
return cell;
}