使用VBA在PDF板上移动文本框

时间:2018-10-10 23:33:01

标签: excel vba excel-vba pdf textbox

我需要您的帮助才能获取TextBox的X和Y坐标(例如“图10”),检查它们是否在特定范围内,然后我需要将文本框移至特定位置X和Y坐标。我正在使用以下VBA Excel宏代码,该代码检查PDF页面上是否存在某些文本(例如“图10”)。我可以使用什么代码来处理TextBox坐标?谢谢。

我需要弄清楚的几件事: 1.如何获取组合PDF中的页数。我需要通过PDF页面创建循环。 2.如何获取PDF页面上特定文本的坐标X&Y和更改X&Y(移动文本)。 3.如何获取PDF中每页的大小

P.S。 1.我能够成功获取打开的PDF文件中的页数,请参见下面的代码。我必须添加几条VBA代码行(注意,我有Office 365)。

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).

    'Note that in some cases it doesn't work (doesn't highlight the text), so in those
    'cases prefer the SearchTextInPDF macro, if you have only ONE WORD to find!

    'The code uses late binding, so no reference to external library is required.
    'However, the code works ONLY with Adobe Professional, so don't try to use it with
    'Adobe Reader because you will get an "ActiveX component can't create object" error.

    'Written by:    Christos Samaras
    'Date:          04/05/2014
    'e-mail:        xristos.samaras@gmail.com
    'site:          http://www.myengineeringworld.net
    '----------------------------------------------------------------------------------------

    'Declaring the necessary variables.
    Dim TextToFind  As String
    Dim CoordX      As Double
    Dim PDFPath     As String

    '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")

    '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

        '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
            Dim rect(3) As Integer
            Dim numPages As Integer
            numPages = AVDoc.GetNumPages()
            CoordX = 0
        End If

    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

0 个答案:

没有答案