jcAmats
jcAmats

Reputation: 5

iText: having trouble creating a pdf file in android

i have a problem with iText in creating a pdf file in android. It keeps crashing and this error keeps appearing in log cat:

java.lang.NoClassDefFoundError: com.itextpdf.text.Document

this is my java file:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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

public class MainActivity extends Activity {

public static Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.Submit);

    button.setOnClickListener(new OnClickListener() 
    {           
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub

            toPDF();

        }
    });
}

public void toPDF(){

    Document document=new Document();
    try {
        PdfWriter.getInstance(document,new FileOutputStream("try.pdf"));
        document.open();
        document.add(new Paragraph("Hello Android!! :)"));
        document.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

and i already included <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in AndroidManifest.xml

and this is my .classpath..

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
     <classpathentry kind="src" path="src"/>
     <classpathentry kind="src" path="gen"/>
     <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
     <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itextpdf-5.3.4.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itextpdf-5.3.4-javadoc.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itextpdf-5.3.4-sources.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-pdfa-5.3.4.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-pdfa-5.3.4-javadoc.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-pdfa-5.3.4-sources.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-xtra-5.3.4.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-xtra-5.3.4-javadoc.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-xtra-5.3.4-sources.jar"/>
     <classpathentry kind="output" path="bin/classes"/>
</classpath>

I also tried to insert SD card because someone says that it might be the problem, but still, it crashed...

hope anyone can help me.. i'm new to android.. :|

Upvotes: 0

Views: 1667

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82533

There are only three reasons you will ever get this error:

  1. The class genuinely doesn't exist. If you are using code from an official example and getting this, make sure you have the latest build of the library
  2. You have not added the jar to your build path. To fix this, right click on the jar in Eclipse, and do Build Path ► Add to Build Path.
  3. Your jar is not in the /libs folder. This happens when you have added the jar to the build path, but newer versions of ADT need it to be in /libs. Put it there and re-add it to the build path.

Mostly, such errors occur because newer versions of the ADT require all external jars to be in the libs folder. Your colleague was probably on a different version than you, and hence the error.

In your case, simple move all the necessary jar files from itext-5.3.4 into libs.

Upvotes: 2

Related Questions