Adding a Header to PDF File with vba

I use this string to add footers to a PDF documents and would like to adapt it to add headers - I revised Set Footer to Set Header

  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 =; fp.readonly = true;  " & vbLf & "
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
VBA has no facility to work with PDF documents, so I assume you're using VBA in conjunction with a 3rd party library (like Acrobat, or something of that sort).

If you could tell us the 3rd party utility you're using, perhaps we could give better direction.
Jeffrey CoachmanMIS LiasonCommented:
Just curious...
What the reason you cannot add the header/footer in Access?

Things like this are great for one shot deals or "snapshots".
But they need to be done for each new report as well.
rogerdjrAuthor Commented:
The reason I cannot add the header in access is that I receive the documents as pdf files - give them a new filename and transmit them to another team member.

The header or footer that I add is used to identify the version of the document.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Jeffrey CoachmanMIS LiasonCommented:
So that code successfully adds Footers?
If so, then try "hacking" it to move the info "Up"

For example, ...first change :
Set Footer PageNo centered
Set Header PageNo centered

...Not sure what the rest of your variables represent ...but I would first target changing the values here:
 [bStart,30,bEnd,15], see if it "moves" the output.

In other words, ...if this code works for Footers, then you should be able to modify it to "move up" to the header.
So you will have to play around with the values (possibly in your variables as well) until you see the output "Move up"

Finally, try contacting the author of the Footer code, ..and see if they have any modifications so that it will work in the header.

rogerdjrAuthor Commented:
Will try this later this week - work is very busy the last week or so

Sorry for the delay
Jeffrey CoachmanMIS LiasonCommented:
rogerdjrAuthor Commented:
Did some experimentation changing the term header / footer does not have any affect

This term adjusts the vertical location  [bStart,30,bEnd,15] measured from the bottom of the page.

I could not figure out how to calculate page height which would be required to get a value for start and end, locating it at the top of a page (not all pages in all pdfs are 8.5 x 11 portrait)

Continuing to search for options
Jeffrey CoachmanMIS LiasonCommented:
Then perhaps the best you can shoot for is getting this going just for 8.5 x 11 portrait...

Keep us posted
rogerdjrAuthor Commented:
Got to be a way to evaluate a page and read its height for the calculation - I'll continue the search
Jeffrey CoachmanMIS LiasonCommented:
perhaps you could see if it is an option for the person/program, creating the PDF to generate the headers/footers...?
Thus avoiding the whole issue...?
rogerdjrAuthor Commented:
Found a solution at this website:

Watermarking a PDF with JavaScript and Acrobat X -

Code I used to add a Watermark header is:

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, Optional RotatePageDegrees As Integer) ', ByVal HeaderFooter As String) ', ByVal SupressNoPrintMsg As Boolean) 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, numPages1 As Integer, PageNoVar As Integer
    Dim sfile As String
    Dim sText As String
    Dim iFilenum As Integer
    Dim fileName As String '06-18-2011
    Set App = CreateObject("")
    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
        Set AcroPDDoc = AVDoc.GetPDDoc
            If Not IsNull(RotatePageDegrees) Then
             numPages = AcroPDDoc.GetNumPages()
                For PageNoVar = 0 To numPages - 1
                    Dim PDFPage As AcroPDPage
                    Set PDFPage = AcroPDDoc.AcquirePage(PageNoVar)
                    Call PDFPage.SetRotate(0)
                    Call PDFPage.SetRotate(RotatePageDegrees)
            End If

    ex1 = "this.addWatermarkFromText({" & vbLf
    ex1 = ex1 & "cText: " & """" & "DRAFT\n\nCOPY" & """" & "," & vbLf
    ex1 = ex1 & "," & vbLf
    ex1 = ex1 & "," & vbLf
    ex1 = ex1 & "cFont: " & """" & "Helvetica-Bold" & """" & "," & vbLf
    ex1 = ex1 & "nFontSize:36," & vbLf
    ex1 = ex1 & "aColor:," & vbLf
'    ex1 = ex1 & "nStart: this.pageNum," & vbLf
    ex1 = ex1 & "nOpacity: 0.5" & vbLf
    ex1 = ex1 & "});"
        AForm.Fields.ExecuteThisJavaScript ex1
    End If
    On Error Resume Next
    AcroPDDoc.Save 1, DestPath & DestFileName
    numPages = AcroPDDoc.GetNumPages()
        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
On Error GoTo 0
    AVDoc.Close (True)
    Set AcroPDDoc = Nothing
    Set AVDoc = Nothing
    Set App = Nothing

    If DoYouWantToSendToPrinter = 6 Then
        appPDF = """" & "C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe" & """"
        RetVal = Shell(appPDF & " /t /h /s /o " & """" & DestPath & DestFileName & """" & " Adobe PDF", 0)
    End If

End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rogerdjrAuthor Commented:
No workable solutions added - by searching the Internet I found a solution that worked
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.