Skip to main content
Known Participant
April 9, 2021
Question

Add Header and Footer to each Page of the PDF-Document via VBA

  • April 9, 2021
  • 2 replies
  • 5551 views

Hello guys,

 

I have no clue how to add Header and Footer to my PDF-document.

 

I must be able to understand this:

  1. Merge more PDF-documents.
  2. Determine which Format each Page has [A4, A3, A2, A1, or none of these]
  3. Write "Page x of N" to every single page of that document in the footer (down in the Right Corner).
  4. Write the Document ID in the header (up in the Right Corner).
  5. Write the Document Name in the (middle of the) header.
  6. Write other document-related entries in the (middle of the) footer, such as "DRAFT", "CONFIDENTIAL" etc.
  7. None of the entries are allowed to overlap each other

 

I was trying to find the solution by searching the internet. I am not looking for entire solution. Moreover, I would like to understand the principles of the tools, to be able to control them.

 

jso.AddFiled

For the start, I would like to understand jso.AddFiled("Textfeld" & i, "text", i Array(0, 0, 50, 13)).

I was playing with the parameters of the Array(x1,x2,x3,x4). But I could not discore their meaning. Only thing, I discovered is, x2 and x4 determine the height of the Text-Box, which is inserted on every footer. At the beginning, I thought, they stand for the coordinates, which are related to DIN A4. But, the more I play with them, the less I understand them.

 

Question:

  • Can you please guide me?

 

Sub MergePDF()

    Dim Aapp As Acrobat.AcroApp
    Dim Doc01 As Acrobat.AcroPDDoc
    Dim Doc02 As Acrobat.AcroPDDoc
    Dim Doc03 As Acrobat.AcroPDDoc
    Set Aapp = CreateObject("AcroExch.App")
    Set Doc01 = CreateObject("AcroExch.PDDoc")
    Set Doc02 = CreateObject("AcroExch.PDDoc")
    Dim sName00, sName01, sName02, sName03, sName04 As String
    Dim sPfad00, sPfad01, sPfad02, sPfad03, sPfad04 As String
    Dim vAnzahl01, vAnzahl02, vAnzahl03 As Long
    
    Aapp.Show
    sPfad00 = ThisWorkbook.Path + "\DocTOTAL.pdf"
    sPfad01 = ThisWorkbook.Path + "\Doc_01.pdf"
    sPfad02 = ThisWorkbook.Path + "\Doc_02.pdf"
    
    If Doc01.Open(sPfad01) Then
        sName01 = Doc01.GetFileName()
        vAnzahl01 = Doc01.GetNumPages()
    End If
    If Doc02.Open(sPfad02) Then
        sName02 = Doc02.GetFileName()
        vAnzahl02 = Doc02.GetNumPages()
    End If
    vAnzahl02 = Doc02.GetNumPages()
    If Doc01.InsertPages(vAnzahl01 - 1, Doc02, 0, vAnzahl02, 0) = False Then
        MsgBox "Document " + sName02 + " not inserted into " + sName01
    End If
    vAnzahl01 = Doc01.GetNumPages()
    
    Dim jso As Object
    Dim objTextfeld01 As Object
    Dim objTextfeld02 As Object
    Set jso = Doc01.GetJSObject
    Dim i As Integer
    For i = 0 To vAnzahl01 - 1
        Set objTextfeld01 = jso.AddField("Textfeld" & i, "text", i, Array(0, 0, 50, 13))
        objTextfeld01.Value = "Page " & CStr(i + 1) & " of " + CStr(vAnzahl01)
        objTextfeld01.textSize = 6
        objTextfeld01.textFont = "calibri"
        
        'Set objTextfeld02 = jso.AddField("Textfeld" & i, "text", i, Array(800, 15, 100, 0))
        'objTextfeld02.Value = "UK PROTECT"
        'objTextfeld02.textSize = 12
        'objTextfeld02.textFont = "calibri"
    Next i
    
    If Doc01.Save(PDSaveFull, sPfad00) = False Then
            MsgBox "Document not saved"
    End If

    Set jso = Nothing
    Doc01.Close
    Doc02.Close
    Aapp.Exit
    Set Aapp = Nothing
    Set Doc02 = Nothing
    Set Doc01 = Nothing
End Sub

 

This topic has been closed for replies.

2 replies

Legend
April 10, 2021

The information you need is in the Acrobat SDK, which has thousands of pages of detailed documentation. You cannot (easily or well) learn to program Acrobat by web searches. 

Legend
April 11, 2021

By the way, have you considered the use of addWaterMarkFromText? There is a significant overhead to working with form fields, and you look as if you may be generating thousands. If you MUST generate thousands, at least use heirarchical names (a.b.c etc.) to reduce the name table lookup time.

SnailMTUAuthor
Known Participant
April 11, 2021

addWaterMarkFromText?

I don't even know what it is. Hm, So I'll have to start all over again. 

 

Questions:

  1. Do you have any example of most important ingredients how "addWaterMarkFromText" is used?
  2. Do you have any short example how 2 Form-Fields are inserted on the same page with different content?

 

Thank you anyway.

SnailMTUAuthor
Known Participant
April 9, 2021

I've just posted this question. How can I edit my gramatical mistakes, before somebody replies? 🙂

Bernd Alheit
Community Expert
Community Expert
April 9, 2021

You can edit it. Look at the menu "... More"

SnailMTUAuthor
Known Participant
April 9, 2021

If tried it, but I cannot open it.

I have limited access with my company's laptop.