VBA convert or transfer pdf file to text file.

Taras
Taras used Ask the Experts™
on
I am using Access 2010.I have pdf file that have a table look data in. I need to covert or transfer that pdf file in text file using VBA and then import it in Access db without Adobe Acrobat installed on pc  and not using 3th party software.
If it is possible through VBA and how?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
No, it's not possible through VBA. VBA has no builtin capabilities to do that, so you'd have to have some form of 3rd party software, Abode or otherwise, to do this.
Most Valuable Expert 2014
Commented:
No dice.
Installing Acrobat gets you some libraries that VBA can use to do fun things with PDFs
.NET can create PDF without external references -- I've built ASP.NET pages that do that -- so I presume that you could use Visual Studio to build your own dll to reverse that process and suck content out of a PDF whose structure you understand, but...

straight VBA to open and convert PDFs?

Nope.  Not happening.

Author

Commented:
thanks a lot.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2012
Top Expert 2014

Commented:
.NET can create PDF without external references
Can you explain that a bit more? I'm curious how to do that in .NET, since I often create PDFs in .NET and would love to not deploy anything to do that.
Most Valuable Expert 2014

Commented:
There's the free iTextSharp library

Here's the guts of the code.
Note this is for reference.
It's not nearly workable in this format for you -- but the ideas and keywords to Google to there
The iTextSharp documentation for .Net is pretty sparse, but basically you create a document and add paragraphs, tables and images to it.  you close the writer and document and then redirect the browser to the newly written PDF.
It took me quite a while to wrap my head around the persnickety syntax -- and generating a chart as an image to be added -- but it works very well

Imports iTextSharp.text
Imports iTextSharp.text.pdf

    Protected Function BuildUTReadingsPDF() As String
        Dim TheDoc As New Document(PageSize.LETTER.Rotate, 18, 18, 18, 18)
        Dim url As String = "usr/UT" & Now.ToFileTime & ".pdf"

        'also stymied on usercontrol placeholder repeater updatepanel problem
        'placeholder control gets no intellisense on the update panel

        Dim TheFinalFile As New FileStream(Server.MapPath(url), FileMode.Create)
        Dim TheWriter = PdfWriter.GetInstance(TheDoc, TheFinalFile)

        Dim titleFont = FontFactory.GetFont("Tahoma", 10, iTextSharp.text.Font.BOLD)
        Dim headFont = FontFactory.GetFont("Tahoma", 10)
        Dim subtitleFont = FontFactory.GetFont("Tahoma", 9, iTextSharp.text.Font.BOLD)
        Dim detailFont = FontFactory.GetFont("Tahoma", 8)
        Dim GridheadFont = FontFactory.GetFont("Tahoma", 8, iTextSharp.text.Color.WHITE)
        Dim GridFont = FontFactory.GetFont("Tahoma", 8, iTextSharp.text.Color.BLACK)
        'All right All right All righht!
        'It creates and opens a PDF
        ' Now to get it to be the same as the report! 
        Dim paraClient As New Paragraph("Client Name", titleFont)
        Dim paraName As New Paragraph(Me.theClientName.Text)
        Dim mytable = New iTextSharp.text.Table(UTReadings.Columns.Count)
        mytable.Cellpadding = 2
        mytable.Width = 100
        'Set the column widths
        'in points
        '72 points to the inch
        'I've tried various fiddle-farts to do this dynamically
        'because the gridview is dynamic, you can't crank around its widths
        'they evaluate as zero
        'so I coded them
        Dim TheWidths As Integer() = New Integer(UTReadings.Columns.Count - 1) {}
        TheWidths(0) = 45
        TheWidths(1) = 60
        TheWidths(2) = 60
        TheWidths(3) = 50
        TheWidths(4) = 21
        TheWidths(5) = 21
        TheWidths(6) = 21
        TheWidths(7) = 21
        TheWidths(8) = 21
        TheWidths(9) = 21
        TheWidths(10) = 21
        TheWidths(11) = 21
        TheWidths(12) = 85
        TheWidths(13) = 85
        TheWidths(14) = 35
        TheWidths(15) = 35


        'Transfer rows from GridView to table
        For i As Integer = 0 To UTReadings.Columns.Count - 1
            Dim cellText As String = Server.HtmlDecode(UTReadings.Columns(i).HeaderText)
            Dim cell As New Cell(New Phrase(cellText, GridheadFont))
            cell.BackgroundColor = New iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000080"))

            mytable.AddCell(cell)
        Next

        For i As Integer = 0 To UTReadings.Rows.Count - 1
            If UTReadings.Rows(i).RowType = DataControlRowType.DataRow Then
                For j As Integer = 0 To UTReadings.Columns.Count - 1
                    Dim cellText As String = Server.HtmlDecode(UTReadings.Rows(i).Cells(j).Text)
                    'Dim cell As New iTextSharp.text.Cell(cellText)
                    Dim cell As New Cell(New Phrase(cellText, GridFont))
                    'Set Color of Alternating row
                    If i Mod 2 <> 0 Then
                        cell.BackgroundColor = New iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#DCDCDC "))
                    End If
                    mytable.AddCell(cell)
                Next
            End If
        Next

        For i As Integer = 0 To UTReadings.Columns.Count - 1
            Dim cellText As String = String.Empty 'TheWidths(i).ToString
            Dim cell As New Cell(New Phrase(cellText, GridheadFont))
            cell.BackgroundColor = New iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000080"))
            mytable.AddCell(cell)
        Next
        mytable.SetWidths(TheWidths)
        Dim clsTheHeaderImage As New clsHeaderImage
        Dim TheHeaderImage = clsTheHeaderImage.TheHeaderImage
        TheHeaderImage.ScaleToFit(216, 36)
        TheHeaderImage.SetAbsolutePosition(TheDoc.PageSize.Width - 18 - (3 * 72), TheDoc.PageSize.Height - 54)
        TheDoc.Open()
        With TheDoc
            .Add(paraClient)
            .Add(paraName)
            .Add(mytable)
            If clsTheHeaderImage.Gronk.ToString = String.Empty Then
                .Add(TheHeaderImage)
            Else
                Dim theGronk As New Paragraph(clsTheHeaderImage.Gronk)
                .Add(theGronk)
            End If
        End With

        TheDoc.Close()
        TheWriter.Close()

        'testing code kicks this out to new window
        'Dim script As String = "window.open(""{0}"", ""{1}"");"
        'url = Page.ResolveClientUrl(url)
        'script = String.Format(script, url, "_blank")
        'ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Redirect", script, True)

        BuildUTReadingsPDF = url



    End Function

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
Hmmm .... I was hoping I'd overlooked something in the .NET Framework, but iTextSharp is just like Adobe, PDFSHarp, etc (i.e. it's a 3rd party/external reference, which means it must be deployed to the target machine).

I use PDFSharp, which is very much like iTextSharp (and I believe is an offshoot), so unfortunately I'd be in the same boat with iTextSharp (i.e. I'd have to deploy that 3rd party library, just as I do with PDFSharp).
Most Valuable Expert 2014

Commented:
We're in different boats.
I wear all the hats and it's for my outfit, so deployment on our own server wasn't that painful to start with.
The dll is 3.6 MB

Sorry to have gotten your hopes up :(
Most Valuable Expert 2012
Top Expert 2014

Commented:
Right, but you're still using an external reference. The iTextSharp library isn't part of the .NET Framework, so the machine still must have the correct libraries installed (regardless of the size). Once you've done that, you can then set the reference and use the Imports statement as you've done.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial