首页 > 解决方案 > 如何通过在android中将文本转换为pdf和pdf到docx来将文本转换为docx

问题描述

我想在 android 应用程序中将文本转换为 docx 格式。我想知道如何实现这一点。

我先尝试直接从文本转换为 docx。我尝试实现 Apache POI 和 Aspose 库,但没有找到我的解决方案。运行时的 Aspose 库给出了“重复 API”的错误,我检查了 Aspose 论坛它还没有解决。我尝试了它被告知的任何内容。

我尝试将文本实现为pdf,它完成了。现在我想知道如何从pdf转换为docx?

任何人都可以帮助提供适当的功能细节来完成这项任务吗?或任何其他建议,以便可以将文本转换为 docx?

// Below code is for converting directly from text to docx  .
// This is using Apache POI but it is not importing classes after adding library


private void docxFormat()
    {

        XWPFDocument xwpfDocument = new XWPFDocument();

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File("yourfilepath/filename.docx"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        for(String s:lines) {


            XWPFParagraph xwpfParagraph = xwpfDocument.createParagraph();


            XWPFRun xwpfRun = xwpfParagraph.createRun();

            xwpfRun.setText(s);

        }
        xwpfDocument.write(fileOutputStream);
        fileOutputStream.close();
    }

// Anybody any suggestion for converting text to docx

标签: androidprogrammatically

解决方案


您可以通过 Java API 的代码使用以下Aspose.Words for Android轻松地将文本文件转换为 Word 格式,例如 DOC、DOCX、RTF 和许多其他格式(PDF、XPS、HTML 等) :

try
{
    String licString = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Aspose.Words.Android.lic";
    String inputPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/input.txt";
    String outputPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/output.docx";

    com.aspose.words.License lic = new com.aspose.words.License();
    lic.setLicense(licString, this);

    com.aspose.words.Document doc = new com.aspose.words.Document(inputPath);
    doc.save(outputPath);
}
catch (Exception e)
{
    e.printStackTrace();
}

希望这可以帮助。我与 Aspose 一起担任开发人员宣传员。


推荐阅读