1

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;
}
}
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
sammyukavi
  • 1,501
  • 2
  • 23
  • 51

2 Answers2

3

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.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
1

If you have the time, you can try and create a Java ME compliant version of iText, but to properly open a PDF the library must use some form of Random Access File because of the xref table at the end of the file. This sort of file connection is not available in Java ME.

What the library can do is to fully load the PDF to memory, which is highly dependent on the file size and the handset memory available.

You better create a Web Service to receive your PDF and return, for example, PNG images from it.

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22