Add Header and Footer to each Page of the PDF-Document via VBA
Hello guys,
I have no clue how to add Header and Footer to my PDF-document.
I must be able to understand this:
- Merge more PDF-documents.
- Determine which Format each Page has [A4, A3, A2, A1, or none of these]
- Write "Page x of N" to every single page of that document in the footer (down in the Right Corner).
- Write the Document ID in the header (up in the Right Corner).
- Write the Document Name in the (middle of the) header.
- Write other document-related entries in the (middle of the) footer, such as "DRAFT", "CONFIDENTIAL" etc.
- 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
