我使用了来自Tom Roush PDFBox Android的PDFBox并将其导入build.gradel:dependencies {compile 'org.apache:pdfbox-android:1.8.9.0'}
但每次我运行活动时,只会出现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();
}
}
答案 0 :(得分:1)
我添加了" .pdf"并将toast移到了document.close()后面。现在Toast没有显示,我无法在存储/模拟/ 0 /下载中找到该文件。
自API 23+起,您需要检查活动中的权限。这是解决方案。