Replace Text in PDF using VBA

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Ashu2019
User
Posts: 8
Joined: Mon Feb 10, 2020 10:14 am

Replace Text in PDF using VBA

Post by Ashu2019 »

Dear Team,

can some one help me with how to replace text and save, I want to open PDF search for some text example Apple and replace it with Banana save the pdf and exit

Here's my current code, Any help or direction will be appreciated.

Code: Select all


Public Sub CreateDoc()
    Dim PXV As PDFXEdit.PXV_Inst
    Dim pxc As New PDFXEdit.PXC_Inst
    Dim Doc As IPXC_Document
    Dim objdcb As IPXC_DocAuthCallback
    Dim objContent As IPXC_Content
    Dim retv() As Byte
    Dim ar1(20) As String
    Dim retv2 As IPXS_PDFVariant
    Dim ts As IPXC_TState
    Dim pf As PXC_PointF
    
    
Set PXV = New PDFXEdit.PXV_Inst
PXV.Init
Set pxc = PXV.GetExtension("PXC")
pxc.Init ""

Set Doc = pxc.OpenDocumentFromFile("Path\FP101A - Copy.pdf", Nothing)

Set objContent = Doc.Pages.Item(0).GetContent(CAccessMode_FullClone)

For i = 1 To objContent.Items.Count - 1

    If objContent.Items.Item(i).Type = CIT_Text Then

        Dim s() As String

        Call objContent.Items.Item(i).Text_GetDataSA(retv())

        Set retv2 = objContent.Items.Item(i).Text_GetData2()
        
        For ir = LBound(retv) To UBound(retv)

            sText = sText & Chr(retv(ir))
            
            If sText = "SamplText" Then
                
                MsgBox "Found: " & sText
                
                'Replace "d" of "Approved" with the letter "N"
                retv(3) = 65
                Call retv2.Arr_SetInt(ir, 78)
                Set ts = objContent.Items.Item(i).GetTState
               'Set res = objContent.Items.Item(i).Text_SetData(retv2, ts) '<--- this line gives error Function not available in VB
                
            End If

        Next

        Debug.Print sText

        sText = vbNullString

    End If

Next


Doc.WriteToFile "path\macroedited.pdf"
Doc.Close

pxc.Finalize
   
End Sub

Ashu2019
User
Posts: 8
Joined: Mon Feb 10, 2020 10:14 am

Re: Replace Text in PDF using VBA

Post by Ashu2019 »

Support?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: Replace Text in PDF using VBA

Post by Tracker Supp-Stefan »

Hello Ashu2019,

Find and replace is not yet possible in our products.

Kind regards,
Stefan
Ashu2019
User
Posts: 8
Joined: Mon Feb 10, 2020 10:14 am

Re: Replace Text in PDF using VBA

Post by Ashu2019 »

Thanks Stefan,

All i want is to edit the text and save pdf back is it possible i have the content item I am looking for I need to edit that content item text and save the pdf that's it.

Regards,
Ashish.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: Replace Text in PDF using VBA

Post by Tracker Supp-Stefan »

Hello Ashu2019,

Here are all the methods for an IPXC_ContentItem:
https://sdkhelp.pdf-xchange.com/view/PX ... ontentItem

I am not a developer myself - so I do not know if the methods from the above page would allow you to make the needed changes to that Content Item.

Kind regards,
Stefan
Ashu2019
User
Posts: 8
Joined: Mon Feb 10, 2020 10:14 am

Re: Replace Text in PDF using VBA

Post by Ashu2019 »

@Vasyl-Tracker Dev Team

any help please :(
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8624
Joined: Wed Jan 03, 2018 6:52 pm

Re: Replace Text in PDF using VBA

Post by TrackerSupp-Daniel »

Hello, Ashu2019

Just to clarify, are you using one of our SDK products for this task, or are you just trying to apply VBA actions to the end-user Editor?

If the former (using an SDK product), there may be a way you could develop something on your own to perform this task, but it would not be something our Dev team can build for you. If the latter, and you are using the end user editor, this is very likely not possible at this time, considering we do not yet offer our own Find and Replace function either.

We are looking to add Find and replace in the future, but for the time being, we do not have a solution in house to offer for replacement of base content text.

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com