Copy link to clipboard
Copied
Hello guys,
I have no clue how to add Header and Footer to my PDF-document.
I must be able to understand this:
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:
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
Copy link to clipboard
Copied
I've just posted this question. How can I edit my gramatical mistakes, before somebody replies? 🙂
Copy link to clipboard
Copied
You can edit it. Look at the menu "... More"
Copy link to clipboard
Copied
If tried it, but I cannot open it.
I have limited access with my company's laptop.
Copy link to clipboard
Copied
New members can't edit their own posts unfortunately.
Copy link to clipboard
Copied
OK
Copy link to clipboard
Copied
Hello guys,
luckily, I figured a lot myself. However, ...
Remaining Questions:
Thanks
Code:
Sub MergePDF1()
Dim Aapp As Acrobat.AcroApp
Dim Doc01 As Acrobat.AcroPDDoc
Dim Doc02 As Acrobat.AcroPDDoc
Set Aapp = CreateObject("AcroExch.App")
Set Doc01 = CreateObject("AcroExch.PDDoc")
Set Doc02 = CreateObject("AcroExch.PDDoc")
Aapp.Show
Dim sName00, sName01, sName02, sName03 As String
Dim sPfad00, sPfad01, sPfad02, sPfad03 As String
Dim vAnzahl01, vAnzahl02, vAnzahl03 As Long
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
If Doc01.InsertPages(vAnzahl01 - 1, Doc02, 0, Doc02.GetNumPages(), 0) = False Then
MsgBox "Document " + sName02 + " not inserted into " + sName01
End If
Dim jso01 As Object
Dim jso02 As Object
Dim jso03 As Object
Dim jso04 As Object
Dim jso05 As Object
Dim jso06 As Object
Dim jso07 As Object
Dim objTextfeld01 As Object
Dim objTextfeld02 As Object
Dim objTextfeld03 As Object
Dim objTextfeld04 As Object
Dim objTextfeld05 As Object
Dim objTextfeld06 As Object
Dim objTextfeld07 As Object
Dim gPage As Acrobat.CAcroPDPage
Dim pageSize As Object
Dim iPageWidth As Integer
Dim iPageHeight As Integer
Dim iArray_1 As Integer
Dim iArray_2 As Integer
Dim iArray_3 As Integer
Dim iArray_4 As Integer
Dim iTextBoxHight As Integer
Dim iTextBoxLength As Integer
Dim iTextBoxLength01 As Integer ' TextBox für Attachment A. --> iTextBoxLength01 muss 43 sein. When Calibri 6 ist used
Dim iTextBoxLength02 As Integer ' TextBox für PROTECTED. --> iTextBoxLength01 muss 43 sein. When Calibri 6 ist used
Dim iTextBoxLength03 As Integer ' TextBox für Page 3 of 135. --> iTextBoxLength01 muss 43 sein. When Calibri 6 ist used
Dim iTextBoxLength04 As Integer ' TextBox für "Short Doc ID" --> iTextBoxLength01 muss 66 sein. When Calibri 6 ist used
Dim iTextBoxLength05 As Integer ' TextBox für "Long Doc ID" --> iTextBoxLength01 muss 136 sein. When Calibri 6 ist used
Dim iTextBoxLength06 As Integer ' TextBox für variable Title of the document --> iTextBoxLength01 muss variabel sein. When Calibri 6 ist used
Dim sTextTB01, sTextTB02, sTextTB03, sTextTB04, sTextTB05, sTextTB06 As String
sTextTB01 = "Attachment 1"
sTextTB02 = "PROTECTED"
sTextTB03 = "Page 3 of 354"
sTextTB04 = "Short Doc ID"
sTextTB05 = "Long Doc ID Long Doc ID Long Doc ID"
sTextTB06 = "Veeeeeeeeeeeeerrryy Looooooong Docuuuuuuuuument Naaaaaaaaaaaaame"
'sTextTB06 = "Generator - ID-File"
iTextBoxLength01 = 45
iTextBoxLength02 = 43
iTextBoxLength03 = 43
iTextBoxLength04 = 66
iTextBoxLength05 = 136
iTextBoxLength06 = Len(sTextTB06) * 2
iTextBoxHight = 12
Dim i As Integer
'vAnzahl01 = Doc01.GetNumPages()
For i = 0 To Doc01.GetNumPages() - 1
Set gPage = Doc01.AcquirePage(i)
Set pageSize = gPage.GetSize
'Here I extract the Size of each Page. Need to have as a reference for putting 7 Text-Boxes
iPageWidth = pageSize.x
iPageHeight = pageSize.y
'Up-Left for "Attachment 1"
iTextBoxLength = iTextBoxLength01
iArray_1 = iTextBoxLength
iArray_2 = iPageHeight - iTextBoxHight
iArray_3 = 0
iArray_4 = iPageHeight
Set jso01 = Doc01.GetJSObject
Set objTextfeld01 = jso01.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
'objTextfeld01.Value = "Page " & CStr(i + 1) & " of " + CStr(vAnzahl01)
objTextfeld01.Value = sTextTB01
objTextfeld01.textSize = 6
objTextfeld01.textFont = "calibri"
Set objTextfeld01 = Nothing
Set jso01 = Nothing
'Up-Middle for "PROTECTED"
iTextBoxLength = iTextBoxLength02
iArray_1 = (iPageWidth + iTextBoxLength) / 2
iArray_2 = iPageHeight - iTextBoxHight / 2 ' 13 ist die Höhe der TextBox
iArray_3 = (iPageWidth + iTextBoxLength) / 2 - iTextBoxLength
iArray_4 = iPageHeight
Set jso02 = Doc01.GetJSObject
Set objTextfeld02 = jso02.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
objTextfeld02.Value = sTextTB02
objTextfeld02.textSize = 4
objTextfeld02.textFont = "calibri"
Set objTextfeld02 = Nothing
Set jso02 = Nothing
'Down-Middle for "PROTECTED"
iTextBoxLength = iTextBoxLength02
iArray_1 = (iPageWidth + iTextBoxLength) / 2
iArray_2 = 0
iArray_3 = (iPageWidth + iTextBoxLength) / 2 - iTextBoxLength
iArray_4 = iTextBoxHight
Set jso03 = Doc01.GetJSObject
Set objTextfeld03 = jso03.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
objTextfeld03.Value = sTextTB02
objTextfeld03.textSize = 6
objTextfeld03.textFont = "calibri"
Set objTextfeld03 = Nothing
Set jso03 = Nothing
'Down-Right for "Page i of N"
iTextBoxLength = iTextBoxLength03
iArray_1 = iPageWidth - iTextBoxLength
iArray_2 = 0
iArray_3 = iPageWidth
iArray_4 = iTextBoxHight
Set jso04 = Doc01.GetJSObject
Set objTextfeld04 = jso04.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
objTextfeld04.Value = "Page " & CStr(i + 1) & " of " + CStr(vAnzahl01)
objTextfeld04.textSize = 6
objTextfeld04.textFont = "calibri"
Set objTextfeld04 = Nothing
Set jso04 = Nothing
'Up-Right (but 1 * iTextBoxHight/2 below) for "Short Document ID"
iTextBoxLength = iTextBoxLength04
iArray_1 = iPageWidth - iTextBoxLength
iArray_2 = iPageHeight - iTextBoxHight / 2 - iTextBoxHight / 2
iArray_3 = iPageWidth
iArray_4 = iPageHeight - iTextBoxHight / 2
Set jso05 = Doc01.GetJSObject
Set objTextfeld05 = jso05.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
objTextfeld05.Value = sTextTB04
objTextfeld05.textSize = 6
objTextfeld05.textFont = "calibri"
Set objTextfeld05 = Nothing
Set jso05 = Nothing
'Up-Right for "Long Document ID"
iTextBoxLength = iTextBoxLength05
iArray_1 = iPageWidth - iTextBoxLength
iArray_2 = iPageHeight - iTextBoxHight / 2
iArray_3 = iPageWidth
iArray_4 = iPageHeight
Set jso06 = Doc01.GetJSObject
Set objTextfeld06 = jso06.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
objTextfeld06.Value = sTextTB05
objTextfeld06.textSize = 6
objTextfeld06.textFont = "calibri"
Set objTextfeld06 = Nothing
Set jso06 = Nothing
'Up-Middle for "Very Long Document name"
iTextBoxLength = iTextBoxLength06
iArray_1 = (iPageWidth + iTextBoxLength) / 2
iArray_2 = iPageHeight - iTextBoxHight / 2 - iTextBoxHight / 2
iArray_3 = (iPageWidth + iTextBoxLength) / 2 - iTextBoxLength
iArray_4 = iPageHeight - iTextBoxHight / 2
Set jso07 = Doc01.GetJSObject
Set objTextfeld07 = jso07.AddField("Textfeld" & i, "text", i, Array(iArray_1, iArray_2, iArray_3, iArray_4))
objTextfeld07.Value = sTextTB06
objTextfeld07.textSize = 4
objTextfeld07.textFont = "calibri"
Set objTextfeld07 = Nothing
Set jso07 = Nothing
Set gPage = Nothing
Set pageSize = Nothing
Next i
If Doc01.Save(PDSaveFull, sPfad00) = False Then
MsgBox "Document not saved"
End If
Doc01.Close
Doc02.Close
Aapp.Exit
Set Aapp = Nothing
Set Doc02 = Nothing
Set Doc01 = Nothing
End Sub
Copy link to clipboard
Copied
1. You use the same name for the text fields.
Copy link to clipboard
Copied
See if you can do what you want in the form editor; you may be just trying to push form fields beyond their purpose (i.e. filling forms). As noted, if two form fields have the same name, they are the same field, and have the same contents. Is centering an option in the UI? Is a white background? The UI gives access to everything form fields can do, pretty much, so there's no point looking beyond that. Though a detailed reading of all the field methods and properties can't hurt in case there is an inventive combination available.
Copy link to clipboard
Copied
Thank you, I've just tried it. It worked.
However, the member "Test Screen Name" suggested for me to use so-called "addWaterMarkFromText"
So, I'll have to dig deep into it.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
addWaterMarkFromText?
I don't even know what it is. Hm, So I'll have to start all over again.
Questions:
Thank you anyway.
Copy link to clipboard
Copied
Please let us know if anything in the documentation of addWatermarkFromText is unclear. It seems pretty comprehensive to me, but there is always room for more clarity.
Copy link to clipboard
Copied
This Documentation is not helpful at all: LINK
Question:
Code:
Sub MergePDF_WaterMark()
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")
Set Doc03 = CreateObject("AcroExch.PDDoc")
Aapp.Show
Dim sName00, sName01, sName02, sName03, sName04 As String
Dim sPfad00, sPfad01, sPfad02, sPfad03, sPfad04 As String
Dim vAnzahl01, vAnzahl02, vAnzahl03 As Long
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(Doc01.GetNumPages() - 1, Doc02, 0, Doc02.GetNumPages(), 0) = False Then
MsgBox "Document " + sName02 + " not inserted into " + sName01
End If
Dim jso01 As Object
Set jso01 = Doc01.GetJSObject
jso01.addWatermarkFromText _
cText:="PROTECTED", _
nFontSize:=8, _
nTextAlign:=1, nHorizAlign:=xlCenter, nVertAlign:=4
jso01.addWatermarkFromText _
cText:="UK PROTECT", _
nFontSize:=6, _
nTextAlign:=1, nHorizAlign:=xlCenter, nVertAlign:=3
jso01.addWatermarkFromText _
cText:="10037205513517173", _
nFontSize:=6, _
nTextAlign:=1, nHorizAlign:=2, nVertAlign:=3
jso01.addWatermarkFromText _
cText:="UHC-OOKJ-ÖÖLPÜ-103275 Revision 01", _
nFontSize:=6, _
bPercentage:=True, _
nVertValue:=0.96, _
nTextAlign:=1, nHorizAlign:=2, nVertAlign:=3
jso01.addWatermarkFromText _
cText:="Attachment A", _
nFontSize:=8, _
nTextAlign:=1, nHorizAlign:=0, nVertAlign:=3
Dim i As Integer
vAnzahl01 = Doc01.GetNumPages()
For i = 1 To vAnzahl01
'Unable to integrate
'aColor:=vbGreen, _
'bPercentage:=True, _
'nHorizValue:=0.8, _
'nVertValue:=0.6, _
jso01.addWatermarkFromText _
cText:="Page " + CStr(i) + " of " + CStr(vAnzahl01), _
cFont:="Arial", _
nFontSize:=8, _
nTextAlign:=1, nHorizAlign:=2, nVertAlign:=4, _
nStart:=i - 1, nEnd:=i - 1
Next i
Set jso01 = Nothing
Set jso02 = Nothing
Set jso03 = Nothing
If Doc01.Save(PDSaveFull, sPfad00) = False Then
MsgBox "Document not saved"
End If
Doc01.Close
Doc02.Close
Aapp.Exit
Set Aapp = Nothing
Set Doc03 = Nothing
Set Doc02 = Nothing
Set Doc01 = Nothing
End Sub
Copy link to clipboard
Copied
"This Documentation is not helpful at all:"
Really? How else would you have known the correct syntax?
"Navigation on Page: How can I insert ID-No2 just below"
You have to know the coordinates.
"Navigation on Page: " Ditto
"Watermark vs. Form-Fields: Are addWatermarkFromText supposed to be that slow? "
That is surprising. But it may be just how it is. My comments on form fields, however, come from what the end user may experience. It may be a trade off. Form fields are also just ignored by some PDF viewers, and widely lost when printing; another reason not to use them for adding general text. addWatermarkFromText seems to be the mainstream alternative.
Copy link to clipboard
Copied
"...Really? How else would you have known the correct syntax?..."
-->
I've googled for a similar problem, then I did try-and-error tactic.
Documentation, 0 help.
"...You have to know the coordinates. ..."
--> WOW, amazing reply. fascinating
This is one of the most unempathetic forums, I ever visited.
Copy link to clipboard
Copied
Good luck with the strategy of not using the documentation first and often.
For Acrobat programming, my experience is Google 0, Documentation 1. Well, documentation 0.8, sweat and swearing 0.2.
Good luck also with your strategy of alienating the people who might have chosen to help you. Let us know how that works out.
Copy link to clipboard
Copied
"...Let us know how that works out...."
-->
So far, your input was just slightly above 0 (documentation + coordinates + watermarks).
Google algorithm is more humane in communication than you.
It seems to me that I have to use plyres to pull words out your mouth.