2015-09-25 70 views
0

我剛開始使用應用程序的文本,我正在努力提高我的android知識。然而我無法弄清楚的是如何獲得已填寫的pdf,然後使用意圖通過電子郵件發送。我一直在Google上搜索和研究,但我找不到任何東西。有誰知道如何去做呢?如何用itext創建電子郵件pdf

在我的onCreate之前聲明我的文件;

File file = new File(getExternalFilesDir(null),"pvgform.pdf"); 

這是我的代碼添加信息到填寫的PDF文本

OutputStream output = null; 
try { 
    output = new FileOutputStream(file); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 
try { 
    reader = new PdfReader(getResources().openRawResource(R.raw.form)); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
try { 
    PdfStamper stamper = new PdfStamper(reader, output); 
    AcroFields acroFields = stamper.getAcroFields(); 
    acroFields.setField("fullname", editText.getText().toString()); 
    acroFields.setField("agedob", editText2.getText().toString()); 
    acroFields.setField("description", editText3.getText().toString() + editText4.getText().toString() + editText5.getText().toString()); 
    acroFields.setField("duration", editText6.getText().toString()); 
    acroFields.setField("brandname", editText8.getText().toString()); 
    acroFields.setField("genericname", editText9.getText().toString()); 
    stamper.setFormFlattening(true); 
    stamper.close(); 
} catch (DocumentException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
reader.close(); 

一部分,這是我上的電子郵件意圖爲PDF格式的嘗試:

Intent email = new Intent(Intent.ACTION_SEND); 
email.putExtra(Intent.EXTRA_SUBJECT, "My form"); 
email.putExtra(Intent.EXTRA_TEXT, "Here is a form"); 
Log.d("file",file.getAbsolutePath()); 
Uri uri = Uri.fromFile(file); 
email.putExtra(Intent.EXTRA_STREAM,uri); 
email.setType("application/pdf"); 
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

時我運行這個,沒有附件發送電子郵件。

回答

0

這是我的代碼部分的信息添加到可填寫的PDF文本

你似乎並沒有被寫入的PDF文件。

,這是我上的電子郵件意圖爲PDF格式的嘗試:

Uri uri=Uri.parse("file://"+ "form.pdf"); 

這不是在任何平臺上的有效Uri

將PDF寫入文件,例如external storage。然後,使用解析爲該文件的Uri,使用Uri.fromFile()將您的File對象轉換爲適當的Uri

+0

是不是打開壓模並添加用戶輸入文本將其添加到可填充文本?另外,我編輯了我的uri。好點嗎? – Aria

+0

@Aria:「是不是打開壓模並添加用戶輸入文本,將它添加到可填寫文本中?」 - 是的,但只在內存中。 「另外,我編輯了我的uri,是否更好?」 - 首先,我沒有看到任何將PDF保存到該文件的代碼。其次,使用'new File(getFilesDir(),「form.pdf」)'更安全地構建該路徑。第三,「Uri」將毫無用處,因爲第三方電子郵件應用程序無法從應用程序的內部存儲中讀取。您可以將文件保存到'getFilesDir()',但是您需要使用FileProvider來以第三方應用程序可以訪問的方式來提供文件。 – CommonsWare

+0

所以你可以告訴我要添加到我的代碼?對不起,我是一個帶有文本的初學者,所以我不知道還有什麼可以嘗試的 – Aria

相關問題