Avatar of Taras
Taras
Flag for Canada asked on

VBA convert or transfer pdf file to text file.

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?
Microsoft AccessVBA

Avatar of undefined
Last Comment
Scott McDaniel (EE MVE )

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Scott McDaniel (EE MVE )

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Nick67

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Taras

ASKER
thanks a lot.
Scott McDaniel (EE MVE )

.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.
Nick67

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Scott McDaniel (EE MVE )

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).
Nick67

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 :(
Scott McDaniel (EE MVE )

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.