【发布时间】:2019-06-24 19:44:54
【问题描述】:
使用带有 itext pdf 的 Android Studio 创建带有图像的表格时出现问题
我正在创建包含表格和图像的 pdf。问题是我 第一页可以看图片,第二页看不到 页。
-
使用
Android Studio 3.1.3和itextg-5.5.8public void createPDF() throws IOException{ EditText pdfREFNo=(EditText)findViewById(R.id.etReferenceNo); FileName = pdfREFNo.getText().toString()+".pdf"; outpath = Environment.getExternalStorageDirectory() + "/PDF/"; File docsFolder = new File(Environment.getExternalStorageDirectory() + "/PDF"); boolean isPresent = true; if (!docsFolder.exists()) { isPresent = docsFolder.mkdir(); } if (isPresent) { outpath = Environment.getExternalStorageDirectory() + "/PDF/"; } else { } try { PdfWriter.getInstance(doc, new FileOutputStream(outpath+FileName)); Document doc = new Document(PageSize.A4,20f,20f,50f,50f); Font fontHeader = new Font(FontFamily.HELVETICA, 11, Font.BOLD, new BaseColor(0, 0, 0)); Font fontCell = new Font(FontFamily.HELVETICA, 10, Font.NORMAL, new BaseColor(0, 0, 0)); Font fontCell_white = new Font(FontFamily.HELVETICA, 10, Font.NORMAL, new BaseColor(255, 255, 255)); doc.open(); PdfPTable table1 = new PdfPTable(8); table1.setWidthPercentage(100f); PdfPTable table_A = new PdfPTable(6); table_A.setWidthPercentage(100f); // convert image to byte array (arrTick) try { Drawable d = getResources().getDrawable(R.drawable.tick); Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); arrTick = BMP.getBytes(bitmap); } catch (Exception e) { e.printStackTrace(); } //Creating Table 1 insert_cell(table1,"Table 1 ",Element.ALIGN_LEFT,8,fontHeader,1 ,1, false, emptyArr, -1); insert_cell(table1, "Yes", Element.ALIGN_LEFT, 2, fontCell, 0 ,1, false, emptyArr, -1); insert_cell(table1, "No", Element.ALIGN_LEFT, 2, fontCell, 0 ,1, false, emptyArr, -1); insert_cell(table1, "Uncertain", Element.ALIGN_LEFT, 4, fontCell, 0 ,1, false, emptyArr, -1); if (cbYes.isChecked() == true) { insert_cell(table1, "", Element.ALIGN_CENTER, 2, fontCell, 0, 1, true, arrTick, 3); } else { insert_cell(table1, "", Element.ALIGN_CENTER, 2, fontCell_white, 0, 1, false, emptyArr, -1); } if (cbNo.isChecked() == true) { insert_cell(table1, "", Element.ALIGN_CENTER, 2, fontCell, 0, 1, true, arrTick, 3); } else { insert_cell(table1, "", Element.ALIGN_CENTER, 2, fontCell_white, 0, 1, false, emptyArr, -1); } if (cbUncertain.isChecked() == true) { insert_cell(table1, "", Element.ALIGN_CENTER, 4, fontCell, 0, 1, true, arrTick, 3); } else { insert_cell(table1, "", Element.ALIGN_CENTER, 4, fontCell_white, 0, 1, false, emptyArr, -1); } doc.add(table1); doc.newPage(); insert_cell(table_A,"Remark",Element.ALIGN_LEFT,6,fontHeader,1 ,1, false, emptyArr, -1); insert_cell(table_A, etRemarks.getText().toString(), Element.ALIGN_LEFT, 6, fontCell, 0 ,1, false, emptyArr, -1); insert_cell(table_A," ",Element.ALIGN_LEFT,6,fontCell_white,0 ,0, false, emptyArr, -1); insert_cell(table_A,"table_A",Element.ALIGN_LEFT,6,fontHeader,1 ,1, false, emptyArr, -1); insert_cell(table_A, "Yes", Element.ALIGN_LEFT, 2, fontCell, 0 ,1, false, emptyArr, -1); insert_cell(table_A, "No", Element.ALIGN_LEFT, 2, fontCell, 0 ,1, false, emptyArr, -1); insert_cell(table_A, "Pending", Element.ALIGN_LEFT, 2, fontCell, 0 ,1, false, emptyArr, -1); if (cbYes2.isChecked()) { insert_cell(table_A, "", Element.ALIGN_CENTER, 2, fontCell, 0, 1, true, arrTick, 3); } else { insert_cell(table_A, "", Element.ALIGN_CENTER, 2, fontCell_white, 0, 1, false, emptyArr, -1); } if (cbNo2.isChecked()) { insert_cell(table_A, "", Element.ALIGN_CENTER, 2, fontCell, 0, 1, true, arrTick, 3); } else { insert_cell(table_A, "", Element.ALIGN_CENTER, 2, fontCell_white, 0, 1, false, emptyArr, -1); } if (cbUncertain.isChecked()) { insert_cell(table_A, "", Element.ALIGN_CENTER, 2, fontCell, 0, 1, true, arrTick, 3); } else { insert_cell(table_A, "", Element.ALIGN_CENTER, 2, fontCell_white, 0, 1, false, emptyArr, -1); } doc.add(table_A); doc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } private void insert_cell(PdfPTable table, String text, int align, int colspan, Font font, int background, int border, boolean signFlag, byte[] sign, int type) throws IOException, BadElementException { if(text.trim().equalsIgnoreCase("") == false && signFlag == false) { PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font)); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPaddingLeft(5f); cell.setPaddingTop(3f); cell.setPaddingBottom(3f); cell.setPaddingRight(5f); if (background == 0) { cell.setBackgroundColor(BaseColor.WHITE); } else if (background == 1) { cell.setBackgroundColor(BaseColor.LIGHT_GRAY); } else if (background == 2) { cell.setBackgroundColor(BaseColor.BLACK); }else if (background == 3) { cell.setBackgroundColor(BaseColor.YELLOW); } if (border == 0) { cell.setBorder(PdfPCell.NO_BORDER); } else if (border == 1) { cell.setBorder(PdfPCell.BOX); } table.addCell(cell); } // to add empty cell if (text.trim().equalsIgnoreCase("") == true && signFlag == false) { PdfPCell cell = new PdfPCell(new Phrase("-", font)); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPaddingLeft(5f); cell.setPaddingTop(3f); cell.setPaddingBottom(3f); cell.setPaddingRight(5f); if (background == 0) { cell.setBackgroundColor(BaseColor.WHITE); } else if (background == 1) { cell.setBackgroundColor(BaseColor.LIGHT_GRAY); } else if (background == 2) { cell.setBackgroundColor(BaseColor.BLACK); }else if (background == 3) { cell.setBackgroundColor(BaseColor.YELLOW); } if (border == 0) { cell.setBorder(PdfPCell.NO_BORDER); } else if (border == 1) { cell.setBorder(PdfPCell.BOX); } table.addCell(cell); } // to add image if(text.trim().equalsIgnoreCase("") ==true && signFlag == true && sign != null) { Bitmap img1 = BMP.getImage(sign); Drawable d1 = new BitmapDrawable(img1); BitmapDrawable bitDw = ((BitmapDrawable) d1); Bitmap bmp = bitDw.getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream); Image image = Image.getInstance(stream.toByteArray()); switch (type) { case 1: // signature try{ image.scaleAbsolute(150f,70f); PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.addElement(image); table.addCell(cell); }catch (Exception e) { e.printStackTrace(); } break; case 2: // photo try{ image.setRotationDegrees(270f); PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.addElement(image); table.addCell(cell); }catch (Exception e) { e.printStackTrace(); } break; case 3: // tick try{ image.scaleAbsolute(100f,18f); image.setAlignment(Element.ALIGN_CENTER); PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.addElement(image); table.addCell(cell); }catch (Exception e) { e.printStackTrace(); } break; } }
我正在创建包含表格和图像的 pdf。问题是我 第一页可以看图片,第二页看不到 页面。
【问题讨论】: