如何使用ABCPdf.NET从PDF文件中的所有页面中提取文本? [英] How to use the ABCPdf.NET to extract texts from all pages of a PDF file?

查看:217
本文介绍了如何使用ABCPdf.NET从PDF文件中的所有页面中提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用ABCPdf.NET工具从PDF文件中提取文本的内容?

How to use the ABCPdf.NET tool to extract the content texts from a PDF file?

我试过了gettext方法,但不提取的内容:

I tried the GetText method but doesn't extract the contents:

var doc = new Doc();    

        var url = @".../FileName.pdf";

        doc.Read(url);

        string xmlContents = doc.GetText("Text");
        Response.Write(xmlContents);
        doc.Clear();
        doc.Dispose();

我的PDF有近1000字,但一个Gettext只返回4-5的话。我意识到它返回的第一页只有文字。

My pdf has almost 1000 words but the GetText only returns 4-5 words. I realized it returns only the texts of the first page.

因此​​,问题应该是如何提取从PDF文件的所有页面的文字? - (改标题,以使其更清晰)

So the question should be "how to extract the text from all pages of a pdf file?" -(changed the Title to make it clearer).

谢谢

推荐答案

有关您的利益,是你!

 public string ExtractTextsFromAllPages(string pdfFileName)
    {
        var sb = new StringBuilder();

        using (var doc = new Doc())
        {
            doc.Read(pdfFileName);

            for (var currentPageNumber = 1; currentPageNumber <= doc.PageCount; currentPageNumber++)
            {
                doc.PageNumber = currentPageNumber;
                sb.Append(doc.GetText("Text"));
            }
        }

        return sb.ToString();
    }

如果您还没有网址,但有个字节,则:

if you don't have the url but have the bytes, then:

public string ExtractTextsFromAllPages(Byte[] pdfBytes)
    {
        var sb = new StringBuilder();

        using (var doc = new Doc())
        {
            doc.Read(pdfBytes);

            for (var currentPageNumber = 1; currentPageNumber <= doc.PageCount; currentPageNumber++)
            {
                doc.PageNumber = currentPageNumber;
                sb.Append(doc.GetText("Text"));
            }
        }

        return sb.ToString();
    }

这篇关于如何使用ABCPdf.NET从PDF文件中的所有页面中提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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