Avatar of rogerdjr
rogerdjr
Flag for United States of America asked on

Editing an Acrobat .PDF file to add page footers from Access 2013

I have this subroutine that is supposed to add a page footer on every page of a group of PDF files - it works fine except the footer only appears on the 1st page.

Hopefully its a simple fix to add the footer to every page

I'm using Access 2013 and Acrobat XI Professional

I'd appreciate any help I can get.

Thanks
 ----------------------------
Private Sub TestButton_Click()

    Dim TestFunction As String, PathNm As String, FileNm As String, DestPathNm As String, CountFiles As Integer, MaxCountFiles As Double, MaxPgsPerFile, PathTest As String
    Dim StrSql As String, dbs As Database, rst As Recordset, Rst1 As Recordset
    Dim DoYouWantToSendToPrinter As Integer
    Dim SourcePath As String, DestPath As String, NoFiles As Integer, NoPages As Integer, DoYouWantToInclPathInFtr As Integer
    Dim FontSize As Integer, FontColor As String
   
    MsgBox "Make Sure Printer is not 'OFF-Line'" & vbNewLine & vbNewLine & "Make a Process to add a leader or trailer page" & vbNewLine & vbNewLine & "Make it so it can print in XX (e.g. 50 pages) page batches to take advantage of the stapler" & vbNewLine & vbNewLine & "Put all these options in a table and have a drop down menu"
   
    STRUSERID = GetNetUserName()
   
    StrSql = "SELECT [0_UserSpecificData].UserId, [0_UserSpecificData].PrintPdfLastPathSource, [0_UserSpecificData].PrintPdfLastPathDest, "
    StrSql = StrSql & "[0_UserSpecificData].PrintPdfLastNoFiles, [0_UserSpecificData].PrintPdfLastNoPages FROM 0_UserSpecificData WHERE "
    StrSql = StrSql & "[0_UserSpecificData].UserId="
    StrSql = StrSql & """" & STRUSERID & """"
    StrSql = StrSql & ";"
   
    Set dbs = CurrentDb
   
    Set rst = dbs.OpenRecordset(StrSql)
   
    If Not rst.EOF Then
        If IsNull(rst![PrintPdfLastPathSource]) Then
            SourcePath = "c:\0\"
        Else
            SourcePath = rst![PrintPdfLastPathSource]
        End If
       
        If IsNull(rst![PrintPdfLastPathDest]) Then
            DestPath = "e:\0\"
        Else
            DestPath = rst![PrintPdfLastPathDest]
        End If
       
        If IsNull(rst![PrintPdfLastNoFiles]) Then
            NoFiles = 25
        Else
            NoFiles = rst![PrintPdfLastNoFiles]
        End If
       
        If IsNull(rst![PrintPdfLastNoPages]) Then
            NoPages = 20
        Else
            NoPages = rst![PrintPdfLastNoPages]
        End If
    Else
        SourcePath = "c:\0\"
        DestPath = "e:\0\"
        NoFiles = 25
        NoPages = 20
    End If
   
    DoYouWantToSendToPrinter = MsgBox("Do You Want to Print These Files (Yes) " & vbNewLine & "Or Just Add Footers and Save (No)", vbYesNoCancel)
   
    If DoYouWantToSendToPrinter = 2 Then Exit Sub
   
    PathNmChck = MsgBox("Path with Files to Process" & vbNewLine & SourcePath, vbYesNoCancel)
    If PathNmChck = 6 Then
        PathNm = SourcePath
    ElseIf PathNmChck = 2 Then
        Exit Sub
    Else
   
       Dim strMessage As String
       Dim startDirectory
       strMessage = "Select a directory"
       startDirectory = "My Computer" 'c:\program files"
       Dim objFF As Object
       Set objFF = CreateObject("Shell.Application").BrowseForFolder(0, strMessage, &H1, startDirectory)
       If Not objFF Is Nothing Then
        getdirectory = objFF.Items.Item.Path
           PathNm = objFF.Items.Item.Path
           MsgBox PathNm
       Else
        getdirectory = vbNullString
        MsgBox "No directory selected"
        Exit Sub
       End If
       Set objFF = Nothing
    End If
   
    If Right(PathNm, 1) = "\" Then
    Else
        PathNm = PathNm & "\"
    End If
   
    SourcePath = PathNm
   
    PathNmChck = MsgBox("Souce Path From " & SourcePath & vbneline & vbNewLine & "Destination Path Files Will be Process to" & vbNewLine & DestPath, vbYesNoCancel)
    If PathNmChck = 6 Then
        DestPathNm = DestPath
        PathTest = Dir(DestPath)
