Quantcast
Channel: < I runat="server" / >
Viewing all articles
Browse latest Browse all 10

Indexing PDF documents with Lucene and Sitecore

$
0
0

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


Viewing all articles
Browse latest Browse all 10

Trending Articles