How to find text from pdf image?

好久不见. 提交于 2020-01-01 03:41:07

问题


I am developing a C# application in which I am converting a PDF document to an image and then rendering that image in a custom viewer.

I've come across a bit of a brick wall when trying to search for specific words in the generated image and I was wondering what the best way to go about this would be. Should I find the x,y location of searched word?


回答1:


You can use tessract OCR image for text recognition in console mode.

I don't know about such SDK for pdf.

BUT, if you want to get all word coordinates and values, you can use next my not complex code, thank nguyenq for hocr hint:

public void Recognize(Bitmap bitmap)
{
    bitmap.Save("temp.png", ImageFormat.Png);
    var startInfo = new ProcessStartInfo("tesseract.exe", "temp.png temp hocr");
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    var process = Process.Start(startInfo);
    process.WaitForExit();

    GetWords(File.ReadAllText("temp.html"));

    // Futher actions with words
}

public Dictionary<Rectangle, string> GetWords(string tesseractHtml)
{
    var xml = XDocument.Parse(tesseractHtml);

    var rectsWords = new Dictionary<System.Drawing.Rectangle, string>();

    var ocr_words = xml.Descendants("span").Where(element => element.Attribute("class").Value == "ocr_word").ToList();
    foreach (var ocr_word in ocr_words)
    {
        var strs = ocr_word.Attribute("title").Value.Split(' ');
        int left = int.Parse(strs[1]);
        int top = int.Parse(strs[2]);
        int width = int.Parse(strs[3]) - left + 1;
        int height = int.Parse(strs[4]) - top + 1;
        rectsWords.Add(new Rectangle(left, top, width, height), ocr_word.Value);
    }

    return rectsWords;
}



回答2:


Use ITextSharp download it here. Make sure the PDF is searchable.

and use this code:

public static string GetTextFromAllPages(String pdfPath)
{
    PdfReader reader = new PdfReader(pdfPath); 

    StringWriter output = new StringWriter();  

    for (int i = 1; i <= reader.NumberOfPages; i++) 
        output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));

    return output.ToString();
}


来源:https://stackoverflow.com/questions/12577752/how-to-find-text-from-pdf-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!