2013-05-10 17 views
4

我將doc文件爲PDF格式,在Android中使用下列庫,文檔轉換成PDF在Android中,無法執行DEX

  • iText的 - 1.4.8.jar
  • POI-3.0-FINAL。罐子
  • POI暫存器-3.2-FINAL.jar

這裏是我的示例代碼

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

}

但我得到這個錯誤

[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; 

我已刪除了我的Android的支持,v4.jar。從lib文件夾中的A/C這個答案answer about the error但我仍然得到同樣的錯誤:(

請幫我解決這個問題 任何人誰也做了文檔轉換爲PDF,請分享您的代碼。

,我將非常感激:)

問候

+0

您似乎有不匹配的Apache POI jar包 - 如果您想包含Core和Scratchpad,您必須*擁有相同版本的jar包! – Gagravarr 2013-07-31 21:44:36

+0

@Droid GEEK:你解決了這個問題嗎?我面臨同樣的問題。 – SKK 2014-12-01 12:24:42

回答

1

的問題是要包括兩次或更多的東西:

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

查看複製庫的構建路徑。

此外,一旦這個解決了,你會problably必須在project.properties文件中加入這一行: dex.force.jumbo =真

這將允許您解決與問題65535方法限制了一段時間的問題。

相關問題