Getting Lucene to index pdf document is a doddle, thanks to the document published on SDN. if you follow the instructions to the letter, you will be able to make Lucene index pdf documents in no time
HOWEVER, please be aware that you have to properly dispose the objects (to name a few... PDDocument and Ikvm.io.InputStreamWrapper objects) after you parsed the document(s).
If you forget to do so, you will soon notice that your temp folder (c:\windows\temp) will be filled with pdfbox temp files (e.g pdfbox124bb19809ftmp). In the worst case scenario, the C drive runs out of disk space => no website.
Original Code Published on SDN
1: private string ParsePDF(MediaItem mediaItem)
2: {
3: Stream stream = mediaItem.GetMediaStream();
4: ikvm.io.InputStreamWrapper wrapper = new ikvm.io.InputStreamWrapper(stream);
5: PDDocument doc = PDDocument.load(wrapper);
6: PDFTextStripper stripper = new PDFTextStripper();
7: return stripper.getText(doc);
8: }
Revised Code
1: private string ParsePDF(MediaItem mediaItem)
2: {
3: PDDocument doc = null;
4: ikvm.io.InputStreamWrapper wrapper = null;
5: try
6: {
7:
8: Stream stream = mediaItem.GetMediaStream();
9: wrapper = new ikvm.io.InputStreamWrapper(stream);
10: doc = PDDocument.load(wrapper);
11: PDFTextStripper stripper = new PDFTextStripper();
12:
13: return stripper.getText(doc);
14: }
15: catch (Exception Ex)
16: {
17: //[some logging here] ..
18: return String.Empty;
19: }
20: finally
21: {
22: if ((doc != null) && (wrapper != null))
23: {
24: doc.close();
25: wrapper.close();
26: }
27: }
28: }
Rebuild the index and have a good night sleep :)