2013-04-11 56 views
0

我拼命地試圖插入一個圖像到現有的PDF與droidtext。droidtext添加圖像不起作用

該項目的原始版本是用iText製作的。 因此,該代碼已經存在,並已修改爲適合Android。

我所做的是以現有的PDF爲背景。 插入文本和十字架在這個pdf內的指定位置。 就像填寫表格一樣。 目前爲止,這很有效,但沒有徹底改變代碼。

現在我想設置一個圖像到頁面的底部簽署表單。 我使用了我的原始代碼並對其進行了一些調整。 哪一個根本不起作用。 我試圖在特定位置設置圖像。也許這是錯誤。

所以我試圖做到「官方」的方式。 Pengiuns.jpg圖片位於SD卡上。

try { 
Document document = new Document(); 
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf"); 
PdfWriter.getInstance(document,new FileOutputStream(f)); 
document.open(); 
document.add(new Paragraph("Simple Image")); 
String path = Environment.getExternalStorageDirectory()+"/Penguins.jpg"; 

if (new File(path).exists()) { 
    System.out.println("filesize: " + path + " = " + new File(path).length()); 
} 

Image image =Image.getInstance(path); 
document.add(image); 
document.close(); 
} catch (Exception ex) { 
    System.out.println("narf"); 
} 

但仍然沒有形象。 我得到的是一個帶有「簡單圖像」字樣的PDF,沒有別的。 我可以訪問圖片。我通過if()獲取正確的文件大小。 不會引發異常。

所以我的問題是,我怎樣才能得到一張位於SD卡上的圖像到我的pdf? 這裏有什麼錯誤? 但最重要的是,如何將圖像設置爲pdf內大小的特定位置? 在我原來的代碼中,我使用setAbsolutePosition(x,y)。 當我在代碼中使用它時,Eclipse並沒有抱怨,但它確實工作嗎?

+0

Droidtext是iText的一個過時版本叉子。這個分支沒有被iText的作者認可(我是原始的iText開發者),因此它不受支持。您是否嘗試過使用iText的官方Android端口? http://itextsupport.com/download/android.html – 2013-04-11 15:30:32

回答

1

你得到「簡單圖像」的原因是因爲你在Paragraph。爲了添加圖像,使用方法:

Image myImg1 = Image.getInstance(stream1.toByteArray()); 

如果你想擁有它你可以使用下面的代碼的最後頁面頁腳,但它與文字作品。您可以嘗試操作它的圖像:

Phrase footerText = new Phrase("THIS REPORT HAS BEEN GENERATED USING INSPECTIONREPORT APP"); 
HeaderFooter pdfFooter = new HeaderFooter(footerText, false); 
doc.setFooter(pdfFooter); 

這裏是示例代碼。這裏我已經上傳了一張圖片到Imageview,然後添加到了pdf中。希望這可以幫助。

private String NameOfFolder = "/InspectionReport"; 

Document doc = new Document(); 

try { //Path to look for App folder 
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder; 
    String CurrentDateAndTime= getCurrentDateAndTime(); 

    // If App folder is not there then create one 
    File dir = new File(path); 
    if(!dir.exists()) 
     dir.mkdirs(); 


    //Creating new file and naming it 
    int i = 1; 

    File file = new File(dir, "Inspection"+Integer.toString(i)+"-" + CurrentDateAndTime+".pdf"); 
    while(file.exists()) { 
     file = new File(dir, "Inspection"+Integer.toString(i++)+"-" + CurrentDateAndTime+".pdf");} 


     String filep= file.toString(); 
     FileOutputStream fOut = new FileOutputStream(file); 

     Log.d("PDFCreator", "PDF Path: " + path); 
     PdfWriter.getInstance(doc, fOut); 
     Toast.makeText(getApplicationContext(),filep , Toast.LENGTH_LONG).show(); 

     //open the document 
     doc.open(); 

     ImageView img1 = (ImageView)findViewById(R.id.img1); 
     ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 
     Bitmap bitmap1 = ((BitmapDrawable)img1.getDrawable()).getBitmap(); 
     bitmap1.compress(Bitmap.CompressFormat.JPEG, 100 , stream1); 
     Image myImg1 = Image.getInstance(stream1.toByteArray()); 
     myImg1.setAlignment(Image.MIDDLE); 

     doc.add(myImg1); 
0

嘗試以下代碼:

/* Inserting Image in PDF */ 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android); 

bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream); 

Image myImg = Image.getInstance(stream.toByteArray()); 

myImg.setAlignment(Image.MIDDLE); 

//add image to document 
doc.add(myImg);