如何在Android中使用iText将多个带文本的位图图像添加到表格中?
问题描述:
我想创建一个PDF文件,其中包含带有表格中文本的条形码图像。我无法以这种格式创建。如何在Android中使用iText将多个带文本的位图图像添加到表格中?
PdfPTable table = new PdfPTable(qrCodeModelArrayList.size());
for (int i = 0; i < qrCodeModelArrayList.size(); i++) {
Bitmap bitmap = qrCodeModelArrayList.get(i).getQrBitMap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); //use the compression format of your need
InputStream is = new ByteArrayInputStream(stream.toByteArray());
Bitmap bmp = BitmapFactory.decodeStream(is);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream1);
Image bitmapImage = null;
try {
bitmapImage = Image.getInstance(stream1.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
PdfPCell cell = new PdfPCell();
cell.addElement(bitmapImage);
Paragraph p = new Paragraph(qrCodeModelArrayList.get(i).getQrCodeName());
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
table.addCell(cell);
}
使用此代码时,我也得到了输出如下:
我无法查看我的条码图像和所有列在同一行加入。我想要5列。
答
我已更新您的问题,以便显示实际问题:您正在尝试创建QRCode,应使用BarcodeQRCode
类来完成此操作。您应该使用placeBarcode()
方法将代码添加到PDF中。您无法使用createAwtImage()
方法,因为Android上没有AWT。我认为你目前使用的代码有一些图像相关的问题。 QRCode由矢量数据组成,并将其转换为有损JPEG格式(这是一个坏主意,因为条形码扫描仪在阅读JPG时会遇到麻烦,您应该使用PNG代替)。你也在压缩条形码;这对于可能是矢量格式的图像来说没有意义。
使用placeBarcode()
方法会将QRCode添加为矢量图像(使用PDF语法)。这比添加条形码作为光栅图像要好得多。
向我们显示您的代码。 –
这个问题已经在[官方FAQ]中得到了解答(http://developers.itextpdf.com/frequently-asked-developer-questions-7): [如何将图像和文本添加到同一个单元格?]( http://developers.itextpdf.com/content/best-itext-questions-stackoverview/tables/itext7-how-add-image-and-text-same-cell)如果需要iText 5的答案,请参见[old版本](http://developers.itextpdf.com/question/how-add-image-and-text-same-cell)。 –
@AmedeeVanGasse请检查我的代码 –