Need help integrating Core API to Excel VBA Application to combine PDF  SOLVED

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, 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.
Post Reply
anon5409742
User
Posts: 12
Joined: Wed Jan 27, 2021 3:31 pm

Need help integrating Core API to Excel VBA Application to combine PDF

Post by anon5409742 »

Hi,

We are currently in the process of changing the PDF toolkit that we use in our Excel VBA Application. We are actually controlling pdfTK using command line batch script to be able to merge documents together.

I've installed Core API SDK and started coding some test code to get the hang of it. I've reused code I've found on this forum but now I'm stuck on combining documents together.

I'd like to use a mergePDF function that takes 2 parameters to work: sMainDocPath for the main document that we would like to add pages at the end and the arrayFilesPath array that should hold all the files path that needs to be added. This function should send back the output file path if the operation was successful.

Here is how it's coded so far:

Code: Select all

Public Sub DocCombine()

Dim ary(1) As Variant
Dim sMainDoc As String

'main document that we want to merge other pdf at the end
sMainDoc = Environ("TEMP") & "\2021_01_20_CPAP_212857_Rapport suivi Stickies.pdf"

'build array with all the files we want to combine at the end of main doc
ary(0) = Environ("TEMP") & "\Annexe 1 Effluent Bury X rays.pdf"
ary(1) = Environ("TEMP") & "\Test B.pdf"

'call the mergePDF
Call mergePDF(sMainDoc, ary)

End Sub
Here is mergePDF function. What should be my next step? I've seen in the doc the method op.CombineDocs, but I don't know how to use it.

Code: Select all

Private Function mergePDF(sMainDocPath As String, arrayFilesPath() As Variant) As String

Dim objPXC      As New PDFXCoreAPI.PXC_Inst
Dim objDcb      As IPXC_DocAuthCallback
Dim objDocMain  As IPXC_Document
Dim objDocExtra As IPXC_Document
Dim objContent  As IPXC_Content

objPXC.Init ""

Set objDoc = objPXC.OpenDocumentFromFile(Environ("TEMP") & "\2021_01_20_CPAP_212857_Rapport suivi Stickies.pdf", objDcb)

For i = lbound(arrayFilesPath) to ubound(arrayFilesPath)
	'the following is non-sense, Im just trying to code what I want to do
	objDoc.addPages(arrayFilesPath(i))
next

objdoc.writeToFile Environ("Temp") & "\test.pdf"
objdoc.close
objPXC.Finalize

Set objDoc = nothing
Set objPXC = nothing

End Function


Thanks!
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Need help integrating Core API to Excel VBA Application to combine PDF

Post by Sasha - Tracker Dev Team »

Hello anon5409742,

The op.combineDocs is on the Editor SDK level like all of the operations are.
Check out the CoreAPIDemo application that holds many samples on how to work with the CoreAPI and look at how the work with pages is being done:
https://github.com/tracker-software/PDF ... oreAPIDemo

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
anon5409742
User
Posts: 12
Joined: Wed Jan 27, 2021 3:31 pm

Re: Need help integrating Core API to Excel VBA Application to combine PDF  SOLVED

Post by anon5409742 »

Thanks for this.

I was able to develop something that works. For everyone wondering what I came up with, here is the call sub:

Code: Select all


Public Sub DocCombine()

Dim ary() As String
'main document that we want to merge other pdf at the end

ReDim ary(1)

'Mock-up array
ary(0) = Environ("TEMP") & "\2021_01_20_CPAP_212857_Rapport suivi Stickies.pdf"
ary(1) = Environ("TEMP") & "\2021_01_28_212857_QLT.pdf"

Call mergePDF_XChangeCoreAPI(ary)

End Sub
Here is the combine function. It takes an array as parameter with all the files path. Assume the first one will be the main doc so we insert pages at the end of it.

Code: Select all

Private Function mergePDF_XChangeCoreAPI(arrayFilesPath() As String) As String
''=======================================================
'' Program:     mergePDF_XChangeCoreAPI
'' Desc:        EARLY BINDING: Function to wrap XChangeCoreApi SDK to be able to merge
''              multiple PDFs together.
''              If successful, return the string of the final document,
''              otherwise returns empty string.
''
'' Author:      anon5409742
'' Arguments:   arrayFilesPath - Array to hold all files path. Index (0) should
''              be the first document to add pages to.
''
'' Comments:
''
'' Changes----------------------------------------------
'' Date         Programmer    Change
'' 2021-02-02   XWritten
''=======================================================