On Error Resume Next
        If Len(PathTest) = 0 Then MkDir DestPath
On Error GoTo 0
    ElseIf PathNmChck = 2 Then
        Exit Sub
    Else
   
       strMessage = "Select a directory"
       startDirectory = "My Computer" 'c:\program files"
       Set objFF = CreateObject("Shell.Application").BrowseForFolder(0, strMessage, &H1, startDirectory)
       If Not objFF Is Nothing Then
        getdirectory = objFF.Items.Item.Path
           DestPathNm = objFF.Items.Item.Path
           MsgBox DestPathNm
       Else
        getdirectory = vbNullString
        MsgBox "No directory selected"
        Exit Sub
       End If
       Set objFF = Nothing
    End If
   
    If Right(DestPathNm, 1) = "\" Then
    Else
        DestPathNm = DestPathNm & "\"
    End If
   
    CountFiles = 1
   
    MaxCountFiles = InputBox("How Many Files Do You Want to Print?", , NoFiles)
    MaxPgsPerFile = InputBox("How Many Pages Per File Do You Want to Print?", , NoPages)
    FontSize = InputBox("What Font Size Do You Want for the Footer?" & vbNewLine & vbNewLine & "Default is 8 Point", , 8) '11-11-2011
    FontColor = InputBox("What Font Color Do You Want for the Footer?" & vbNewLine & vbNewLine & "Default is Red", , "Red") '11-11-2011

    FileNm = Dir(PathNm & "*.pdf")
    While FileNm <> "" And CountFiles <= MaxCountFiles
        TestFunction = AddPageNumbers(PathNm, FileNm, DestPathNm, FileNm, 1, MaxPgsPerFile, CountFiles, DoYouWantToSendToPrinter, FontSize, FontColor) '07-25-2011, DoYouWantToInclPathInFtr)
       
        CountFiles = CountFiles + 1
        FileNm = Dir()
    Wend
   
    With rst
        .Edit
            ![PrintPdfLastPathSource] = PathNm
            ![PrintPdfLastPathDest] = DestPathNm
            ![PrintPdfLastNoFiles] = MaxCountFiles
            ![PrintPdfLastNoPages] = MaxPgsPerFile
        .Update
    End With
End Sub

Public Function AddPageNumbers(ByVal SourcePath As String, ByVal SourceFileName As String, ByVal DestPath As String, ByVal DestFileName As String, ByVal FrstPg As Double, ByVal MaxNoPgs As Double, ByVal FileNo As Double, ByVal DoYouWantToSendToPrinter As Integer, ByVal FontSize As Integer, ByVal FontColor As String) As String '07-25-2011, ByVal DoYouWantToInclPathInFtr As Integer) As String

    Dim ex1 As String
    Dim App As Object, AVDoc As Object, AcroPDDoc As Object, AForm As Object
    Dim Ret As Long
    Dim sString As String * 255
    Dim PdfPrint As String, numPages As Integer
       
    Dim sfile As String
    Dim sText As String
    Dim iFileNum As Integer
   
    Dim FileName As String '06-18-2011
   
    Set App = CreateObject("Acroexch.app")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
   
    If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"
    If Right(DestPath, 1) <> "\" Then DestPath = SourcePath & "\"
   
    'FileName = SourcePath & SourceFileName '06-18-2011 - doesn't like symbols in the path either ":" or "\" - need to research
    FileName = SourceFileName '06-18-2011
    booleanresult = AVDoc.Open(SourcePath & SourceFileName, "")
   
    If booleanresult = True Then
        App.Show
        Set AcroPDDoc = AVDoc.GetPDDoc
       
            ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
            ex1 = ex1 & FileName & " -- " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
       
        ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=" & FontSize & "; fp.textColor = " & FontColor & ";  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "

       
