繁体   English   中英

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

[英]The type java.awt.geom.AffineTransform cannot be resolved. It is indirectly referenced from required .class files

我正在使用ItextPdf生成PDF文件,但我在此行canvas.addImage(background, width, 0,0, height, 20, 430);上获得了一个异常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();
    }
}   

您使用的是错误的iText版本。 您应该使用iTextG而不是“普通Java”iText版本。 作为Android开发人员,您知道在Android上禁止使用java.awt (和javax.nio ,...)类。

“普通Java”iText使用未在Android上列入白名单的类(例如,在PdfGraphics2D类中)。 这就是我们创建iTextG的原因。 iTextG与iText完全相同,只是我们删除了所有依赖于“禁止类”(而java.awt.geom.AffineTransform就是其中一个类)。

iTextG中的功能略少(我们不得不放弃PdfGraphics2D ),但乍一看,我看不到代码中iTextG不支持的任何内容。

长话短说:用其Android端口iTextG替换iText,您的问题将得到解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM