C#读取PDF文件的文本内容

public static string ReadPdfContent(string filepath)
        {
            try
            {
                string pdffilename = filepath;
                PdfReader pdfReader = new PdfReader(pdffilename);
                int numberOfPages = pdfReader.NumberOfPages;
                StringBuilder text = new StringBuilder();
                for (int i = 1; i <= numberOfPages; ++i)
                {
                    text.Append(iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, i));
                }
                pdfReader.Close();
                return text.ToString();
            }
            catch (Exception ex)
            {
                return "原因:" + ex.ToString();
            }
        }

注:此方法需要引用iTextSharp

你可能感兴趣的:(C#编程语言,c#,pdf,开发语言)