java将pdf文件转String

来源:互联网 发布:视频播放cms 编辑:程序博客网 时间:2024/06/16 21:17

/**
* 读取pdf文件内容
*
* @param file
* @return
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String pdf2String(File file)
throws IOException
{
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
PDDocument pdfDocument = PDDocument.load(inputStream);
StringWriter writer = new StringWriter();
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfDocument, writer);
String contents = writer.getBuffer().toString();
// System.out.println(“文档内容:” + contents);
PDDocumentInformation documentInformation = pdfDocument.getDocumentInformation();
// System.out.println(“标题:” + documentInformation.getTitle());

    pdfDocument.close();    return contents;}
0 0
原创粉丝点击