'this.addWatermarkFromText({
'cText: "Document content expires 7 days from " + util.printd("mmm dd, yyyy", new Date()),
'nTextAlign: app.constants.align.center,
'nHorizAlign: app.constants.align.center,
'nVertAlign: app.constants.align.center,
'nOpacity: 0.5
'});
''---The above script will display the print date and the expiry date is set for 7 days.
       
        AForm.Fields.ExecuteThisJavaScript ex1
    End If
   
   
    On Error Resume Next
   
    AcroPDDoc.Save 1, DestPath & DestFileName
   
    numPages = AcroPDDoc.GetNumPages()
   
'text file listing documents printed
        sfile = DestPath & "Files Printed " & Format$(Now, "YYMMDD") & ".txt"
        sText = IIf(FileNo < 10, "00", IIf(FileNo < 100, "0", "")) & FileNo & " --- " & "Printed " & MaxNoPgs & " of " & IIf(numPages < 10, "00", IIf(numPages < 100, "0", "")) & numPages & " --- " & SourcePath & SourceFileName & " --- " & DestPath & DestFileName
       
        If FileNo = 1 Then Kill DestPath & "Files Printed " & Format$(Now, "YYMMDD") & ".txt"
       
        iFileNum = FreeFile
        Open sfile For Append As iFileNum
        Write #iFileNum, sText
        Close #iFileNum
   
   
    If MaxNoPgs >= numPages Then
        MaxNoPgs = numPages
    End If
   
    If DoYouWantToSendToPrinter = 6 Then
        PdfPrint = PrintPdfFile(DestPath, DestFileName, FrstPg, MaxNoPgs)
        WaitForPrintingToFinish
    End If
   
    AcroPDDoc.Close
    AVDoc.Close (True)
    App.Exit
   
    Ret = FindWindowWild(0, "Adobe*")
    Call GetWindowText(Ret, sString, 255)
   
    Ret = FindWindow(vbNullString, sString)
   
    PostMessage Ret, WM_CLOSE, CLng(0), CLng(0)

    Set AcroPDDoc = Nothing
    Set AVDoc = Nothing
    Set App = Nothing
End Function


'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo Sub AddPageNumbers02282011revised03012011()
'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo.vbs
'03-01-2011 Superceded by Add Page Numbers     '-----------------------------------------------
'03-01-2011 Superceded by Add Page Numbers     Dim path As String, AcroAVDoc As Object, AcroPDDoc As Object, Path1 As String
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     path = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers     Path1 = "C:\1\test-edited.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Filename = "C:/1/This is a Test.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     '~~> Establish an existing application object
'03-01-2011 Superceded by Add Page Numbers     On Error Resume Next
'03-01-2011 Superceded by Add Page Numbers     Set App = GetObject(, "Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     '~~> If not found then create new instance
'03-01-2011 Superceded by Add Page Numbers     If Err.Number <> 0 Then
'03-01-2011 Superceded by Add Page Numbers         MsgBox Err.Number
'03-01-2011 Superceded by Add Page Numbers         Set App = CreateObject("Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers     Err.Clear
'03-01-2011 Superceded by Add Page Numbers     On Error GoTo 0
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     App.Show
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = CreateObject("AcroExch.AVDoc")
'03-01-2011 Superceded by Add Page Numbers     Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     booleanresult = AVDoc.Open(path, "")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     If booleanresult = True Then
'03-01-2011 Superceded by Add Page Numbers         Set AcroAVDoc = App.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers         Set AcroPDDoc = AcroAVDoc.GetPDDoc
'03-01-2011 Superceded by Add Page Numbers       ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & Filename & " " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=6; fp.textColor = color.red;  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex1
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Save 1, Path1
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Close
'03-01-2011 Superceded by Add Page Numbers     AcroAVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     AVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     App.Exit
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set AcroPDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AcroAVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers Sub AddPageNumbers1()
'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo.vbs
'03-01-2011 Superceded by Add Page Numbers     '-----------------------------------------------
'03-01-2011 Superceded by Add Page Numbers     Dim path As String, AcroAVDoc As Object, AcroPDDoc As Object, Path1 As String
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     path = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers     Path1 = "C:\1\test-edited.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Dim Part1Document As Acrobat.CAcroPDDoc
'03-01-2011 Superceded by Add Page Numbers     Set Part1Document = CreateObject("AcroExch.PDDoc")
'03-01-2011 Superceded by Add Page Numbers     Part1Document.Open (PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers 'note - Java script doesn't like \ so need to work through the path string and replace \ with / before running java script
'03-01-2011 Superceded by Add Page Numbers     Filename = "C:/1/This is a Test.pdf"
'03-01-2011 Superceded by Add Page Numbers     Set App = CreateObject("Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers     App.Show
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = CreateObject("AcroExch.AVDoc")
'03-01-2011 Superceded by Add Page Numbers     Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     booleanresult = AVDoc.Open(path, "")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     If booleanresult = True Then
'03-01-2011 Superceded by Add Page Numbers         Set AcroAVDoc = App.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers '02-25-2011        Set AcroAVDoc = AcroApp.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers         Set AcroPDDoc = AcroAVDoc.GetPDDoc
'03-01-2011 Superceded by Add Page Numbers '02-22-2011    If avdoc.Open(path, "") Then
'03-01-2011 Superceded by Add Page Numbers       '//write JS-Code on a variable'
'03-01-2011 Superceded by Add Page Numbers 'note - Java script doesn't like \ so need to work through the path string and replace \ with / before running java script
'03-01-2011 Superceded by Add Page Numbers       ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & Filename & " " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
'03-01-2011 Superceded by Add Page Numbers '      ex1 = ex1 & FileName
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=6; fp.textColor = color.red;  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers       ex = "  // Set Footer PageNo centered  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  var Box2Width = 50  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  for (var p = 0; p < this.numPages; p++)   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   {   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.textSize=6;  fp.readonly = true;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.alignment=""center"";  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     }  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   }  "
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers 'MsgBox ex & vbNewLine & vbNewLine & ex1
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex1
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers '    AcroPDDoc.Save 1, path
'03-01-2011 Superceded by Add Page Numbers '
'03-01-2011 Superceded by Add Page Numbers '    AcroPDDoc.Close
'03-01-2011 Superceded by Add Page Numbers '
'03-01-2011 Superceded by Add Page Numbers '    Set AcroPDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers '    Set AcroAVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers '    Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers '    Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Save 1, Path1
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Close
'03-01-2011 Superceded by Add Page Numbers     AcroAVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     AVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     App.Exit
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set AcroPDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AcroAVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers Sub AddPageNumbers02232011()
'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo.vbs
'03-01-2011 Superceded by Add Page Numbers     '-----------------------------------------------
'03-01-2011 Superceded by Add Page Numbers     Dim path As String, PDDoc
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     path = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set App = CreateObject("Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers     App.Show
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = CreateObject("AcroExch.AVDoc")
'03-01-2011 Superceded by Add Page Numbers     Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     booleanresult = AVDoc.Open(path, "")
'03-01-2011 Superceded by Add Page Numbers     If booleanresult = True Then
'03-01-2011 Superceded by Add Page Numbers         PDDoc = AVDoc.GetPDDoc
'03-01-2011 Superceded by Add Page Numbers '02-22-2011    If avdoc.Open(path, "") Then
'03-01-2011 Superceded by Add Page Numbers       '//write JS-Code on a variable'
'03-01-2011 Superceded by Add Page Numbers       ex = "  // Set Footer PageNo centered  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  var Box2Width = 50  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  for (var p = 0; p < this.numPages; p++)   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   {   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.textSize=6;  fp.readonly = true;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.alignment=""center"";  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     }  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   }  "
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     PDDoc.Save 1, path
'03-01-2011 Superceded by Add Page Numbers     PDDoc.Close
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set PDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers Sub AddPageNumbersRogerTryingToFigureOut()
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Dim AcroApp As Acrobat.CAcroApp
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Dim Part1Document As Acrobat.CAcroPDDoc
'03-01-2011 Superceded by Add Page Numbers             Dim Part2Document As Acrobat.CAcroPDDoc
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Set AcroApp = CreateObject("AcroExch.App")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Set Part1Document = CreateObject("AcroExch.PDDoc")
'03-01-2011 Superceded by Add Page Numbers             Set Part2Document = CreateObject("AcroExch.PDDoc")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             PdfPathFile = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers             Part1Document.Open (PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers             Part1Document.OpenAVDoc (PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers 'MsgBox "Test - " & AcroApp.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers            Set currdoc = AcroApp.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers '            Set CurrDoc = Part1Document.OpenAVDoc(PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers       ex = "  // Set Footer PageNo centered  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  var Box2Width = 50  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  for (var p = 0; p < this.numPages; p++)   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   {   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.textSize=6;  fp.readonly = true;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.alignment=""center"";  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     }  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   }  "
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers






 
Public Function PrintPdfFile(ByVal PdfPath As String, ByVal PdfFileNm As String, ByVal PdfFirstPageToPrint As Integer, ByVal PdfMaxPagesToPrint As Integer) As String
    Dim PdfPathAndFile As String
    Dim AVDoc As Acrobat.AcroAVDoc
    Dim retcd As Integer
   
    If Right(PdfPath, 1) = "\" Then
        PdfPathAndFile = PdfPath & PdfFileNm
    Else
        PdfPathAndFile = PdfPath & "\" & PdfFileNm
    End If
   
    Set AVDoc = CreateObject("AcroExch.AVDoc")
   
    retcd = AVDoc.Open(PdfPathAndFile, "Title")    ' pathname of pdf file to be opened
   
    If retcd <> -1 Then
'        MsgBox ("Failed to open PDF document " & PdfPathAndFile)
        Exit Function
    End If

    'AVDoc.PrintPages(firstPage, numPages, 2, true, true)
    If PdfMaxPagesToPrint >= 1 Then PdfMaxPagesToPrint = PdfMaxPagesToPrint - 1  'allows for the fact that the first page is page 0
    If PdfFirstPageToPrint >= 1 Then PdfFirstPageToPrint = PdfFirstPageToPrint - 1 'allows for the fact that the first page is page 0
   
    'PrintPagesSilent(nFirstPage As Long, nLastPage As Long, nPSLevel As Long, bBinaryOk As Long, bShrinkToFit As Long) As Boolean
        'Params 1 & 2 are: Print from page __ to page __
        'Param 3: Must be either 1 or 2. 1 generates Level 1 PostScript code. 2 generates Level 2 PostScript code. (You'll have to test in your own environment, but fortunately the list is quite short.)
        'Param 4: True, indicates the PostScript code may contain binary data. False, indicates binary data is encoded into an ASCII format.
        'Param 5: True, shrink (if necessary). False, print actual size; pages may appear clipped.

    retcd = AVDoc.PrintPagesSilent(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, True)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
    '11-23-2011 retcd = AVDoc.PrintPagesSilent(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, 1)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
    'retcd = AVDoc.PrintPages(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, 1)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
    'avDoc.PrintPagesSilent(0, (numPages - 1), PSLevel, BinaryOk, ShrinkToFit);


    AVDoc.Close (1)

End Function
Adobe AcrobatMicrosoft AccessVisual Basic Classic

Avatar of undefined
Last Comment
Nick67

8/22/2022 - Mon
NVIT

Can you reduce this? Do you have an idea of where the trouble might be?
Nick67

Woof!
Heavy evil.
You've certainly come a ways since we figured out how to get your images in the PDF to play nice.
I'd appreciate any help I can get.
Editing PDF's with the javascript monstrosities involved is not for the faint of heart :)
it works fine except the footer only appears on the 1st page.


I'm thinking we must have a looping logic error in there.
We're going to need
'pseudocode
'figure out the number of pages in the document
'for x = 1 to number of pages
'add the footer
next x

So, this must be the operative stuff
   If booleanresult = True Then
         App.Show
         Set AcroPDDoc = AVDoc.GetPDDoc
         
             ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
             ex1 = ex1 & FileName & " -- " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
         
         ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=" & FontSize & "; fp.textColor = " & FontColor & ";  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "

         
 'this.addWatermarkFromText({
 'cText: "Document content expires 7 days from " + util.printd("mmm dd, yyyy", new Date()),
 'nTextAlign: app.constants.align.center,
 'nHorizAlign: app.constants.align.center,
 'nVertAlign: app.constants.align.center,
 'nOpacity: 0.5
 '});
 ''---The above script will display the print date and the expiry date is set for 7 days.
         
         AForm.Fields.ExecuteThisJavaScript ex1
     End If


