zoukankan      html  css  js  c++  java
  • lucene.net搜索文档(pdf,doc,txt)内容

       public static void AddTxtDocument(string path, IndexWriter writer)
            {
                try
                {
                    
                    Document doc = new Document();
                    string StrContent;
                    if (Path.GetExtension(path).ToLower() == ".pdf")
                    {
                        StrContent = pdfToTxt(path);
                    }
                    else
                    {
                        using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
                        {
                            StrContent = sr.ReadToEnd();
                        }
                    }
                    doc.Add(new Field(CONTENT_KEY_NAME, StrContent, Field.Store.NO, Field.Index.ANALYZED));//内容
                    doc.Add(new Field(TITLE,Path.GetFileNameWithoutExtension(path) , Field.Store.YES, Field.Index.ANALYZED));//标题
                    doc.Add(new Field(FILE_KEY_NAME, path, Field.Store.YES, Field.Index.NO));//文件名
                    doc.Add(new Field(CREATEDATE, new FileInfo(path).LastWriteTime.ToString(), Field.Store.YES, Field.Index.NO));//创建时间
                    writer.AddDocument(doc);
                }
                catch (Exception)
                {
                    
                    throw;
                }
               
            }
    
            private static string   pdfToTxt(string pdffile)
            {
    
    
    
                PDDocument doc = PDDocument.load(pdffile);
    
    
    
                PDFTextStripper pdfStripper = new PDFTextStripper();
    
    
    
                return  pdfStripper.getText(doc);
    
            }

    lucene.net搜索pdf文件内容前,先要读取pdf文本,这必然要有一个转换,pdfbox就必不可少了,当然也还有其他方式(运行已有的exe),网上方法很多,

    只要能把pdf图片转为字符串,lucene.net就能搜索得到了。

    使用pdfbox需:

    1.下载pdfbox的dll

    2.再引用一下两个命名空间:

    using org.pdfbox.pdmodel;
    using org.pdfbox.util;

  • 相关阅读:
    pymoo: Multi-objective Optimization in Python
    读代码——NSGAII
    读论文——A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II
    神经网络入门00
    梯度下降pthon实现
    在线加解密工具
    安恒杯-一张谍报
    漏洞挖掘学习记录
    安恒杯-元数据存储
    安恒杯-babysql
  • 原文地址:https://www.cnblogs.com/bile/p/3117176.html
Copyright © 2011-2022 走看看