我正在使用ItextPdf生成PDF文件,但我在此行canvas.addImage(background, width, 0,0, height, 20, 430);
上收到异常,即无法解析类型java.awt.geom.AffineTransform
。它是从必需的.class
文件间接引用的。在这一行,我试图设置背景图像。请帮我解决这个例外。
public void createPDF() throws NumberFormatException, ParseException
{
list1.add("I-Tax Number : ");
list1.add("Category : ");
list1.add("Service : ");
list1.add("Number : ");
list1.add("Amount : ");
list1.add("Status : ");
list2.add(iTaxNumber);
list2.add("Bill Payment");
list2.add("Idea Postapid");
list2.add("9644212111");
list2.add("100");
list2.add("SUCCESS");
Font trfont = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLDITALIC,
new BaseColor(130, 130, 140));
Font otherfont = new Font(FontFamily.TIMES_ROMAN, 12, Font.NORMAL,
new BaseColor(160, 160, 160));
Font datefont = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD,
new BaseColor(130, 130, 140));
Font thanksFont = new Font(FontFamily.TIMES_ROMAN, 14, Font.BOLDITALIC,
new BaseColor(130, 130, 140));
Document doc = new Document(new Rectangle(792, 612));
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "demo98989.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter docPdfWriter = PdfWriter.getInstance(doc, fOut);
Paragraph fromTotoDate = new Paragraph("Date : 25-oct-2015", datefont);
fromTotoDate.setAlignment(Element.ALIGN_RIGHT);
fromTotoDate.setIndentationRight(5);
doc.addAuthor("betterThanZero");
doc.addCreationDate();
doc.addProducer();
doc.addCreator("www.xyz.com");
doc.setPageSize(PageSize.A4);
doc.open();
PdfPTable table = setTable(list1, list2);
Paragraph trId = new Paragraph("Transaction Id : 889879899", trfont);
trId.setAlignment(Element.ALIGN_RIGHT);
trId.setIndentationRight(65);
Paragraph p = new Paragraph("\n\n\n\n");
Paragraph nextline = new Paragraph("\n");// for blank line
doc.add(fromTotoDate);
doc.add(p);
doc.add(trId);
int list1size = list1.size();
String size = String.valueOf(list1size);
Image trDetails_Icon;
Bitmap bmp = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.trreceipt);
ByteArrayOutputStream streamTrReceipt = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, streamTrReceipt);
trDetails_Icon = Image.getInstance(streamTrReceipt.toByteArray());
trDetails_Icon.scaleAbsolute(445f, 238f);
trDetails_Icon.setAbsolutePosition(76, 516);
doc.add(trDetails_Icon);
doc.add(nextline);
doc.add(table);
Paragraph thanktouMessage = new Paragraph("Thanks for Being with Us ! ", thanksFont);
thanktouMessage.setAlignment(Element.ALIGN_CENTER);
doc.add(nextline);
doc.add(thanktouMessage);
Font contFont = new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL,
new BaseColor(130, 130, 140));
doc.add(nextline);
Paragraph cont = new Paragraph("For more info contact us", contFont);
cont.setAlignment(Element.ALIGN_RIGHT);
cont.setIndentationRight(20);
doc.add(cont);
System.out.println("list2.get(1) = "+list2.get(1));
float width;
float height;
Image background;
Bitmap bmp1 = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.trans);
ByteArrayOutputStream streamTrReceipt1 = new ByteArrayOutputStream();
bmp1.compress(Bitmap.CompressFormat.PNG, 100, streamTrReceipt1);
System.out.println("list2.get(1) = "+list2.get(1)+"ELSE");
width = PageSize.A4.getWidth()-40;
height = (PageSize.A4.getHeight()/2)-25;
background = Image.getInstance(streamTrReceipt1.toByteArray());
PdfContentByte canvas = docPdfWriter.getDirectContentUnder();
canvas.addImage(background, width, 0,0, height, 20, 430);
Toast.makeText(getApplicationContext(), "Created...", Toast.LENGTH_LONG).show();
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally
{
doc.close();
}
}
答案 0 :(得分:1)
您使用的是错误的iText版本。您应该使用iTextG而不是"普通Java" iText版本。作为Android开发人员,您知道在Android上禁止使用java.awt
(和javax.nio
,...)类。
"普通Java" iText使用未在Android上列入白名单的类(例如,在PdfGraphics2D
类中)。这就是我们创建iTextG的原因。 iTextG与iText基本相同,只是我们已经删除了所有依赖于"禁止的类" (java.awt.geom.AffineTransform
是其中一个类。)
iTextG的功能略少(我们不得不放弃PdfGraphics2D
),但乍一看,我看不到代码中iTextG支持的任何内容。
长话短说:将iText替换为其Android端口iTextG,您的问题将得到解决。