首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android Studio中将PDF转换为Word

在Android Studio中将PDF转换为Word
EN

Stack Overflow用户
提问于 2020-07-17 04:29:06
回答 1查看 868关注 0票数 1

我想把我的pdf文件转换成word,我已经搜索并找到了用于阅读pdf的文本和用于转换word的apache poi,我有一个如何使用它的问题:D hehe

代码语言:javascript
复制
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

代码语言:javascript
复制
        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文档

代码语言:javascript
复制
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("myfile.docx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            doc.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }

//关闭所有打开的文件

代码语言:javascript
复制
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        reader.close();

    }
}


}

这就是错误

有人能帮我吗?如果有人能解决我的问题,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2020-07-17 06:53:03

解决方案

在类路径中找不到接口Paragraph。在较早的POI版本中会发生此故障,因为WL和SL接口位于poi jar中未包含的scratchpad中。

POI早于V4.1.2

使用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

因此,如果您使用的是4.1.2,那么您只需要在类路径/build.gradle中找到一个专用的POI库jar,并且必须找到段落接口(参见https://mvnrepository.com/artifact/org.apache.poi)。

如果问题仍然存在,请确保已将相关点库添加为implementation从属关系,而不是仅针对test

build.gradle示例:

代码语言:javascript
复制
dependencies {
   // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
   compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62942989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档