得到PD​​F文件只字数 [英] Get ONLY word count from PDF document

查看:61
本文介绍了得到PD​​F文件只字数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从PDF文档中得到的只是字数编程。

I was hoping to get just the word count from a pdf document programmatically.

我看着PDFSharp,但它是什么我想要做的awefully笨重。我没有对服务器的访问,所以我不能安装Acrobat才能到他们的API或任何东西。我愿意做它iTextSharp的或其他工具。

I've looked at PDFSharp, but it's awefully bulky for what I want to do. I don't have access to the server, so I can't install acrobat to get to their api's or anything. I'd be willing to do it in iTextSharp or another tool.

推荐答案

iTextSharp的有一个美好的 PdfTextExtractor 对象,将让你的所有文本(如assumming一个@Rob指出,其实际存储为文本,而不是图片或纯矢量)。一旦你得到了所有的文字简单的regex会给你的字数。

iTextSharp has a wonderful PdfTextExtractor object that will get you all of the text (assumming as @Rob A pointed out that its actually stored as text and not images or pure vector). Once you've got all of the text a simple RegEx will give you the word count.

下面的代码应该为你做。 (测试iText的5.1.1.0)

The code below should do it for you. (Tested on 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;
        }
    }
}

这篇关于得到PD​​F文件只字数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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