Adobe PDF add page numbers

twelin

New Member
Joined
May 10, 2019
Messages
15
Hi,
I am using this macro to merge two pdf:s. Each pdf have page numbers, and the merged file keeps the original page numbers.
I want to make a new numbering for the whole new (merged) pdf.
Any clues?

Sub MergePdf()
'inspired by VBA A2Z youtube:

Dim Aapp As Acrobat.AcroApp
Dim PDF As Acrobat.AcroPDDoc
Dim SourcePDF As Acrobat.AcroPDDoc

Dim WB As Workbook
Dim WS As Worksheet
Dim Folder As String
Dim FileCompanySv, FileCompanyEn, FileFundEn, FileFundSv As String
Dim Path As String
Dim i As Integer
Dim PDF_pages, SourcePDF_pages As Long

Set WB = Workbooks("All.xlsm")
Set WS = Sheets("FileNamesSe")
Set Aapp = CreateObject("AcroExch.App")
Set PDF = CreateObject("AcroExch.PDDoc")
Set SourcePDF = CreateObject("AcroExch.PDDoc")

WB.Activate
i = 3

Path = WS.Cells(1, 2).Value
FileFundSv = WS.Cells(i, 1).Value
FileCompanySv = WS.Cells(1, 3).Value
Aapp.Show

PDF.Open (Path & FileFundSv)
PDF_pages = PDF.GetNumPages() - 1
SourcePDF.Open (Path & FileCompanySv)
SourcePDF_pages = SourcePDF.GetNumPages()

If PDF.InsertPages(PDF_pages, SourcePDF, 0, SourcePDF.GetNumPages(), True) = False Then
Debug.Print "Failed to insert the page"
End If

If PDF.Save(PDSaveFull, "C:\Users\towe03\Test\KOOOOLLLA.pdf") = False Then
Debug.Print "Failed to save"
Else
Debug.Print "Doc Saved"
End If

PDF.Close
SourcePDF.Close

Set Aapp = Nothing
Set PDF = Nothing
Set SourcePDF = Nothing

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,752
Using the Acrobat API you can add a text field to each page containing the page number which overlays the original page number.

This macro adds a text field at the bottom centre of each page. The field has a yellow background fill so you can see its position. There is commented-out code which sets a white background once you've got the text field in the correct position - experiment with the Const values for the field's width, height and position from the bottom of the page.

VBA Code:
Option Explicit

