Maintain PDF properties by VBA

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

Moderators: Tracker Support, TrackerSupp-Daniel, Sean - Tracker, Vasyl-Tracker Dev Team, Chris - Tracker Supp, 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
Roland_27
User
Posts: 3
Joined: Wed Aug 28, 2024 7:46 am

Maintain PDF properties by VBA

Post by Roland_27 »

Dear all,
We need to switch from Acrobat Distiller to PDF-XChange and I have already managed to merge PDF files into one.
Now there is another problem to solve. We need to set some PDF properties like “title” or “author” with VBA code.
It is part of a batch process and several 100 PDF files are created. Maintaining this with a manual editor is not an option.

I have found in https://downloads.pdf-xchange.com/Edito ... DK_MAN.pdf the function PXCV_GetDocumentInfoW, which seems to be the function to read the properties. I cannot find the write function.

Many thanks for any help.
Roland
Roland_27
User
Posts: 3
Joined: Wed Aug 28, 2024 7:46 am

Re: Maintain PDF properties by VBA

Post by Roland_27 »

I just found the solution.
If there is only one document, maintain the property values in the source (word, excel, PowerPoint, ... )
produce the pdf with:

Code: Select all

    'If it is a Word document
    If Not DOC Is Nothing Then
        With DOC
            .ExportAsFixedFormat PDFOutputFilename, 17, False, 0, , , , , True, , , , True, False
        End With
    End If
    'If it is an Excel document
    If Not SHT Is Nothing Then
        With SHT
            .ExportAsFixedFormat xlTypePDF, PDFOutputFilename, , True, , , , False
        End With
    End If
If you have to merge several pdf files and take care, there is in the final document the correct property title, you must set the values in the first file of the merged files.
Instead of open the Set objDocMain = objPXC.NewDocument() take the first document of the list as objDocMain.
I hope it helps...
Best regards
Roland

Code: Select all

Public Function testPDF(strInput As String)
    On Error GoTo testPDF_error
' execute in direct window:
' (replace files with your pdf files)
'testPDF "W:\your path to file\FileOne.pdf#" _
       & "W:\your path to file\FileTwo.pdf#" _
       & "W:\your path to file\FileThree.pdf#" _
       & "W:\your path to file\FileFour.pdf"

    Dim objDocMain       As IPXC_Document  ' holder for the main document
    Dim objDocSlaves     As IPXC_Document  ' add the additional pages
    Dim i                As Long           ' counter for page add loop
    Dim bolObjPxcDone    As Boolean        ' check for objPXC license object
    
    Dim arrayFilesPath() As String
    arrayFilesPath = Split(strInput, "#")

    '/// first page as main:
    ' set objPXC flag to false - error 91 missing object can be for other objects as well...
    bolObjPxcDone = False
    'Create Main Doc for first page - his must contain all properties like title, author, etc
    Set objDocMain = objPXC.OpenDocumentFromFile(arrayFilesPath(0), Nothing)
    ' set objPXC flag to true again.
    bolObjPxcDone = True
    
    '/// catch second document
    Set objDocSlaves = objPXC.OpenDocumentFromFile(arrayFilesPath(1), Nothing)
    'add it to final document
    objDocMain.Pages.InsertPagesFromDoc objDocSlaves, objDocMain.Pages.Count, 0, objDocSlaves.Pages.Count, 0
    'Empty variable
    Set objDocSlaves = Nothing
    
    '/// Loop all remaining files path and add pages at the end of MAIN doc
    If UBound(arrayFilesPath) > 1 Then
        For i = 2 To UBound(arrayFilesPath)
            If arrayFilesPath(i) <> "" Then
                'catch a document
                Set objDocSlaves = objPXC.OpenDocumentFromFile(arrayFilesPath(i), Nothing)
                
                'add it to final document
                objDocMain.Pages.InsertPagesFromDoc objDocSlaves, objDocMain.Pages.Count, 0, objDocSlaves.Pages.Count, 0
                
                'Empty variable at each loop
                Set objDocSlaves = Nothing
            End If
        Next i
    Else
        ' only 2 pages... no further action
    End If

MainDocComplete:

    'Write final file
    objDocMain.WriteToFile "W:\L\BDTP\Products\FSA_EquityAdvisory\Output\Test\sirtTest.pdf"
    objDocMain.Close

testPDF_exit:
    Exit Function

testPDF_error:
    Select Case Err.Number
        Case -2080440318 ' Automation error
            ' looks like a file is not available. take next file
            If i = UBound(arrayFilesPath) Then
                Set objDocSlaves = Nothing  ' close object and jump out of the loop
                GoTo MainDocComplete
            Else
                i = i + 1                   ' take next file
                Resume
            End If
        Case 91 ' Object variable or With block variable not set
            If bolObjPxcDone = False Then
                initiatePdfXchange
                Resume
            End If
        Case Else
            Debug.Print Err.Number; "' "; Err.Description
            Stop
            Resume
    End Select
End Function

Public Function initiatePdfXchange()
    Dim objFSO As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objTF As Object
    
    ' replace the file with your license file
    Set objTF = objFSO.OpenTextFile("\\your path to file\pdfXchange_license.txt", 1)
    Dim apiKey As String
    
    apiKey = objTF.readall

    'initialization
    Set objPXC = New PDFXCoreAPI.PXC_Inst
    objPXC.Init sKey:=apiKey
    
End Function
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2409
Joined: Thu Jun 30, 2005 4:11 pm

Re: Maintain PDF properties by VBA

Post by Vasyl-Tracker Dev Team »

Hi Roland.

With our Core API SDK you may try to use this way, to read/write the doc's props:

https://sdkhelp.pdf-xchange.com/view/PX ... ument_Info

HTH.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Post Reply