用java读取pdf
需求描述起来比较简单,就是java读取pdf文件,pdf文件中有表格,需要一个单元格一个单元格的读.如果实在不行,读取单行也行.希望大家帮我解决一下.谢谢了.(补充一下....
需求描述起来比较简单,就是java读取pdf文件,pdf文件中有表格,需要一个单元格一个单元格的读.如果实在不行,读取单行也行.希望大家帮我解决一下.谢谢了.(补充一下.服务器是用的linux操作系统)
展开
2个回答
2013-06-16
展开全部
可以使用PDFBOX0.7.3控件:
import java.io.InputStream;import java.io.IOException;
import org.apache.lucene.document.Document;import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;import org.pdfbox.util.PDFTextStripper;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {COSDocument cosDoc = null;<br/> try {cosDoc = parseDocument(is);<br/> } catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("该PDF文档是加密文档,无法处理");
}
String docText = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
PDDocument pdDoc = null;
try {pdDoc = new PDDocument(cosDoc);<br/> PDDocumentInformation docInfo = pdDoc.getDocumentInformation();<br/> if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){<br/> title = docInfo.getTitle();}
} catch (Exception e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
System.err.println("无法取得该PDF文档的元数据" + e.getMessage());
} finally {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}
return null;
}
private static COSDocument parseDocument(InputStream is) throws IOException {
PDFParser parser = new PDFParser(is);parser.parse();return parser.getDocument();
}
private void closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {try {cosDoc.close();} catch (IOException e) {}
}}
private void closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
try { pdDoc.close();
} catch (IOException e) {
}}}
import java.io.InputStream;import java.io.IOException;
import org.apache.lucene.document.Document;import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;import org.pdfbox.util.PDFTextStripper;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {COSDocument cosDoc = null;<br/> try {cosDoc = parseDocument(is);<br/> } catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("该PDF文档是加密文档,无法处理");
}
String docText = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
PDDocument pdDoc = null;
try {pdDoc = new PDDocument(cosDoc);<br/> PDDocumentInformation docInfo = pdDoc.getDocumentInformation();<br/> if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){<br/> title = docInfo.getTitle();}
} catch (Exception e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
System.err.println("无法取得该PDF文档的元数据" + e.getMessage());
} finally {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}
return null;
}
private static COSDocument parseDocument(InputStream is) throws IOException {
PDFParser parser = new PDFParser(is);parser.parse();return parser.getDocument();
}
private void closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {try {cosDoc.close();} catch (IOException e) {}
}}
private void closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
try { pdDoc.close();
} catch (IOException e) {
}}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-16
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询