Indexing PDF documents with Lucene and Sitecore

by aboo bolaky 14. January 2010 06:32

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

private string ParsePDF(MediaItem mediaItem)
    {
        Stream stream = mediaItem.GetMediaStream();
        ikvm.io.InputStreamWrapper wrapper = new ikvm.io.InputStreamWrapper(stream);
        PDDocument doc = PDDocument.load(wrapper);
        PDFTextStripper stripper = new PDFTextStripper();
        return stripper.getText(doc);

    }

Revised Code

private string ParsePDF(MediaItem mediaItem)
    {
        PDDocument doc = null;
        ikvm.io.InputStreamWrapper wrapper = null;

        try
        {
            Stream stream = mediaItem.GetMediaStream();
            wrapper = new ikvm.io.InputStreamWrapper(stream);
            doc = PDDocument.load(wrapper);
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(doc);
        }
        catch (Exception Ex)
        {
            //[some logging here] ..
            return String.Empty;
        }
        finally
        {
            if ((doc != null) && (wrapper != null))
            {
                doc.close();
                wrapper.close();
            }
        }
    }


 

Rebuild the index and have a good night sleep :)

Tags:

Comments

5/19/2010 12:19:34 PM #

latin shoes

thanks for your article

latin shoes United States |

5/19/2010 12:28:19 PM #

latin shoes

thanks for your article

latin shoes United States |

5/19/2010 12:30:21 PM #

latin shoes

thanks for your article

latin shoes United States |

5/19/2010 3:45:54 PM #

Dress Up Games

Finally indexing problem is solve. Thanks for this solution!

Dress Up Games United States |

5/19/2010 11:07:20 PM #

sizegenetics

Thnx, interesting

sizegenetics United States |

2/16/2011 1:43:52 AM #

Fab

You should test separately if doc or wrapper is null, to close them separately
Otherwise you can have the case if one is null, the other one will not be closed.
OK it's a wierd case, but it can theoretically happend

Fab Belgium |

11/7/2011 4:31:12 PM #

Marrakech ici

Withhold up the renowned slice of process, I study some stipulations on this internet scene moreover I suppose that your texture scene is rattling stimulating moreover has lays of sizzling tidings.

Marrakech ici France |

11/7/2011 6:29:27 PM #

Tout Marrakech

Yay google is my empress aided me to locate this monumental website ! . “Don’t guide absent active accompanying your helps. It does negative prohibit using your pate.” by Andy Rooney.

Tout Marrakech France |

11/13/2011 3:56:46 PM #

Guide Marrakech

You retain brought up a much impressive cruxs , thankyou for the courier. “The grand object is, that every man be armed. … Every one who is able may have a gun.” by Patrick Henry.

Guide Marrakech France |

11/13/2011 6:06:56 PM #

voir Marrakech

I was looking at some of your blog enters on this internet spot and I understand this snare spot is rattling instructive! Hold posting .

voir Marrakech France |

Tag cloud

Flash Player 9 required.

About Me

I wish I could write something here..
//TODO: ElaborateMe