Is it really that unreadable at the source or did it just paste very badly?
I am seeing the beginnings of the javascript loop
for (var p = 0; p < this.numPages; p++)

This'll be hard because we can't set breakpoints.
Can you clean this chunk of the code up to be more easily human-readable, without breaking it?
Once that is done, we'll try hardcoding for this.numPages on a document you've precalculated the number of pages.

If that works, then we'll know that this.numPages isn't calculating what we think -- it's returning 1 -- and that's why only the first page gets altered
Nick67

This is a little cleaner
    If booleanresult = True Then
         App.Show
         Set AcroPDDoc = AVDoc.GetPDDoc
         
             ex1 = "  // Set Footer PageNo centered  " & vbLf 
             ex1 = ex1 & "  var Box2Width = 500  " & vbLf 
	     ex1 = ex1 & "  for (var p = 0; p < this.numPages; p++)   " & vbLf 
	     ex1 = ex1 & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf 
	     ex1 = ex1 & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf 
	     ex1 = ex1 & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf 
	     ex1 = ex1 & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf 
	     ex1 = ex1 & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf 
	     ex1 = ex1 & "         fp.value = """
             ex1 = ex1 & FileName & " -- " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
         
             ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf
             ex1 = ex1 & "         fp.textSize=" & FontSize & "; fp.textColor = " & FontColor & ";  fp.readonly = true;  " & vbLf 
             ex1 = ex1 & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "

         
 'this.addWatermarkFromText({
 'cText: "Document content expires 7 days from " + util.printd("mmm dd, yyyy", new Date()),
 'nTextAlign: app.constants.align.center,
 'nHorizAlign: app.constants.align.center,
 'nVertAlign: app.constants.align.center,
 'nOpacity: 0.5
 '});
 ''---The above script will display the print date and the expiry date is set for 7 days.
         
         AForm.Fields.ExecuteThisJavaScript ex1
     End If

Open in new window


Try it and ensure I broke nothing.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Nick67

Something else might be wrong, though.
Your footer on the first page, is it correct?
Or is the text actually (say on a 35 page PDF) Page:35/35

That would be a different logic error.
The text being created is right, but only page one gets altered because IT isn't being incremented
Nick67

Look here, near the bottom
https://acrobatusers.com/tutorials/watermarking-a-pdf-with-javascript
This looks immensely less complex that what you have.
In fact, you have something like it commented out below what is actually operating now.

Did it not work how you needed it to?
rogerdjr

ASKER
I tried Nick67's 2014-11-26 at 10:39:39ID: 40467472 suggestion and it still only puts the footer on the first page - see attached "Before" and After Files
J--0-Before-After-Test-11-26-2014.zip
âš¡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rogerdjr

ASKER
I went to the webpage Nick67 Referred me to but I'm not really sure how to incorporate this into the process I agree it looks a lot simpler
Nick67

@rogerdjr
There was no fix in #40467472
I just reformatted the code to make it more human readable.
Thank you for the samples.
Clearly the text of the field is correct for the page that it is on.
That eliminates the conjecture from #40467497

Now, I've been looking at how this is meant to work
The idea is to add a field
var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);
to each page
That isn't happening
Instead, it only adds the field to the first page.
I am now think that the part that names the field String(""xftPage""+p+1) is wonky.
I suspect that creates just "xftPagep1" and not "xftPage1" , "xftPage2" , "xftPage3" ...

Let's try ""xftPage"" + String(p+1) instead.
This bit Page: "" + String(p+1)+ ""/"" + this.numPages; concatenates correctly, so it's worth a shot
Nick67

