每行阅读PDF [英] Reading PDF per Line

查看:84
本文介绍了每行阅读PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用iText5 for .NET逐行读取的PDF文件
我通过互联网搜索但我只发现每页内容阅读PDF文件。

How can I read a PDF file line by line using iText5 for .NET? I have search through the internet but I only found reading PDF file per page content.

请参阅下面的代码。

public string ReadPdfFile(object Filename)
{

    string strText = string.Empty;
    try
    {
        PdfReader reader = new PdfReader((string)Filename);

        for (int page = 1; page <= reader.NumberOfPages; page++)
        {
            ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();

            String s = PdfTextExtractor.GetTextFromPage(reader, page, its);

            s = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(s)));
            strText = strText + s;

        }
        reader.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return strText;
}


推荐答案

试试这个,使用 LocationTextExtractionStrategy 而不是 SimpleTextExtractionStrategy 它会在返回的文本中添加换行符。然后,您可以使用strText.Split('\ n')将文本拆分为字符串[] ,并按行进行消费。

Try this, use theLocationTextExtractionStrategy instead of the SimpleTextExtractionStrategy it will add new line characters to the text returned. Then you can use strText.Split('\n') to split your text into a string[] and consume it on a per line basis.

这篇关于每行阅读PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