Replace Text in PDF using VBA

PDF-XChange Editor SDK for Developers

Moderators: Daniel - PDF-XChange, PDF-XChange Support, Paul - PDF-XChange, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - Tracker, Ivan - Tracker Software, Stefan - PDF-XChange

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.
Post Reply
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
Stefan - PDF-XChange
Site Admin
Posts: 19255
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Replace Text in PDF using VBA

Post by Stefan - PDF-XChange »

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
Stefan - PDF-XChange
Site Admin
Posts: 19255
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Replace Text in PDF using VBA

Post by Stefan - PDF-XChange »

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
Daniel - PDF-XChange
Site Admin
Posts: 10131
Joined: Wed Jan 03, 2018 6:52 pm

Re: Replace Text in PDF using VBA

Post by Daniel - PDF-XChange »

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
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
KunjanSoni
User
Posts: 2
Joined: Fri Dec 13, 2024 9:39 am

Re: Replace Text in PDF using VBA

Post by KunjanSoni »

Hi All,

I am trying to open a PDF using VBA and want to search if few text are available in opened PDF? If yes then, PDF should visible and searched page shall be visible on PDF Xchange pro. I am able to open PDF using following code but I am not aware how to search a text in PDF using VBA and make the instance visible.

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


Set PXV = New PDFXEdit.PXV_Inst
PXV.Init
Set pxc = PXV.GetExtension("PXC")
pxc.Init ""

Set Doc = pxc.OpenDocumentFromFile("Path\PrintedPDF.pdf", Nothing)


pxc.Finalize

End Sub
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19255
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Replace Text in PDF using VBA

Post by Stefan - PDF-XChange »

Hello KunjanSoni,

Please take a look at the sample here:
https://sdkhelp.pdf-xchange.com/view/PXV:op_search

It does show exactly how to use the search operation and then focus on a highlighted and found result.

Kind regards,
Stefan
KunjanSoni
User
Posts: 2
Joined: Fri Dec 13, 2024 9:39 am

Re: Replace Text in PDF using VBA

Post by KunjanSoni »

Dear Stefan,

Thank you for your reply.

I converted C# code to VBA and tired to use it in Excel VBA but, I am facing following problems.

1. Excel VBA is not allowing me to define a parameter named as “Input”. Please refer to attached snap where , respective lines are highlighted is red. Can I use it “input1” instead of input? However, “Input” works well in visual studio VB code.

2. pdfCtl is not defined anywhere. So, I am using following commands to define it. Please correct me in case of any misunderstanding.
Dim pdfCtl As PDFXEdit.PXV_Control
Set pdfCtl = CreateObject("PDFXEdit.PXV_Control")

3. I am not able to assign PDF file path to Doc variable defined as PDFXEdit.IPXV_Document. Please guide, how to assign a PDF document path or document to Doc variable.

Please note that, I am using PDF Xchange pro editor software. I have registered PDFXEditCore.x64.dll using regsvr32 successfully. Excel version is 64 bit.

Thanks and regards
Kunjan


image.png
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19255
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Replace Text in PDF using VBA

Post by Stefan - PDF-XChange »

Hello KunjanSoni,

I've asked colleagues in our dev team to take a look at this topic, however it might be that what you are trying to do is not in the capabilities of VBA. They would have more details for you when they have a chance to post.

Kind regards,
Stefan
Post Reply