How to use the ABCPdf.NET to extract texts from all pages of a PDF file?
Asked Answered
M

3

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,

Melissa answered 12/6, 2012 at 10:52 Comment(3)
you should post what you have tried so far....Citizen
ok, added more details! remove your negativity!Melissa
@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.Mercaptide
M
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();
    }
Melissa answered 12/6, 2012 at 13:47 Comment(1)
What datatype is "doc"? The line "var doc = new Doc()" ist not working. Can you please tell me, what the necessary using-directive is?Tolmach
E
1

Have you tried the GetText method?

Endurant answered 12/6, 2012 at 10:56 Comment(0)
L
1
doc.Read(.......);
var textOperation = new TextOperation(doc);
textOperation.PageContents.AddPages();
string allText = textOperation.GetText();
Lantz answered 26/6, 2015 at 15:1 Comment(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.Shafer

© 2022 - 2024 — McMap. All rights reserved.