gpt4 book ai didi

java - 无法解析类型 java.awt.geom.AffineTransform。它是从所需的 .class 文件中间接引用的

转载 作者:太空狗 更新时间:2023-10-29 14:05:11 31 4
gpt4 key购买 nike

我正在使用 ItextPdf 生成 PDF 文件,但我在这一行 canvas.addImage(background, width, 0,0, height, 20, 430); 上遇到异常,即类型 java.awt.geom.AffineTransform 无法解析。它是从所需的 .class 文件中间接引用的。在这条线上,我试图设置背景图像。请帮我摆脱这个异常(exception)。

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();
}
}

最佳答案

您使用的 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,您的问题将得到解决。

关于java - 无法解析类型 java.awt.geom.AffineTransform。它是从所需的 .class 文件中间接引用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985480/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com