5

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

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();

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.

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

Thanks,

The Light
  • 26,341
  • 62
  • 176
  • 258
  • 5
    you should post what you have tried so far.... – Parv Sharma Jun 12 '12 at 10:53
  • ok, added more details! remove your negativity! – The Light Jun 12 '12 at 13:23
  • @Thelight It's not "negativity" and downvoting isn't personal. Originally I downvoted because it was a one line question that didn't convey much thought had gone into it other than the very base requirement of actually asking a question. I've removed my downvote now as it's apparent you are working towards it and feeding back, and have added more to the question itself. – Adam Houldsworth Jun 13 '12 at 10:52

3 Answers3

12

For your benefit, yes you!

 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();
    }
The Light
  • 26,341
  • 62
  • 176
  • 258
  • What datatype is "doc"? The line "var doc = new Doc()" ist not working. Can you please tell me, what the necessary using-directive is? – Michael Hutter Jan 29 '19 at 09:42
1

Have you tried the GetText method?

John Koerner
  • 37,428
  • 8
  • 84
  • 134
1
doc.Read(.......);
var textOperation = new TextOperation(doc);
textOperation.PageContents.AddPages();
string allText = textOperation.GetText();
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
  • 1
    Note this will not work in previous versions of Abcpdf, such as version 8. It looks like the `TextOperation` class might've been added in version 10 or thereabouts. – clamum Apr 27 '17 at 14:21