我正在创建包括表格和图像的pdf文件。问题是我 可以在第一页上看到图像,但在第二页上看不到 页。
使用Android Studio 3.1.3
和itextg-5.5.8
public 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文件。问题是我 可以在第一页上看到图像,但在第二页上看不到 页面。
答案 0 :(得分:0)
通过使用Image.getInstance(字节[]符号)修复。
case 3: // tick
try{
Image tick = Image.getInstance(sign); // byte[] sign
tick.scaleAbsolute(100f,18f);
tick.setAlignment(Element.ALIGN_CENTER);
PdfPCell cell = new PdfPCell();
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.addElement(tick);
table.addCell(cell);
}catch (Exception e) {
e.printStackTrace();
}
break;
代替:
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());
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: