如何使用Java将PDF文件转换为Word文件?

6

如何使用Java将pdf文件转换为word文件?

而且,这看起来是否像它表面上那么容易?


谷歌一下,你会找到答案的。你也可以使用stackoverflow的搜索引擎... - ZeusNet
1
你可以使用 Apache POI。http://poi.apache.org/ - Ronny K
1
它看起来容易的地方在哪里? - Peter Lawrey
1
@Holger,我已经问过了,但我需要一些答案。 - Gentuzos
发布你尝试过的代码,然后我会展示答案。 - newuser
显示剩余2条评论
2个回答

11

尝试使用PDFBOX

public class PDFTextReader
{
   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;
    }
    public static void main(String args[]){

         try {

            String content = pdftoText(PDF_FILE_PATH);

            File file = new File("/sample/filename.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
}

2
非常感谢,但图片和数学符号怎么办?我需要直接将其转换为Word文件吗? - Gentuzos
1
您的PDF文档包含图像和数学字符。 - newuser
1
是的,但将其转换为文本文件并不能解决这个问题。 - Gentuzos
2
哦,因为OCR处理过程太困难,并且提取图像内容需要很长时间。我使用JPedal jar http://www.idrsolutions.com/demo-landing-page/,只需通过控制台运行jar即可。 - newuser
1
这是一个第三方的jar包。你可以下载com.sun.media jar包,链接为http://www.java2s.com/Code/Jar/s/Downloadsunjaicodecjar.htm。 - newuser
显示剩余11条评论

6
我深入研究了这个问题,发现为了获得正确的结果,你无法避免使用MS Word。即使像LibreOffice这样的资助项目也难以进行正确的转换,因为Word格式相当复杂且版本不断变化。只有MS Word才能跟踪到这一点。
因此,我实现了documents4j,它使用Java API将转换委托给MS Word。此外,它还允许您将转换移动到另一台机器上,并通过REST API联系该机器。你可以在GitHub页面找到详细信息。

1
类型com.documents4j.job.AbstractConverterBuilder无法解析。它是从所需的.class文件间接引用的,但该类型在javadoc参考中不存在。 - 0x777
1
似乎你的类路径不完整。Javadoc 只包含官方 API 类。 - Rafael Winterhalter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接