简体   繁体   English

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

[英]How to Convert PDF file to DOC file in Android App Programmatic

I m developing android that convert pdf file to word file without using internet can some one please help me on this. 我正在开发无需使用互联网即可将pdf文件转换为word文件的android程序,请对此提供帮助。 I have one solution but throw file format exception : 我有一个解决方案,但抛出文件格式异常:

I have search many open source library but its cant help me 我搜索了许多开源库,但它无法帮助我

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

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

error : 错误:

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

Try below code 试试下面的代码

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;
}

From https://pdfbox.apache.org https://pdfbox.apache.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM