Adding page numbers to PDF via (Excel) VBA

New Here ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

Dear community,

although this question was already asked a hundred times, I am stuck with it for two days now and hope somebody of you can help me.

I want to add page numbers to an existing PDF (at the bottom on the right side), beginning with number "1" on the second page of the document.

Through googling I found out that the "common" way to do this is adding, e.g., text fields and then flattening the document so that the text field becomes integrated in the PDF.

Unfortunatelly, I get various problems when I try doing that:

These are the relevant parts of my VBA code so far:

Dim AcroApp as Acrobat.CAcroApp

Dim jso as Object

Dim KurzGesamt as Acrobat.CAcroPDDoc

Dim i As Integer, intSeiten As Integer

Dim objTextfeld As Object

strPfadVerteilungEndlauf = ThisWorkbook.Path & "\Verteilung Endlauf\"

strNameKurzGesamt = "00 Kurzübersicht_komplett_" & Format(Date, "YYYY_MM_DD") & "_" & ThisWorkbook.Name & ".pdf"

Set AcroApp = CreateObject("AcroExch.App")

Set KurzGesamt = CreateObject("AcroExch.PDDoc")

KurzGesamt.Open (strPfadVerteilungEndlauf & strNameKurzGesamt)

Set jso = KurzGesamt.GetJSObject

intSeiten = KurzGesamt.GetNumPages

For i = 2 To intSeiten

    Set objTextfeld = jso.AddField("Textfeld", "text", i, Array(35, 470, 400, 440))

    objTextfeld.Value = Str(i)

    objTextfeld.textSize = 10

    objTextfeld.textFont = "Calibri"

Next i

jso.FlattenPages

Call KurzGesamt.Save(1, strPfadVerteilungEndlauf & strNameKurzGesamt)

Set jso = Nothing

Call AcroApp.CloseAllDocs

Set KurzGesamt = Nothing

Call AcroApp.Exit

Set AcroApp = Nothing

The "set objTextfeld" line leads to the "Object Required" error. Therefore, I first thought the declaration of the jso object does not work.

But when I omit the loop, the text field occurs on the first page of the PDF (with value 0).

When I then set the "i" in the "addfield" command to another page number, the field occurs on this page, but still with value 0.

So how can I

1. create a loop that inserts the page numbers on each page, from the second page on?

2. place the page number at the bottom on the right side? (What I tried concerning the array settings did not really lead to success.)

I also tried to solve the problem with watermarks what did not work either.

By the way, I have Acrobat 7.0 and MS Excel 2010 installed and the reference in Excel is set.

Thank you in advance for any helpful advice!

TOPICS
Acrobat SDK and JavaScript

Views

1.9K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , May 23, 2018 May 23, 2018
You must use i-1 as page number in the addfield command. The first page in the document is 0.

Likes

Translate

Translate
Adobe Community Professional ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

Form fields with the same name has always the same value. You must create fields with unique names.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

Hello Bernd,

I already thought about that and tried "Textfeld(i)" and "Textfeld(str(i))" instead, but that makes no difference. Any ideas?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

Try this:

"Textfeld" & i

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

The loop still doesn't work. But if I select a certain page number in the addfield command now, apparently text fields with all page numbers are inserted on that page. Is that maybe a step in the right direction?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

You must use i-1 as page number in the addfield command. The first page in the document is 0.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

LATEST

That's it! Thank you very much!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines