@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdftest);
Button Btgerar = (Button) findViewById(R.id.gerarpdf);
Btgerar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CriarPDF();
}
});
}
public void CriarPDF() {
String FILE = Environment.getExternalStorageDirectory().toString() + "/PDF/" + "Ficha.pdf";
EditText txt = (EditText) findViewById(R.id.editText);
// Create New Blank Document
Document document = new Document(PageSize.A4);
// Create Directory in External Storage
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/PDF");
myDir.mkdirs();
// Create Pdf Writer for Writting into New Created Document
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));
// Open Document for Writting into document
document.open();
document.add(new Paragraph(txt.getText().toString()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
// Close Document after writting all content
document.close();
Toast.makeText(this, "PDF File is Created. Location : " + FILE,
Toast.LENGTH_LONG).show();
// Create new Page in PDF
document.newPage();
}
}
I have declared in android manifest
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE
and build gradle version is :
compileSdkVersion 24
buildToolsVersion '24.0.0'
minSdkVersion 8
targetSdkVersion 24
The application can run normally, but can't create pdf file and I can't find.