How to use the ABCPdf.NET to extract texts from al

2019-02-10 23:45发布

问题:

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,

回答1:

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


回答2:

Have you tried the GetText method?



回答3:

doc.Read(.......);
var textOperation = new TextOperation(doc);
textOperation.PageContents.AddPages();
string allText = textOperation.GetText();