首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android应用程序中将PDF文件转换为DOC文件

如何在Android应用程序中将PDF文件转换为DOC文件
EN

Stack Overflow用户
提问于 2019-05-16 21:03:34
回答 1查看 1K关注 0票数 0

我正在开发的安卓转换pdf文件到word文件,而不使用互联网,可以有人可以帮助我在这一点。我有一个解决方案,但是抛出了文件格式异常:

我已经搜索了很多开源库,但都无济于事

代码语言:javascript
复制
 Document pdfDocument = new Document(new File(selectedImagePath).getAbsolutePath());

   String path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Test.doc";
                pdfDocument.save(path, SaveFormat.Doc);

错误:

代码语言:javascript
复制
class com.aspose.pdf.internal.ms.System.ArgumentException: Save a document to a doc format is not supported.
EN

回答 1

Stack Overflow用户

发布于 2019-05-16 21:13:59

尝试下面的代码

代码语言:javascript
复制
static String pdftoText(String fileName) {
    PDFParser parser;
    String parsedText = null;
    PDFTextStripper pdfStripper = null;
    PDDocument pdDoc = null;
    COSDocument cosDoc = null;
    File file = new File(fileName);
    if (!file.isFile()) {
        System.err.println("File " + fileName + " does not exist.");
        return null;
    }
    try {
        parser = new PDFParser(new FileInputStream(file));
    } catch (IOException e) {
        System.err.println("Unable to open PDF Parser. " + e.getMessage());
        return null;
    }
    try {
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);
        parsedText = pdfStripper.getText(pdDoc);
    } catch (Exception e) {
        System.err
                .println("An exception occured in parsing the PDF Document."
                        + e.getMessage());
    } finally {
        try {
            if (cosDoc != null)
                cosDoc.close();
            if (pdDoc != null)
                pdDoc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return parsedText;
}

来自https://pdfbox.apache.org

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56169282

复制
相关文章

相似问题

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