7 Replies Latest reply: Jul 3, 2012 12:13 PM by Five_Fingers RSS

    VBS Import txt File

    Five_Fingers

      I'm try to work out a VBscript solution to import an entire text file (I can do it manually) into a pdf form. I'm having trouble finding any documentation on an appropriate AVDoc object.

        • 1. Re: VBS Import txt File
          Test Screen Name MVP

          Not sure why you can't just read the text file and set the form field from the text in it?

          • 2. Re: VBS Import txt File
            Five_Fingers Community Member

            Thanks for the reply. I'm not sure I understand your question. I'm trying to automate the text file import process that I've confirmed works manually (i.e., mouse-click in the menu to select the import of a txt file). When I test a manual import, all the form fields are propagated properly with the txt file data.

             

            A person could physically repeat this process for every text file that needed importing, however, I'm trying to automate this process to make it less labor intensive and more simple for the end user. Hope this makes sense. The closest example I've come across so far is the following code, but I don't understand parts of it and can't find any reference for the objects used.

             

             

            Dim bok As Long

            Dim acroexchapp As Acrobat.CAcroApp

            Dim acroexchavdoc As Acrobat.CAcroAVDoc

            Dim aformaut As AFORMAUTLib.AFormApp

            Dim j As Integer

             

            Set acroexchapp = CreateObject("Acroexch.app")

            Set acroexchavdoc = CreateObject("Acroexch.avdoc")

             

            bok = acroexchavdoc.Open(Path)

             

            Dim Fld As AFORMAUTLib.Field

            Dim Flds As AFORMAUTLib.Fields

             

            If (bok) Then

            Set aformaut = CreateObject("AFormAut.App")

            Set Flds = aformaut.Fields

            End If

             

            For each fld in Flds

            Set Fld = Flds(field)

            Fld.Value = value

            Next

             

            acroexchapp.Show

            • 3. Re: VBS Import txt File
              Test Screen Name MVP

              Is this an Acroform (made in Acrobat) or an XFA (XML) form (made in Designer)?

              Do you have the Acrobat SDK?

              • 4. Re: VBS Import txt File
                Five_Fingers Community Member

                Unfortunately, I'm an end user for the pdf form, so I'm not sure how it was created. It seems to be an Acroform (made in Acrobat).

                 

                I haven't downloaded Acrobat SDK, but I've read through a lot of the support materials looking for answers. My research seems to indicate that what I'm trying to code is possible as long as the end user is opening the form in Acrobat and not just Reader.

                • 5. Re: VBS Import txt File
                  Test Screen Name MVP

                  The Acrobat SDK is what you need. It is mostly documentation, and does include the Acrobat OLE interfaces.

                   

                  Now, I'm sure there is no direct OLE method for this. The OLE methods haven't been touched in years.

                   

                  BUT, Acrobat JavaScript might be able to do this - don't know. If it can, you can use JavaScript from OLE.

                   

                  Don't assume that just because there is a function to do something in Acrobat that there is a corresponding method to automate it though.

                  • 6. Re: VBS Import txt File
                    Five_Fingers Community Member

                    Okay. Thanks for the fast reply and info. I'll take a look.

                     

                    UPDATE:

                     

                    I've found exactly what I need... in VB. The rest of my solution is in VBScript. I'm trying to figure out my options from this point. Any thoughts? Thanks again for your help.

                     

                     


                     

                    Option Strict Off

                    Option Explicit On

                    Module EntryPointJSObjectAccessVB

                     

                        Public jsObj As Object

                        Private Const NUM_ROWS As Short = 7

                     

                        Public Sub Main()

                            Dim SOURCE_DOCUMENT As String = objShell.ExpandEnvironmentStrings("%Temp%\Form.pdf")

                            Dim DATA_FILE As String = objShell.ExpandEnvironmentStrings("%Temp%\import_data.txt")

                     

                            ' create at desktop

                            Dim OUTPUT_FOLDER As String = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\New Files\")

                            System.IO.Directory.CreateDirectory(OUTPUT_FOLDER)

                            Dim outputString As String

                            Dim i As Short

                     

                            ' We gain access to the JavaScript DOM through the GetJSObject method

                            ' of the CAcroPDDoc object.

                     

                            On Error GoTo ErrorHandler

                     

                            Dim retVal As Integer

                            Dim count As Integer

                            Dim srcDoc As Acrobat.CAcroPDDoc

                     

                            ' Open the source PDF document

                     

                            srcDoc = CreateObject("AcroExch.PDDoc")

                            retVal = srcDoc.Open(SOURCE_DOCUMENT)

                            If retVal <> -1 Then

                                Err.Description = "Failed to open source document. Make sure that SOURCE_DOCUMENT has been configured."

                                GoTo ErrorHandler

                            End If

                     

                            ' Acquire the JSObject interface

                     

                            jsObj = srcDoc.GetJSObject

                     

                            ' For each row in data.txt, as defined by the NUM_ROWS constant, we:

                            '

                            ' 1. Spawn the form template.

                            ' 2. Populate the form with the data from data.txt.

                            ' 3. Save the PDF.

                     

                            Dim newDoc As Object

                            Dim template As Object

                            For i = 1 To NUM_ROWS

                     

                     

                                ' Acquire the form template and spawn a new page

                                template = jsObj.getTemplate("Templates:1")

                                If template Is Nothing Then

                                    Err.Description = "Failed to acquire form template. Aborting..."

                                    GoTo ErrorHandler

                                End If

                                template.spawn(1, False, False)

                     

                                ' Extract the page and populate the form with the data

                                newDoc = jsObj.extractPages(1)

                                newDoc.importTextData(DATA_FILE, i - 1)

                     

                                ' Save the new PDF to the output folder

                                outputString = OUTPUT_FOLDER & "Output" & i & ".pdf"

                                newDoc.saveAs(outputString)

                                newDoc.closeDoc()

                            Next

                     

                    EndSub:

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(jsObj)

                            jsObj = Nothing

                            If Not srcDoc Is Nothing Then

                                srcDoc.Close()

                                System.Runtime.InteropServices.Marshal.ReleaseComObject(srcDoc)

                                srcDoc = Nothing

                            End If

                            Exit Sub

                     

                    ErrorHandler:

                     

                            MsgBox("Error Occured: " & Err.Description)

                            Resume EndSub

                     

                        End Sub

                    End Module

                    • 7. Re: VBS Import txt File
                      Five_Fingers Community Member

                      SOLVED:

                       

                      I've finalized my solution by incorporating the VBScript element entirely into VB. The function of the following VB code accomplishes what I needed (Data in the clipboard is converted from Unicode to ASCII and saved as a text file. The text file is then imported into the pdf form.). Hope this is useful to others and saves them some time.

                       


                       

                      Option Strict Off

                      Option Explicit On

                      Module JSObjectAccessVB

                       

                          Public Sub tEXTdATA()

                       

                              Dim objFSO, objHTML, objOutputFile, strClipboardText

                              Dim strSource As String = Environment.GetEnvironmentVariable("Temp") + "output.txt"

                              Const ForAppending = 8

                       

                              'Capture clipboard text and save as ASCII text file in Temp folder

                       

                              objFSO = CreateObject("Scripting.FileSystemObject")

                              objHTML = CreateObject("htmlfile")

                              objOutputFile = objFSO.CreateTextFile(strSource, ForAppending, False)

                              strClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text")

                              objOutputFile.Write(strClipboardText)

                              objOutputFile.Close()

                       

                          End Sub

                          Public Sub PDFsAVE()

                       

                              Dim jsObj As Object

                              Dim SOURCE_DOCUMENT As String = Environment.GetEnvironmentVariable("Temp") + "\Form_blank.pdf"

                              Dim DATA_FILE As String = Environment.GetEnvironmentVariable("Temp") + "\output.txt"

                              Dim OUTPUT_FILE As String = Environment.GetEnvironmentVariable("Temp") + "\Form_filled.pdf"

                              Const NUM_ROWS As Short = 7

                       

                              ' Gain access to the JavaScript DOM through the GetJSObject method of the CAcroPDDoc object.

                       

                              On Error GoTo ErrorHandler

                       

                              Dim retVal As Integer

                              Dim srcDoc As Acrobat.CAcroPDDoc

                       

                              ' Open the source PDF document

                       

                              srcDoc = CreateObject("AcroExch.PDDoc")

                              retVal = srcDoc.Open(SOURCE_DOCUMENT)

                              If retVal <> -1 Then

                                  Err.Description = "Failed to open source document. Make sure that SOURCE_DOCUMENT has been configured."

                                  GoTo ErrorHandler

                              End If

                       

                              ' Acquire the JSObject interface

                       

                              jsObj = srcDoc.GetJSObject

                       

                              ' Extract the page and populate the form with the data

                       

                              If System.IO.File.Exists(DATA_FILE) = False Then

                                  MsgBox("proposal_output.txt file missing!", MsgBoxStyle.Critical, "File Not Found")

                                  Application.Exit()

                              End If

                       

                              jsObj.importTextData(DATA_FILE, 0)

                       

                              ' Save the new filled PDF file

                       

                              jsObj.saveAs(OUTPUT_FILE)

                       

                              ' Open new filled PDF file

                       

                              System.Diagnostics.Process.Start(OUTPUT_FILE)

                       

                      EndSub:

                              System.Runtime.InteropServices.Marshal.ReleaseComObject(jsObj)

                              jsObj = Nothing

                              If Not srcDoc Is Nothing Then

                                  srcDoc.Close()

                                  System.Runtime.InteropServices.Marshal.ReleaseComObject(srcDoc)

                                  srcDoc = Nothing

                              End If

                              Exit Sub

                       

                      ErrorHandler:

                       

                              MsgBox("Error Occured: " & Err.Description)

                              Resume EndSub

                       

                          End Sub

                       

                      End Module