Aboo Bolaky

{.NET, C#, Sitecore ...} Free your mind...

Indexing PDF documents with Lucene and Sitecore

clock January 13, 2010 21:32 by author Aboo Bolaky

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 :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Comments

Add comment


(Will show your Gravatar )  

  Country flag

biuquote
  • Comment
  • Preview
Loading



A b o u t M e

Annoying

Brilliant

Open and

Objective

in every way..
Only Human >>
 
"First learn computer science and all the theory.

Next develop a programming style.

Then forget all that and just hack." Carrette (1990)