简体   繁体   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, 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);

// Read the PDF page by page //逐页读取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);
        }

// Write the word document //写word文档

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

// Close all open files // 关闭所有打开的文件

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

    }
}


}

And this is the error这就是错误

错误图像

Can anyone help me?I am grateful if anyone can solve my problem谁能帮助我?如果有人能解决我的问题,我将不胜感激

Solution解决方案

The interface Paragraph is not found in classpath.在类路径中找不到接口Paragraph This failure happend in older POI versions because the WL and SL interfaces were inside scratchpad , which is not included in poi jar.此故障发生在较旧的 POI 版本中,因为 WL 和 SL 接口位于scratchpad内,而poi jar 中不包含该暂存器。

POI older than V4.1.2早于 V4.1.2 的 POI

When you are using an older version than 4.1.2 you must also add the poi-scrachpad library as dependency to your classpath / build.gradle当您使用比 4.1.2 更旧的版本时,您还必须将poi-scrachpad库作为依赖项添加到您的类路径 / build.gradle

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

Switch to POI V4.1.2切换到 POI V4.1.2

As you can see at https://github.com/apache/poi/commit/f2dba42227abb4edf0ba0313972762974585693f the WP and SL interfaces were moved by this commit on 28 May 2015 for release 4.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 ) 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 )

If this is still a problem, ensure you have added the POI library as an implementation dependency and not accidently only for test .如果这仍然是一个问题,请确保您已将 POI 库添加为implementation依赖项,而不是意外地仅用于test

build.gradle example: 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