Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Itextsharp text extraction

Tags:

c#

itextsharp

I'm using itextsharp on vb.net to get the text content from a pdf file. The solution works fine for some files but not for other even quite simple ones. The problem is that the token stringvalue is set to null (a set of empty square boxes)

token = New iTextSharp.text.pdf.PRTokeniser(pageBytes)
    While token.NextToken()
        tknType = token.TokenType()
        tknValue = token.StringValue

I can meassure the length of the content but I cannot get the actual string content.

I realized that this happens depending on the font of the pdf. If I create a pdf using either Acrobat or PdfCreator with Courier (that by the way is the default font in my visual studio editor) I can get all the text content. If the same pdf is built using a different font I got the empty square boxes.

Now the question is, How can I extract text regardless of the font setting?

Thanks

like image 945
Pakhu Avatar asked Jan 17 '11 08:01

Pakhu


1 Answers

complementary for Mark's answer that helps me a lot .iTextSharp implementation namespaces and classes are a bit different from java version

 public static string GetTextFromAllPages(String pdfPath)
    {
        PdfReader reader = new PdfReader(pdfPath); 

        StringWriter output = new StringWriter();  

        for (int i = 1; i <= reader.NumberOfPages; i++) 
            output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));

        return output.ToString();
    }
like image 59
Iman Avatar answered Oct 13 '22 23:10

Iman