2015-10-07 53 views
2

引用我使用ItextPdf生成PDF文件,但我在這條線canvas.addImage(background, width, 0,0, height, 20, 430);即型號java.awt.geom.AffineTransform無法解析得到一個例外。它是從所需的.class文件間接引用的。在這一行我試圖設置背景圖像。請幫我解決這個例外。無法解析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(); 
    } 
} 

回答

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在您的代碼中不支持的功能。

長話短說:用其Android端口iTextG取代iText,您的問題將得到解決。

+0

我很好奇,這些類是「禁止的」還是僅僅出現在Google選擇在Android中使用的Java API子集中?我認爲這是後者,但我可能是錯的。 –

+0

Google選擇在Android中使用的Java API子集中不存在。 Dalvik虛擬機不知道它們。原因:不同的映像模型和不同的文件處理方式,特別是在Google App Engine環境中(雲中的*文件系統*與磁盤上的文件系統不同)。 –

相關問題