kishore
kishore

Reputation: 19

How To Create a PDF file in raw in Android

How to Create A PDF fiel in raw ..Eventhough I Was using iText.jar I was geting an error specifying File Not Found Error and also saying File cannot be Opened .Here is the code

/** Called when the activity is first created. */

EditText editText;
Button button;
private static String FILE = "C:/FirstPdf.pdf";
String body ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    editText = (EditText)findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            body = editText.getText().toString();

            Document document = new Document();
            try {
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                document.open();
                document.addSubject(body);
                document.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    });

}

Upvotes: 1

Views: 1611

Answers (1)

Athul Harikumar
Athul Harikumar

Reputation: 2491

EditText editText;
Button button;
private static String FILE = "";
 String body ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FILE=(Environment.getExternalStorageDirectory().getAbsolutePath());
editText = (EditText)findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        body = editText.getText().toString();

        Document document = new Document();
        try {
             File temp = new File(FILE.getAbsolutePath(),"abcd.pdf");   
            PdfWriter.getInstance(document, new FileOutputStream(temp));
            document.open();
            document.addSubject(body);
            document.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
});

}

there is no C: in android use Environment.getExternalStorageDirectory().getAbsolutePath() to get external storage path

in manifest add permission to write external storage

Upvotes: 1

Related Questions