【发布时间】:2017-01-26 08:32:54
【问题描述】:
我使用了来自:Tom Roush PDFBox Android 的 PDFBox,并将其导入到 build.gradel:dependencies {compile 'org.apache:pdfbox-android:1.8.9.0'}
但每次我运行 Activity 时,都会出现 Toast,但找不到填充的 PDF 文件。 PDF 位于 /assets 文件夹中。我自己在 /app/src/main/assets 中创建了这个文件夹。我使用 Android Studio。
活动:
public class GwmProbenActivity extends AppCompatActivity {
DatabaseHelperAllgemeineDaten myDb;
public String stOrt;
public String stProgramm;
public String stName;
public String stDatum;
File root;
AssetManager assetManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proben);
Intent intent = getIntent();
stDatum = intent.getStringExtra("datum");
myDb = new DatabaseHelperAllgemeineDaten(this);
PDFBoxResourceLoader.init(getApplicationContext());
root = android.os.Environment.getExternalStorageDirectory();
assetManager = getAssets();
}
public void fillPDF() {
try {
// Load the document and get the AcroForm
PDDocument document = PDDocument.load(assetManager.open("Vorlage_GWM- Protokoll.pdf"));
PDDocumentCatalog docCatalog = document.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
// Fill the text field
PDFieldTreeNode field = acroForm.getField("ProbenahmeDatum");
field.setValue(stDatum);
String path = root.getAbsolutePath() + "/Download/Vorlage_GWM-Protokoll1";
Toast.makeText(this,"Saved filled form to " + path,Toast.LENGTH_LONG).show();
document.save(path);
document.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void speichern (View v) {
fillPDF();
}
}
【问题讨论】:
-
1) 您保存的文件名中缺少“.pdf” 2) 在保存之前调用 Toast,因此它的出现没有任何意义 => 请更正此问题,编辑您的问题并在cmets现在发生了什么。
-
谢谢,我已添加“.pdf”并将 Toast 移到 document.close() 后面。现在 Toast 没有出现,我在 storage/emulated/0/Download 中找不到文件
-
好的,知道了。从 API 23+ 开始,您需要检查 Activity 中的权限。 This is was the solution
-
很高兴听到这个好消息。请删除问题或自己回答。