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.

3 Responses to PowerShell Script: Extract Pages from PDF

  1. Wayne Gunn says:

    Admittedly, I am out of my element with this kind of coding. I don’t use powershell much but I have limited experience with it and mostly, I just copy from others. I see that there are 2 scripts above. The first has to do with setting up a license, and the other is the actual script that does the job. What I wold like to do is call this functionality from vba, so I suppose I will call the script that does the job. But I don’t know how the license script gets included in all of this so I would like some explanations on how exaclty to get this done.

    Also, what is the cost after the evaluation period?

    Thank you

  2. Wayne Gunn says:

    Sorry, it’s I again. I previously sent a message but after the fact I think I know the answer. At first I thought there were 2 scripts but after looking again, I think it is just 1, an upper part that deals with linensing, and then the other that does the work. So I only need to create this 1 script and run it. Did I get it right?

    Thank you

    • Hi Wayne,
      Thank you for your questions. The script in this blog post is, as you say, one single PowerShell script. The first part is setting a LEADTOOLS license and the second part is using LEADTOOLS PDF engine to extract pages.

      If you’re looking for a way to call this script from VBA, then you could try VBA’s Shell function.

      If you have any trouble running this or any other LEADTOOLS script we have a support team dedicated to helping you.

      For licensing information, please contact our sales team so they can tailor licensing terms that suite your use case.

      Both our sales and support teams can be reached through our online chat.

      Thank you,

Leave a Reply

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