PowerShell Script: Extract Pages from PDF

Admittedly, I am late to the party, but I have just started digging into PowerShell. The more I dig, the more I find that there is not much you cannot do in PowerShell.

Recently, I was browsing the forums and came across a post asking if it is possible to use LEADTOOLS in PowerShell to split multi-page PDF files into multiple single page PDF files. The answer is absolutely!

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 split a multi-page PDF into single pages. Below are some snippets so you can get an idea of how easy this is.

Use the LEADTOOLS License

The attached script has a function called Unlock-LT to encapsulate the code and do some validation, but in a nutshell:

$licenseFolder = "C:\LEADTOOLS21\License"
$binFolder = "C:\LEADTOOLS21\Bin\Dotnet4\x64"

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

Write-Verbose -Message "Reference the Leadtools Assembly"
Add-Type -Path (Join-Path -Path $binFolder -ChildPath "Leadtools.dll")

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

Now that the LEADTOOLS PDF support is unlocked, we can use the PDFFile class to extract the pages.

Write-Verbose -Message "Reference the Leadtools.Pdf Assembly"
Add-Type -Path (Join-Path -Path $binFolder -ChildPath "Leadtools.Pdf.dll")

Write-Verbose -Message "Create instance of PDFFile class"
$pdfFile = New-Object -TypeName "Leadtools.PDF.PDFFile" -ArgumentList $srcFile

Write-Verbose -Message "Get number of pages in $baseFileName"
$pageCount = $pdfFile.GetPageCount()

for ($i = 1; $i -le $pageCount; $i++){
    $dstFilePath = "$baseDstPath - (page $i).pdf"
    $pdfFile.ExtractPages($i, $i, $dstFilePath)   

Splitting PDF files is just the tip of the iceberg of what is possible with LEADTOOLS and PowerShell. For my next PowerShell project, I think that I will start working on developing some cmdlets to make using LEADTOOLS even cleaner in PowerShell. Comment below if you have any requests!

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 Conversion Samples

Did you see our previous post, β€œMerge PDF Files – C#, VB, and Java Code”? Stay tuned for more conversion examples to see how LEADTOOLS easily fits into any workflow converting PDF files into other document files or images and back again. 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.


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 *