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,
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();
    }
Have you tried the GetText method?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With