Search code examples
javajava-meitextlwuit

Add Java SE Classes to Java ME read PDF


I'm writing a Java ME application that uses iText to read PDF. When I write my code in standard Java including the iText libraries in the class-path, the application runs. However if I move the code into a java mobile application including the iText libraries in the class-path there is an error during compiling that says

error: cannot access URL
PdfReader reader = new PdfReader(pdfPath);                 
class file for java.net.URL not found

My problem is that I need a work around to read the PDF file. I've tried adding rt.jar as a library into my code which is the package that contains java.io but it is too big to be compiled. Please help me find a work around. My code is here

package PDFreaderpackage;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import javax.microedition.midlet.MIDlet;

public class Midlet extends MIDlet {

Form displayForm;
TextArea pdfText;
private String bookcontent;
public static String INPUTFILE = "c:/test.pdf";
public static int pageNumber = 1;

public void startApp() {
    Display.init(this);
    this.bookcontent = readPDF(INPUTFILE, pageNumber);
    pdfText = new TextArea(bookcontent);
    displayForm = new Form("Works");
    displayForm.addComponent(pdfText);
    displayForm.show();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public String readPDF(String pdfPath, int pageNumber) {

    try {
        PdfReader reader = new PdfReader(pdfPath);
        this.bookcontent = PdfTextExtractor.getTextFromPage(reader, pageNumber);
    } catch (Exception e) {
        System.out.println(e);
    }
    return bookcontent;
}
}

Solution

  • These classes aren't available on a mobile device and JavaME doesn't support Java 5 features. What you are trying to do is somewhat impractical. Codename One allows some more classes thanks to bytecode processing but even then this isn't close to a complete rt.jar.