3

I got error in logcat:

03-23 07:53:22.422: E/AndroidRuntime(2603): FATAL EXCEPTION: main
03-23 07:53:22.422: E/AndroidRuntime(2603): java.lang.NoClassDefFoundError: java.awt.Color
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfChunk.color(PdfChunk.java:501)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:2651)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.flushLines(PdfDocument.java:2388)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.newPage(PdfDocument.java:772)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.close(PdfDocument.java:940)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.Document.close(Unknown Source)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.example.pdfexample.MainActivity.onCreate(MainActivity.java:26)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.Activity.performCreate(Activity.java:5104)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.os.Looper.loop(Looper.java:137)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at dalvik.system.NativeStart.main(Native Method)
help me , thanks in advance
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
user1237894
  • 41
  • 2
  • 6
  • I think you are using any external library.now i would suggest u to put that jar file inside libs folder and then try – Dilip Mar 23 '13 at 08:04

2 Answers2

4

You are using a library that has been designed for pure Java. There are some minor but still notable differences between Java APIs and Android APIs, mostly related to gfx. As you can see the Java Color class doesn't have a strict equivalent on Android. That's what causes your bug here.

Either you find an Android-able PDF library or you use a remote service to convert your document and download it as PDF directly.

This thread might be of interest to you : PDF Library for Android - PDFBox?

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • i am using itext library in libs. some times it creates pdf file with 0 bytes. what can i do now? any new ways to generate pdf with text. – user1237894 Mar 25 '13 at 06:50
0

You need to add the jar files you are using to your build path.

java.lang.NoClassDefFoundError generally occurs when you haven't added the jar files to your build path

How to add the jar file into the build path

follow this link incase you have problem adding jars to the build path besides for the pdf conversion best option would be to use something like iTextPdf

Use this class and call the required functions to create pdf

public class CreatePDF {

    private static Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 25,
            Font.NORMAL, BaseColor.BLACK);

    private static Font Head = new Font(Font.FontFamily.TIMES_ROMAN, 35,
            Font.BOLD, BaseColor.BLACK);

    //Path is the path where you want your pdf to get stored
    public void createPDFDoc(ArrayList<notesWrapper> notesList,String path) {
        // TODO Auto-generated method stub
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream(path));
            document.open();

            for(int i=0;i<notesList.size();i++)
            {               


                addContentHead(document,"Image "+(i+1));                
                addContent(document,notesList.get(i).message);
                if(i<notesList.size())
                {
                    document.newPage();
                }
            }

            document.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    private void addContent(Document document,String content) throws DocumentException {


        Paragraph preface = new Paragraph();              
        addEmptyLine(preface, 1);

        if(!content.equalsIgnoreCase("insert note"))
        preface.add(new Paragraph(content, normalFont));

        else
            addEmptyLine(new Paragraph(), 1);

        addEmptyLine(preface, 3);
        document.add(preface);



    }


    private void addContentHead(Document document,String content) throws DocumentException {


        Paragraph preface = new Paragraph();              
        addEmptyLine(preface, 1);

        preface.add(new Paragraph(content, Head));
        addEmptyLine(preface, 3);
        document.add(preface);



    }



    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }


}
Community
  • 1
  • 1
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • i am using itext library in libs. some times it creates pdf file with 0 bytes. what can i do now? any new ways to generate pdf with text – user1237894 Mar 25 '13 at 06:51
  • I have been using this library to create the pdf it works just fine you need to add this files to your build path I am updating my answer it will give you some more idea – Aashish Bhatnagar Mar 25 '13 at 06:56
  • I have added the link as well download and extract the jars from there extract them and add all of them to build path – Aashish Bhatnagar Mar 25 '13 at 07:04