仅从PDF文档中获取字数

我希望以编程方式从pdf文档中获取单词count。

我看过PDFSharp,但它对于我想要做的事情来说是一个非常笨重的。 我无法访问服务器,所以我无法安装acrobat来访问他们的api或任何东西。 我愿意在iTextSharp或其他工具中做到这一点。

iTextSharp有一个很棒的PdfTextExtractor对象,可以获取所有文本(假设@Rob A指出它实际存储为文本而不是图像或纯矢量)。 一旦你得到了所有的文本,一个简单的RegEx将为你提供单词计数。

下面的代码应该为您完成。 (在iText 5.1.1.0上测试过)

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using iTextSharp.text.pdf.parser; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string InputFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Input.pdf"); //Get all the text string T = ExtractAllTextFromPdf(InputFile); //Count the words int I = GetWordCountFromString(T); } public static string ExtractAllTextFromPdf(string inputFile) { //Sanity checks if (string.IsNullOrEmpty(inputFile)) throw new ArgumentNullException("inputFile"); if (!System.IO.File.Exists(inputFile)) throw new System.IO.FileNotFoundException("Cannot find inputFile", inputFile); //Create a stream reader (not necessary but I like to control locks and permissions) using (FileStream SR = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { //Create a reader to read the PDF iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(SR); //Create a buffer to store text StringBuilder Buf = new StringBuilder(); //Use the PdfTextExtractor to get all of the text on a page-by-page basis for (int i = 1; i <= reader.NumberOfPages; i++) { Buf.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i)); } return Buf.ToString(); } } public static int GetWordCountFromString(string text) { //Sanity check if (string.IsNullOrEmpty(text)) return 0; //Count the words return System.Text.RegularExpressions.Regex.Matches(text, "\\S+").Count; } } } 

您可以使用pdf2text工具,然后计算单词:

工具pdf2text