Click here to Skip to main content
Licence CPOL
First Posted 9 Oct 2007
Views 33,969
Downloads 1,542
Bookmarked 32 times

PDF to Text

By | 9 Oct 2007 | Article
Convert a PDF to text.

Introduction

Looking around trying to find examples of how to extract text out of a PDF, I didn't find much. Well, there are a few, but cost money. I found an example done in Java, and converted it to VB.NET with add-ons and a different logic. The code in this application is very incomplete, and it will be eventually used in an automated process using a file watcher to extract text out of PDFs and then format the text to put it into a SQL Server database. I hope that some one finds this code and the recommend changes or updates useful.

Using the code

The code is pretty easy to use. Both the test functions are stored in a class ExtractPDF. The function to extract the text requires a PDF file name and a password. The password can be Nothing and will be ignored. If the PDF file has a password, a valid password needs to be converted to Bytes and then passed. ItextSharp.dll needs to be referenced. The source code files for itextsharp.dll are also available.

I have two Case statements in the function, so new or more options/formats or whatever else comes in a PDF file can be read and the appropriate action taken.

While (Token.NextToken)
    Select Case Token.TokenType
        Case Token.TK_STRING
            StrBuf.Append(Token.StringValue)
        Case Token.TK_OTHER
            ' What to do with other characters
            Select Case Token.StringValue
                Case "ET"
                    StrBuf.Append(vbCrLf)
            End Select
            'Could add more here
    End Select
End While

Update

I have updated the program and figured out why I was getting the cast error. Sometimes the object is returned as an array and not individually. There is probably a smarter way to get it right with one loop, but I store the streams in an ArrayList and process it later:

Dim Stream As New ArrayList

If objectref.IsArray Then
    Dim Counter As Integer

    For Counter = 0 To objectref.ArrayList.Count - 1
        Stream.Add(Reader.GetPdfObject(objectref.arraylist(Counter)))
    Next
Else
    Stream.Add(Reader.GetPdfObject(objectref))
End If

This code is far from complete, but I thought that it would help some VB programmer out there as the other examples I found where in C# (funny that ItextSharp.dll is all written in C#). If any body has any additions, please feel free to use the code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RG_SA



South Africa South Africa

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:13 9 Feb '12  
GeneralMy vote of 5 Pinmembermyaccountram9:20 14 Aug '10  
GeneralHelp, problematic text extraction PinmemberMember 336444020:09 27 Oct '08  
GeneralRe: Help, problematic text extraction Pinmemberrhwiebe8:02 19 Nov '08  
GeneralFree Text Mining Tool that can convert PDF files to text PinmemberVitaliy Petrenko22:20 23 Nov '07  
GeneralRe: Free Text Mining Tool that can convert PDF files to text Pinmemberrhwiebe8:06 19 Nov '08  
GeneralWill not read some files PinmemberDarcy J Williamson7:40 5 Nov '07  
AnswerRe: Will not read some files PinmemberRG_SA18:59 5 Nov '07  
GeneralRe: Will not read some files PinmemberDarcy J Williamson19:34 5 Nov '07  
QuestionSpecified cast is not valid Pinmembercoppocks7:59 9 Oct '07  
AnswerRe: Specified cast is not valid PinmemberRG_SA1:47 10 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Oct 2007
Article Copyright 2007 by RG_SA
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid