如何阅读 PDF 文件line by line
使用 iText5 for .NET?
我在互联网上搜索过,但我只找到了每页内容阅读 PDF 文件。
请看下面的代码。
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') 将文本拆分为string[]
并以每行为基础使用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)