iTextSharp,其实很多时候都是用于生成PDF,但是他读取PDF的能力也不差,使用如下:
1、下载iTextSharp
2、引用动态链接库
解压缩下载的压缩包里面的itextsharp-dll-core.zip,得到itextsharp.dll,在项目中添加引用itextsharp.dll即可
在文件中需要引入以下3个命名空间:
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
3、API的使用方法看代码:
[csharp]
private string OnCreated(string filepath)
{
try
{
string pdffilename = filepath;
PdfReader pdfReader = new PdfReader(pdffilename);
int numberOfPages = pdfReader.NumberOfPages;
string text = string.Empty;
for (int i = 1; i <= numberOfPages; ++i)
{
byte[] bufferOfPageContent = pdfReader.GetPageContent(i);
text += System.Text.Encoding.UTF8.GetString(bufferOfPageContent);
}
pdfReader.Close();
return text;
}
catch (Exception ex)
{
StreamWriter wlog = File.AppendText(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"\\mylog.log");
wlog.WriteLine("出错文件:" + e.FullPath + "原因:" + ex.ToString());
wlog.Flush();
wlog.Close();return null;
}
1