2014-03-24 25 views
4

我生成使用droidText PDF liberary該方法的getInstance(字節[])是未定義的類型文獻..的Android

我有以下代碼

public void createPDF() 
{ 
    Document doc = new Document(); 


    try { 
      String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText"; 

      File dir = new File(path); 
       if(!dir.exists()){ 
        System.out.println("directory not exists"); 
        dir.mkdirs(); 
      }else{ 
       System.out.println("directory exirsts"); 
      } 
      System.out.println("path="+path); 

      Log.d("PDFCreator", "PDF Path: " + path); 


      File file = new File(dir, "sample.pdf"); 
      FileOutputStream fOut = new FileOutputStream(file); 

      PdfWriter.getInstance(doc, fOut); 

      //open the document 
      doc.open(); 


      Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText"); 
      Font paraFont= new Font(Font.COURIER); 
      p1.setAlignment(Paragraph.ALIGN_CENTER); 
      p1.setFont(paraFont); 

      //add paragraph to document  
      doc.add(p1); 

      Paragraph p2 = new Paragraph("This is an example of a simple paragraph"); 
      Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN); 
      p2.setAlignment(Paragraph.ALIGN_CENTER); 
      p2.setFont(paraFont2); 

      doc.add(p2); 

//

   //set footer 
      Phrase footerText = new Phrase("This is an example of a footer"); 
      HeaderFooter pdfFooter = new HeaderFooter(footerText, false); 
      doc.setFooter(pdfFooter); 




      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.fromFile(file),"application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
      startActivity(intent); 

    } catch (DocumentException de) { 
      Log.e("PDFCreator", "DocumentException:" + de); 
    } catch (IOException e) { 
      Log.e("PDFCreator", "ioException:" + e); 
    } 
    finally 
    { 
      doc.close(); 
    } 

} 

它的工作..但是當我使用下面的行添加圖像,它說:「方法getInstance(字節[])是未定義類型文檔」

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.ic_launcher); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream); 
      Image myImg = Image.getInstance(stream.toByteArray()); 
      //myImg.setAlignment(Image.MIDDLE);     
      add image to document 
      doc.add(myImg); 

幫助我將圖像添加到文檔

+0

的可能的複製[droidtext添加圖片不能正常工作( http://stackoverflow.com/questions/15951656/droidtext-adding-image-doesnt-work) – Appleman1234

+1

檢查導入,經常會發生默認包得到包括,因爲有android.media.Image類。 –

回答

3

Document.java,那類沒有一個getInstance方法。 你想要Image.getInstance按照這StackOverflow question,

E.g.

這不是一個重複的問題,但確實有,回答你的陳述,回答「幫我將圖像添加到文檔」

+0

做到了這一點,但它仍然不起作用 –

+0

請更新你原來的問題,你做了什麼,以及你所做的結果。你說它不起作用,但你沒有描述它是否仍然有相同的錯誤信息,或者如果錯誤信息消失了,但圖像仍然沒有出現,等等。 – Appleman1234

相關問題