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

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

而且,它看起来像它一样容易吗?

回答:

试试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();

}

}

}

以上是 如何使用Java将pdf文件转换为word文件 的全部内容, 来源链接: utcz.com/qa/425280.html

回到顶部