PowerShell Script: Merge PDF Files

Several of my friends that do not consider themselves to be programmers have asked if I know of an easy way to merge multiple PDF files into one file. Of course, I instantly thought of using LEADTOOLS in PowerShell.

To get started, you need the LEADTOOLS assemblies and a license file. If you have not already purchased LEADTOOLS, you can download and request a 60-day evaluation license from our website. From there, all you need is a text editor to create your PS1 script file.

I have attached the complete PowerShell script to merge multiple PDF files into single file as well as launch.json file for VSCode. Below are some snippets so you can get an idea of how easy this is.

Reference the LEADTOOLS Assemblies

The attached script file includes a helper function, Initialize-LtLibs, to load an array of LEADTOOLS assemblies.


ForEach($a in $assemblies){
    Write-Verbose -Message ("Reference the " + $a + " Assembly")
    Add-Type -Path (Join-Path -Path $Path -ChildPath $a)
}

Use the LEADTOOLS License

The attached script has a function called Unlock-LT to use the LEADTOOLS license file, but in a nutshell:


$licenseFolder = "C:\LEADTOOLS21\License"

$licenseFilePath = Join-Path -Path $licenseFolder -ChildPath "Leadtools.lic"
$licenseKeyFilePath="$licenseFilePath.key"

$licenseKey = Get-Content $licenseKeyFilePath
[Leadtools.RasterSupport]::SetLicense($licenseFilePath, $licenseKey)

Merge Multiple PDF Files

The script to combine the PDF files is straightforward:


function Merge-LTPdfFiles{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Path,

        [Parameter(Mandatory)]
        [string[]]$SourcePaths
    )

    If($SourcePaths -lt 2){
        Write-Error "Need two or more source files to merge"
        return
    }

    [Leadtools.Pdf.PDFFile] $pdfFile = New-Object Leadtools.Pdf.PDFFile($SourcePaths[0])
    $pdfFile.MergeWith($SourcePaths[1..($SourcePaths.length-1)], $Path)
}

Merging PDF files is just one of the many things LEADTOOLS can do. Further, PowerShell opens the LEADTOOLS door to users that may not think of themselves as “programmers”. Anyone that is willing to open notepad and learn to write a simple script can leverage the power of LEADTOOLS.

See For Yourself – Free Evaluation

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Stay Tuned For More PowerShell Samples

Did you see our previous post, “PowerShell Script: Extract Pages from PDF”? Stay tuned for more PowerShell examples to see how LEADTOOLS easily fits into any workflow. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at +1-704-332-5532.

About 

Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in PDF and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *