4

I am converting doc file into pdf format in android using following libraries,

  • itext-1.4.8.jar
  • poi-3.0-FINAL.jar
  • poi-scratchpad-3.2-FINAL.jar

here is my sample code

package com.example.converter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.os.Environment;
import android.widget.LinearLayout;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

    public class TestCon extends LinearLayout {

   FileInputStream infile;
   private static String FILE = Environment.getExternalStorageDirectory()
        + "/MyReport.pdf";

   public TestCon(Context context) {
    super(context);
    my_method(context);
  }

  public void my_method(Context context) {
    POIFSFileSystem fs = null;
    Document document = new Document();

    try {
        infile = (FileInputStream) context.getApplicationContext().getAssets().open("test.doc");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        System.out.println("Starting the test");
        fs = new POIFSFileSystem(infile);

        HWPFDocument doc = new HWPFDocument(fs);
        WordExtractor we = new WordExtractor(doc);

        OutputStream file = new FileOutputStream(FILE);

        PdfWriter writer = PdfWriter.getInstance(document, file);

        Range range = doc.getRange();
        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        String[] paragraphs = we.getParagraphText();
        for (int i = 0; i < paragraphs.length; i++) {

            org.apache.poi.hwpf.usermodel.Paragraph pr = range
                    .getParagraph(i);
            // CharacterRun run = pr.getCharacterRun(i);
            // run.setBold(true);
            // run.setCapitalized(true);
            // run.setItalic(true);
            paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
            System.out.println("Length:" + paragraphs[i].length());
            System.out.println("Paragraph" + i + ": "
                    + paragraphs[i].toString());

            // add the paragraph to the document
            document.add(new Paragraph(paragraphs[i]));
        }

        System.out.println("Document testing completed");
    } catch (Exception e) {
        System.out.println("Exception during test");
        e.printStackTrace();
    } finally {
        // close the document
        document.close();
    }
}

}

but I am getting this error

[2013-05-10 12:39:12 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/apache/poi/generator/FieldIterator;
[2013-05-10 12:39:12 - converter] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/apache/poi/generator/FieldIterator;

I have removed my android-support-v4.jar. from lib folder a/c to this answer answer about the error but I am still getting the same error :(

Please help me to solve this issue Anyone who have done the doc to pdf conversion,please share your code.

I will be very thankful :)

Regards

Community
  • 1
  • 1
Droid GEEK
  • 192
  • 2
  • 11
  • You appear to have mis-matched Apache POI jars - If you want to include Core and Scratchpad you *must* have the jars from the same version! – Gagravarr Jul 31 '13 at 21:44
  • @Droid GEEK : Did you fix this issue. I am facing the same issue. – SKK Dec 01 '14 at 12:24

1 Answers1

1

The problem is that you are including something twice or more :

Multiple dex files define Lorg/apache/poi/generator/FieldIterator

Review your build path for duplicated libraries.

In addition, once this is resolved, you'll problably have to add this line in the project.properties file : dex.force.jumbo=true

This will allow you to solve the problem with the 65535 methods limit problem for some time.

Mikel
  • 411
  • 4
  • 8