user1237894
user1237894

Reputation: 41

i want to convert text to pdf in android

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

Upvotes: 3

Views: 3609

Answers (2)

Aashish Bhatnagar
Aashish Bhatnagar

Reputation: 2605

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(" "));
        }
    }


}

Upvotes: 0

Snicolas
Snicolas

Reputation: 38168

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?

Upvotes: 4

Related Questions