Dim objPXC          As New PDFXCoreAPI.PXC_Inst
Dim objDocMain      As IPXC_Document
Dim objDocSlaves    As IPXC_Document
Dim i               As Long

Dim outputFilePath  As String

outputFilePath = Environ("USERPROFILE") & "\Documents\" & Right(arrayFilesPath(0), Len(arrayFilesPath(0)) - InStrRev(arrayFilesPath(0), "\", Len(arrayFilesPath(0))))

'kill output file is already exists
If Len(Dir(outputFilePath)) > 0 Then Kill outputFilePath

'initialization
objPXC.Init ""

'Create new Doc
Set objDocMain = objPXC.NewDocument()

'Use index(0) as first document and insert it
Set objDocSlaves = objPXC.OpenDocumentFromFile(arrayFilesPath(LBound(arrayFilesPath)), Nothing)
objDocMain.Pages.InsertPagesFromDoc objDocSlaves, 0, 0, objDocSlaves.Pages.Count, 0


'Loop all remaining files path and add pages at the end of MAIN doc
For i = LBound(arrayFilesPath) + 1 To UBound(arrayFilesPath)
    Set objDocSlaves = objPXC.OpenDocumentFromFile(arrayFilesPath(i), Nothing)
    
    objDocMain.Pages.InsertPagesFromDoc objDocSlaves, objDocMain.Pages.Count, 0, objDocSlaves.Pages.Count, 0
    'Empty variable at each loop
    Set objDocSlaves = Nothing
Next i

'Write final file and finalize everything
objDocMain.WriteToFile outputFilePath
objDocMain.Close
objPXC.Finalize

'If write was successful and file is present, give the filepath back to function caller.
If Len(Dir(outputFilePath)) > 0 Then
    mergePDF_XChangeCoreAPI = outputFilePath
Else
    MsgBox "Something went wront while merging.", vbCritical
End If

'Free variable
Set objPXC = Nothing
Set objDocMain = Nothing
Set objDocSlaves = Nothing

End Function
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Need help integrating Core API to Excel VBA Application to combine PDF

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Syntax
User
Posts: 6
Joined: Thu Mar 04, 2021 5:24 pm

Re: Need help integrating Core API to Excel VBA Application to combine PDF

Post by Syntax »

Hello!

Congratulations anon5409742, nice code... although something weird happens: im able to merge to several PDFs... but then i get a catastrophic failure every time i run the routine.

The crash happens at row

Code: Select all

MainPDF.WriteToFile OutputPath
The error is:
image.png
image.png (5.58 KiB) Viewed 10394 times

If i restart excel, i can do it again (one time only).
Seems like there must something left open?

Any ideas?

Thanks in advance!
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Need help integrating Core API to Excel VBA Application to combine PDF

Post by Sasha - Tracker Dev Team »

Hello Syntax,

We'll need to see all of your code to assist further.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Syntax
User
Posts: 6
Joined: Thu Mar 04, 2021 5:24 pm

Re: Need help integrating Core API to Excel VBA Application to combine PDF

Post by Syntax »

Hello Sasha!

Thanks for the reply.
The code i used was the same created by anon5409742, in this post. :)

For the sake of testing, i created a small routine... which executes without problem (once).
Then i need to close and open the excel file in order to be able to run the routine again.
This leds me to believe that its an internal DLL issue or maybe something is missing to be closed... any idea?

Code: Select all

Public Sub CreateDoc()
    Dim PXC As New PXC_Inst
    Dim Doc As IPXC_Document
    
    'Initialize
    PXC.Init ""
    
    'Create new document, save as pdf
    Set Doc = PXC.NewDocument()
    Doc.WriteToFile "c:\users\syntax\desktop\Test.pdf"
    
    'Close document and Finalize
    Doc.Close
    PXC.Finalize
End Sub
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Need help integrating Core API to Excel VBA Application to combine PDF

Post by Sasha - Tracker Dev Team »

Hello Syntax,

Can you try creating and initializing the IPXC_Inst once and then Finalizing it once per workflow? Meaning you initialize it when Excel starts and Finalize it when it shuts down - then everything should work normally.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Syntax
User
Posts: 6
Joined: Thu Mar 04, 2021 5:24 pm

Re: Need help integrating Core API to Excel VBA Application to combine PDF

Post by Syntax »

THANK YOU SO VERY MUCH! :D :D
Worked perfectly. :wink:
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Need help integrating Core API to Excel VBA Application to combine PDF

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply