如何在Android App Programmatic中将PDF文件转换为DOC文件

时间:2019-05-16 13:03:34

标签: java android

我正在开发将pdf文件转换为word文件而不使用互联网的android系统,有人可以对此提供帮助。我有一个解决方案,但抛出文件格式异常:

我已经搜索了很多开源库,但是它不能帮助我

 Document pdfDocument = new Document(new File(selectedImagePath).getAbsolutePath());

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

错误:

class com.aspose.pdf.internal.ms.System.ArgumentException: Save a document to a doc format is not supported.

1 个答案:

答案 0 :(得分:1)

尝试下面的代码

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