簡體   English   中英

在 Android Studio 中將 PDF 轉換為 Word

[英]Converting PDF to Word in Android Studio

I want to convert my pdf file to word, i've search & found itext for reading pdf and apache poi for convert word, i have a problem how to use it:D hehe,

public class MainActivity extends AppCompatActivity {

TextView browsefile, filename;
ImageView gambar;
int RequestFile = 2;
Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    browsefile = findViewById(R.id.browsefile);
    filename = findViewById(R.id.filename);
    gambar = findViewById(R.id.gambarfile);
    mContext = MainActivity.this;

    browsefile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startOptionBrowseFile();
        }
    });


}

private void startOptionBrowseFile() {
    Intent openFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    openFileIntent.setType("*/*");
    startActivityForResult(openFileIntent, RequestFile);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RequestFile && resultCode == RESULT_OK && data != null){
        Uri selectedFile = data.getData();

        
        File path = new File(selectedFile.getPath());
        filename.setText(selectedFile.getPath());

        XWPFDocument doc = new XWPFDocument();
        String pdf = String.valueOf(path);
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);

//逐頁讀取PDF

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            TextExtractionStrategy strategy = null;
            try {
                strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Extract the text
            String text=strategy.getResultantText();
            // Create a new paragraph in the word document, adding the extracted text
            XWPFParagraph p = doc.createParagraph();
            XWPFRun run = p.createRun();
            run.setText(text);
            // Adding a page break
            run.addBreak(BreakType.PAGE);
        }

//寫word文檔

        FileOutputStream out = null;
        try {
            out = new FileOutputStream("myfile.docx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            doc.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }

// 關閉所有打開的文件

        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        reader.close();

    }
}


}

這就是錯誤

錯誤圖像

誰能幫助我?如果有人能解決我的問題,我將不勝感激

解決方案

在類路徑中找不到接口Paragraph 此故障發生在較舊的 POI 版本中,因為 WL 和 SL 接口位於scratchpad內,而poi jar 中不包含該暫存器。

早於 V4.1.2 的 POI

當您使用比 4.1.2 更舊的版本時,您還必須將poi-scrachpad庫作為依賴項添加到您的類路徑 / build.gradle

https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad

切換到 POI V4.1.2

正如您在https://github.com/apache/poi/commit/f2dba42227abb4edf0ba0313972762974585693f中看到的那樣,WP 和 SL 接口在 2015 年 5 月 28 日的 4.1.2 版本中被此提交移動

So if you are using 4.1.2 you simple need just a dedicated POI library jar in classpath/build.gradle and the Paragraph interface must be found (see https://mvnrepository.com/artifact/org.apache.poi )

如果這仍然是一個問題,請確保您已將 POI 庫添加為implementation依賴項,而不是意外地僅用於test

build.gradle示例:

dependencies {
   // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
   compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM