【问题标题】:Get ONLY word count from PDF document仅从 PDF 文档中获取字数
【发布时间】:2011-10-07 17:48:35
【问题描述】:

我希望以编程方式仅从 pdf 文档中获取字数。

我查看了 PDFSharp,但它对于我想做的事情来说非常庞大。我无权访问服务器,所以我无法安装 acrobat 来访问他们的 api 或任何东西。我愿意在 iTextSharp 或其他工具中这样做。

【问题讨论】:

  • 您在寻找什么样的成功率?过去我遇到过从扫描图像创建 pdf 的问题,这在某些时候基本上需要 OCR,这有它自己的一系列问题
  • 成功率并不重要...这是针对对翻译报价进行最佳猜测的翻译网站。 ToU 上到处都是白话,说报价不是合同。此外,通常在该行业中,图像不被认为是“可翻译的”(这是一个非常不宽容的行业)。 :)

标签: c# pdf itextsharp


【解决方案1】:

iTextSharp 有一个很棒的PdfTextExtractor 对象,它可以获取所有文本(假设@Rob A 指出它实际上存储为文本而不是图像或纯矢量)。一旦你得到了所有的文本,一个简单的正则表达式就会给你字数。

下面的代码应该可以为您完成。 (在 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;
        }
    }
}

【讨论】:

  • Hass:太棒了,我今晚会插入并标记它已回答...致@peer:如果可以的话,我会去开源...
【解决方案2】:

你可以使用pdf2text工具,然后数字数:

tools pdf2text

【讨论】:

  • 谢谢,上面提到我会尝试开源......但是对于我的问题,这是轻量级的。
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 2010-10-17
  • 2014-05-09
相关资源
最近更新 更多