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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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
 
Upvote 0
Solution
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)?
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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
Back
Top