Public Sub Add_Page_Number_Fields()

    Dim PDFinputFile As String, PDFoutputFile As String
    Dim AcrobatApp As Acrobat.AcroApp
    Dim AcroAVDocInput As Acrobat.AcroAVDoc
    Dim AcroPDDocInput As Acrobat.AcroPDDoc
    Dim jso As Object
    Dim pageField As Object
    Dim page As Long
    Dim pageRect As Variant
    Dim fieldRect(0 To 3) As Double
    Dim pageWidth As Double, fieldLeft As Double
       
    Const FIELD_WIDTH = 50
    Const FIELD_HEIGHT = 15
    Const FIELD_BOTTOM = 15
   
    PDFinputFile = "C:\path\to\PDF document.pdf"
    PDFoutputFile = "C:\path\to\PDF document WITH PAGE NUMBER FIELDS.pdf"
   
    Set AcrobatApp = New Acrobat.AcroApp        'CreateObject("AcroExch.App")
    Set AcroAVDocInput = New Acrobat.AcroAVDoc  'CreateObject("AcroExch.AVDoc")
   
    If AcroAVDocInput.Open(PDFinputFile, "") Then
   
        Set AcroPDDocInput = AcroAVDocInput.GetPDDoc()
   
        Set jso = AcroPDDocInput.GetJSObject
       
        For page = 0 To AcroPDDocInput.GetNumPages() - 1
            pageRect = jso.getPageBox("Crop", page)
            pageWidth = pageRect(2) - pageRect(0)
            fieldLeft = pageWidth / 2 - FIELD_WIDTH / 2
            fieldRect(0) = fieldLeft
            fieldRect(1) = FIELD_BOTTOM + FIELD_HEIGHT
            fieldRect(2) = fieldLeft + FIELD_WIDTH
            fieldRect(3) = FIELD_BOTTOM
            Set pageField = jso.addField("page" & page + 1, "text", page, fieldRect)
            pageField.TextSize = 8
            pageField.FillColor = jso.Color.yellow
            'pageField.FillColor = jso.Color.white
            pageField.TextFont = "Helvetica"
            pageField.Alignment = "center"
            pageField.Value = CStr(page + 1)
            pageField.ReadOnly = True
        Next
       
        'Save as output PDF
        AcroPDDocInput.Save 1, PDFoutputFile
        AcroAVDocInput.Close True
       
        If AcroAVDocInput.Open(PDFoutputFile, "") Then
            AcrobatApp.Show
            AppActivate Mid(PDFoutputFile, InStrRev(PDFoutputFile, "\") + 1), False
        End If
   
    Else
   
        MsgBox "Unable to open PDF input file " & vbCrLf & PDFinputFile
       
    End If

    Set AcroPDDocInput = Nothing
    Set AcroAVDocInput = Nothing
    Set AcrobatApp = Nothing

End Sub
 
Solution

twelin

New Member
Joined
May 10, 2019
Messages
15
Using the Acrobat API you can add a text field to each page containing the page number which overlays the original page number.

This macro adds a text field at the bottom centre of each page. The field has a yellow background fill so you can see its position. There is commented-out code which sets a white background once you've got the text field in the correct position - experiment with the Const values for the field's width, height and position from the bottom of the page.

VBA Code:
Option Explicit

Public Sub Add_Page_Number_Fields()

    Dim PDFinputFile As String, PDFoutputFile As String
    Dim AcrobatApp As Acrobat.AcroApp
    Dim AcroAVDocInput As Acrobat.AcroAVDoc
    Dim AcroPDDocInput As Acrobat.AcroPDDoc
    Dim jso As Object
    Dim pageField As Object
    Dim page As Long
    Dim pageRect As Variant
    Dim fieldRect(0 To 3) As Double
    Dim pageWidth As Double, fieldLeft As Double
      
    Const FIELD_WIDTH = 50
    Const FIELD_HEIGHT = 15
    Const FIELD_BOTTOM = 15
  
    PDFinputFile = "C:\path\to\PDF document.pdf"
    PDFoutputFile = "C:\path\to\PDF document WITH PAGE NUMBER FIELDS.pdf"
  
    Set AcrobatApp = New Acrobat.AcroApp        'CreateObject("AcroExch.App")
    Set AcroAVDocInput = New Acrobat.AcroAVDoc  'CreateObject("AcroExch.AVDoc")
  
    If AcroAVDocInput.Open(PDFinputFile, "") Then
  
        Set AcroPDDocInput = AcroAVDocInput.GetPDDoc()
  
        Set jso = AcroPDDocInput.GetJSObject
      
        For page = 0 To AcroPDDocInput.GetNumPages() - 1
            pageRect = jso.getPageBox("Crop", page)
            pageWidth = pageRect(2) - pageRect(0)
            fieldLeft = pageWidth / 2 - FIELD_WIDTH / 2
            fieldRect(0) = fieldLeft
            fieldRect(1) = FIELD_BOTTOM + FIELD_HEIGHT
            fieldRect(2) = fieldLeft + FIELD_WIDTH
            fieldRect(3) = FIELD_BOTTOM
            Set pageField = jso.addField("page" & page + 1, "text", page, fieldRect)
            pageField.TextSize = 8
            pageField.FillColor = jso.Color.yellow
            'pageField.FillColor = jso.Color.white
            pageField.TextFont = "Helvetica"
            pageField.Alignment = "center"
            pageField.Value = CStr(page + 1)
            pageField.ReadOnly = True
        Next
      
        'Save as output PDF
        AcroPDDocInput.Save 1, PDFoutputFile
        AcroAVDocInput.Close True
      
        If AcroAVDocInput.Open(PDFoutputFile, "") Then
            AcrobatApp.Show
            AppActivate Mid(PDFoutputFile, InStrRev(PDFoutputFile, "\") + 1), False
        End If
  
    Else
  
        MsgBox "Unable to open PDF input file " & vbCrLf & PDFinputFile
      
    End If

    Set AcroPDDocInput = Nothing
    Set AcroAVDocInput = Nothing
    Set AcrobatApp = Nothing

End Sub
John, I can't figure out how to get the page number on the right side of the page (as apposed to mid side)?
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,752
fieldLeft = pageWidth / 2 - FIELD_WIDTH / 2
Calculates the left side of the text field with respect to the width of the page and the width of the field, so that the field is centred. Therefore play around with the above calculation.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,680
Messages
5,637,745
Members
416,982
Latest member
lisam77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top