SlideShow ss = new SlideShow(new HSLFSlideShow(is));//is 为文件的InputStream,建立SlideShow
Slide[] slides = ss.getSlides();//获得每一张幻灯片
for(int i=0;i<slides.length;i++){
TextRun[] t = slides[i].getTextRuns();//为了取得幻灯片的文字内容,建立TextRun
for(int j=0;j<t.length;j++){
content.append(t[j].getText());//这里会将文字内容加到content中去
}
content.append(slides[i].getTitle());
}
index.AddIndex(url, title, content.toString());
}catch(Exception ex){
System.out.println(ex.toString());
}
return null;
}
PDF:
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;
try {
cosDoc = parseDocument(is);
} 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);
PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){
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) {
}
}
}
责任编辑:小草