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 |

Tag cloud

Flash Player 9 required.

About Me

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