DRaNe
DRaNe

Reputation: 21

Problems with iText trying to create a PDF file

I'm trying to create a PDF file when I press a button. I'm using iText library.

My activity:

package com.pdf;

import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    } 

    public void GenerarPDF(View view)throws Exception{
            Document document=new Document();
            PdfWriter.getInstance(document,new FileOutputStream("generando.pdf"));
            document.open();
            document.add(new Paragraph("Testing testing and testing"));
            document.close();
    }

}

My xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:background="#555">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:onClick="GenerarPDF"
        android:text="Button" />

</RelativeLayout>

My manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pdf"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
    <uses-permission android:name="android.permission.BIND_INPUT_METHOD"/>
    <uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".PDFActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And the log cat:

05-31 12:08:58.778: E/AndroidRuntime(662): FATAL EXCEPTION: main
05-31 12:08:58.778: E/AndroidRuntime(662): java.lang.IllegalStateException: Could not execute method of the activity
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.view.View$1.onClick(View.java:3044)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.view.View.performClick(View.java:3511)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.view.View$PerformClick.run(View.java:14105)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.os.Handler.handleCallback(Handler.java:605)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.os.Looper.loop(Looper.java:137)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-31 12:08:58.778: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:08:58.778: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:511)
05-31 12:08:58.778: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-31 12:08:58.778: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-31 12:08:58.778: E/AndroidRuntime(662):  at dalvik.system.NativeStart.main(Native Method)
05-31 12:08:58.778: E/AndroidRuntime(662): Caused by: java.lang.reflect.InvocationTargetException
05-31 12:08:58.778: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:08:58.778: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:511)
05-31 12:08:58.778: E/AndroidRuntime(662):  at android.view.View$1.onClick(View.java:3039)
05-31 12:08:58.778: E/AndroidRuntime(662):  ... 11 more
05-31 12:08:58.778: E/AndroidRuntime(662): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.Document
05-31 12:08:58.778: E/AndroidRuntime(662):  at com.pdf.PDFActivity.GenerarPDF(PDFActivity.java:22)
05-31 12:08:58.778: E/AndroidRuntime(662):  ... 14 more

Added logcat error:

05-31 15:26:00.411: E/dalvikvm(792): Could not find class 'com.itextpdf.text.Document', referenced from method com.pdf.PDFActivity.GenerarPDF

I think thats the real problem, but don't know the solution.

Upvotes: 2

Views: 6420

Answers (3)

anarchy_sumit
anarchy_sumit

Reputation: 1

looks like you forgot to call generatePDF() from onCreate()... hope it helps!

Upvotes: 0

SYM
SYM

Reputation: 11

change file root to this line

File root = new File(android.os.Environment.getExternalStorageDirectory(), "Notes");

Upvotes: 1

Arun George
Arun George

Reputation: 18592

Where exactly are you trying to create the pdf file? I used the itextpdf-5.2.1.jar and was able to successfully create the pdf in the sd card. I added the following lines of codes to your program:

public void GenerarPDF(View view)throws Exception{
        Document document=new Document();
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, "generando.pdf");
        PdfWriter.getInstance(document,new FileOutputStream(gpxfile));
        document.open();
        document.add(new Paragraph("Testing testing and testing"));
        document.close();
} 

Hope this answer helps.

EDIT: The error you mentioned in your comment (06-01 23:32:37.178: E/AndroidRuntime(535): Caused by: java.io.FileNotFoundException: /generando.pdf: open failed: EROFS (Read-only file system) ) is exactly what I get when I run the code you provided. The reason being that you are not specifying the location where the new file can be created/stored. Use the GenerarPDF(View view) in have pasted over here and the program will create the new pdf.

Another cause for this error might be that you don't have an sd card in your device.

Upvotes: 4

Related Questions