I am thinking this would work.
Font sizes and colors still might need to be messed with, but

    If booleanresult = True Then
         App.Show
         Set AcroPDDoc = AVDoc.GetPDDoc
	 ex1 = this.addWatermarkFromText({
	 ex1 = ex1 & "cText: " & FileName & " -- " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now) 
	 ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  ,"
	 ex1 = ex1 & "nTextAlign: app.constants.align.center,"
	 ex1 = ex1 & "nHorizAlign: app.constants.align.center,"
	 ex1 = ex1 & "nVertAlign: app.constants.align.center,"
	 ex1 = ex1 & "nOpacity: 0.5"
	 ex1 = ex1 & " '});"

	AForm.Fields.ExecuteThisJavaScript ex1

     End If

Open in new window


Might do it.
Just more javascript to get executing
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
rogerdjr

ASKER
Well I went back to the basic code before I started to "tweak" functions and found that the effort to set the color for the text was hanging things up. I removed "; fp.textColor = " & FontColor & " and it runs fine.

All I need now is a way to figure out how to set the fond color - any Ideas?

Public Function AddPageNumbers(ByVal SourcePath As String, ByVal SourceFileName As String, ByVal DestPath As String, ByVal DestFileName As String, ByVal FrstPg As Double, ByVal MaxNoPgs As Double, ByVal FileNo As Double, ByVal DoYouWantToSendToPrinter As Integer, ByVal FontSize As Integer, ByVal FontColor As String) As String '07-25-2011, ByVal DoYouWantToInclPathInFtr As Integer) As String

    Dim ex1 As String
    Dim App As Object, AVDoc As Object, AcroPDDoc As Object, AForm As Object
    Dim Ret As Long
    Dim sString As String * 255
    Dim PdfPrint As String, numPages As Integer
       
    Dim sfile As String
    Dim sText As String
    Dim iFileNum As Integer
   
    Dim FileName As String '06-18-2011
   
    Set App = CreateObject("Acroexch.app")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
   
    If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"
    If Right(DestPath, 1) <> "\" Then DestPath = SourcePath & "\"
   
    FileName = SourceFileName '06-18-2011
    booleanresult = AVDoc.Open(SourcePath & SourceFileName, "")
   
    If booleanresult = True Then
        App.Show
        Set AcroPDDoc = AVDoc.GetPDDoc
         'Found Code at this web page https://forums.adobe.com/thread/721676?start=0&tstart=0
            ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
            ex1 = ex1 & FileName & " -- " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
            ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=" & FontSize & "; fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "
             
            'removed "; fp.textColor = " & FontColor & "
             
            AForm.Fields.ExecuteThisJavaScript ex1
    End If
   
   
    On Error Resume Next
   
    AcroPDDoc.Save 1, DestPath & DestFileName
   
    numPages = AcroPDDoc.GetNumPages()
   
'text file listing documents printed
        sfile = DestPath & "Files Printed " & Format$(Now, "YYMMDD") & ".txt"
        sText = IIf(FileNo < 10, "00", IIf(FileNo < 100, "0", "")) & FileNo & " --- " & "Printed " & MaxNoPgs & " of " & IIf(numPages < 10, "00", IIf(numPages < 100, "0", "")) & numPages & " --- " & SourcePath & SourceFileName & " --- " & DestPath & DestFileName
       
        If FileNo = 1 Then Kill DestPath & "Files Printed " & Format$(Now, "YYMMDD") & ".txt"
       
        iFileNum = FreeFile
        Open sfile For Append As iFileNum
        Write #iFileNum, sText
        Close #iFileNum
   
    If MaxNoPgs >= numPages Then
        MaxNoPgs = numPages
    End If
    AcroPDDoc.Close
    AVDoc.Close (True)
    App.Exit
   
    Ret = FindWindowWild(0, "Adobe*")
    Call GetWindowText(Ret, sString, 255)
   
    Ret = FindWindow(vbNullString, sString)
   
    PostMessage Ret, WM_CLOSE, CLng(0), CLng(0)

    Set AcroPDDoc = Nothing
    Set AVDoc = Nothing
    Set App = Nothing
End Function
ASKER CERTIFIED SOLUTION
Nick67

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rogerdjr

ASKER
Great help - thanks as always
Nick67

:)
I'd appreciate any help I can get.

You didn't get a PDF/JavaScript expert, but you did get a logical mind to help you work through the swamp.
That tends to happen with questions I ask rather than answer, too.

Later,

Nick67
âš¡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.