Move text in PDF file - using VBA

Maria Madyarova

New member
Joined
Oct 11, 2018
Messages
2
Reaction score
0
Points
0
Excel Version(s)
Office 265 Pro
I need your help to be able to get the X and Y coordinates of a TextBox (ex. "Figure 10"), check it if they are within a certain range, then I need to move the text box to a specific X and Y coordinates. I am using the following VBA Excel Macro code, that checks if there is a certain text (ex. "Figure 10") on the PDF page. What code I can use to get the Text Coordinates in the PDF file? Thank you.

Few things I need to figure out: 1. How to get the Coordinates X&Y of a specific Text on the PDF Page and change X&Y (move text). 2. How to get the size of each page in PDF

P.S. Working on Engineering report, I am creating Figures that needs to be added to the report. Each Figure is creatied in different application and has Figure Name in the right low corner. When I combine all the PDF Figure pages, figure number is always in a diferent location. I need to write a vba code that would place Figure number in the same place on each PDF page.

I have the folowing VBA code that I have to modify

Code:
Option Explicit

Sub FindTextInPDF()
Application.ScreenUpdating = False
'----------------------------------------------------------------------------------------
'This macro can be used to find a specific TEXT (more than one word) in a PDF document.
'The macro opens the PDF, finds the specified text (the first instance), scrolls so
'that it is visible and highlights it.
'The macro uses the FindText method (see the code below for more info).
'----------------------------------------------------------------------------------------
'Declaring the necessary variables.
Dim TextToFind  As String
Dim CoordX      As Double
Dim PDFPath     As String
Dim DisplayPage As Integer
Dim i           As Integer
'Specify the text you wawnt to search.
'TextToFind = "Christos Samaras"
'Using a range:
TextToFind = ThisWorkbook.Sheets("PDF Report Macro").Range("F10").Value
'Specify the path of the sample PDF form.
'Full path example:
'PDFPath = "C:\Users\Christos\Desktop\How Software Companies Die.pdf"
'Using workbook path:
'PDFPath = ThisWorkbook.Path & "" & "How Software Companies Die.pdf"
'Using a range:
PDFPath = ThisWorkbook.Sheets("PDF Report Macro").Range("C2").Value + "" + ThisWorkbook.Sheets("PDF Report Macro").Range("C3").Value
'Check if the file exists.
If Dir(PDFPath) = "" Then
  MsgBox "Cannot find the PDF file!" & vbCrLf & "Check the PDF path and retry.", vbCritical, "File Path Error"
  Exit Sub
End If
'Check if the input file is a PDF file.
If LCase(Right(PDFPath, 3)) <> "pdf" Then
  MsgBox "The input file is not a PDF file!", vbCritical, "File Type Error"
  Exit Sub
End If
On Error Resume Next
'Initialize Acrobat by creating the App object
Dim App As CAcroApp
Dim AVDoc As CAcroAVDoc
Dim PDDoc As CAcroPDDoc
Set App = CreateObject("AcroExch.App")
'Check if the object was created. In case of error release the object and exit.
If Err.Number <> 0 Then
  MsgBox "Could not create the Adobe Application object!", vbCritical, "Object Error"
  Set App = Nothing
  Exit Sub
End If
'Create the AVDoc object.
Set AVDoc = CreateObject("AcroExch.AVDoc")
'Create the AcroPDDoc object
Set AcroPDDoc = AVDoc.GetPDDoc
'Check if the object was created. In case of error release the objects and exit.
If Err.Number <> 0 Then
  MsgBox "Could not create the AVDoc object!", vbCritical, "Object Error"
  Set AVDoc = Nothing
  Set App = Nothing
  Exit Sub
End If
On Error GoTo 0
'Open the PDF file.
If AVDoc.Open(PDFPath, "") = True Then
  'Open successful, bring the PDF document to the front.
  AVDoc.BringToFront
  Set AVDoc = App.GetActiveDoc
  Set PDDoc = AVDoc.GetPDDoc
  'Get the number of pages in open PDF file
  Dim numOfPage      As Integer
  numOfPage = PDDoc.GetNumPages
  'Maximize the document
  Call AVDoc.Maximize(True)
  'Loop through the pages
  For i = 2 To numOfPage
    'Go to the desired page
    'The first page is 0
    Set PDFPageView = AVDoc.GetAVPageView()
    DisplayPage = i
    Call PDFPageView.GoTo(DisplayPage - 1)
    'Use the FindText method in order to find and highlight the desired text.
    'The FindText method returns true if the text was found or false if it was not.
    'Here are the 4 arguments of the FindText methd:
    'Text to find:          The text that is to be found (in this example the TextToFind variable).
    'Case sensitive:        If true, the search is case-sensitive. If false, it is case-insensitive (in this example is True).
    'Whole words only:      If true, the search matches only whole words. If false, it matches partial words (in this example is True).
    'Search from 1st page:  If true, the search begins on the first page of the document. If false, it begins on the current page (in this example is False).
    If AVDoc.FindText(TextToFind, True, True, False) = False Then
      'Text was not found, close the PDF file without saving the changes.
      AVDoc.Close True
      'Close the Acrobat application.
      App.Exit
      'Release the objects.
      Set AVDoc = Nothing
      Set App = Nothing
      'Inform the user.
      MsgBox "The text '" & TextToFind & "' could not be found in the PDF file!", vbInformation, "Search Error"
    Else
      'select text
      'find the coordinates of the Specofoed Text Box
      'move the specific text to the specified X and Y coordinate
      'b = AVDoc.getField(TextToFind)
      'Var aRect = AVDoc.rect   ' Make a copy of b.rect
      'aRect [0] = 22
    
      'Dim rect(3) As Integer
      CoordX = 0
    End If
  Next
Else
  'Unable to open the PDF file, close the Acrobat application.
  App.Exit
  'Release the objects.
  Set AVDoc = Nothing
  Set App = Nothing
  'Inform the user.
  MsgBox "Could not open the PDF file!", vbCritical, "File error"
End If
End Sub
 
Last edited by a moderator:
Users need Acrobat to help so few can likely do so. Try attaching simple Excel and PDF files to better help us help you.

Different Applications handle Figure labeling differently. Sounds like yours is adding it as plain text with right flush tab for alignment.

I suspect that you probably need to iterate textbox objects at some point and check their contents. You would then have the object and can get/set its properties.
 
Back
Top