Print PDF to another PDF with powershell

Hi everyone

I have many PDFs file with image in the folder and need they solved in PDF again to be opened on Word.

Script command used
Get-ChildItem -Path C:\Users\username\Downloads\PDF* -Include invoice.pdf, mypdf.pdf | ForEach-Object {Start-Process $.FullName -Verb Print}
Get-ChildItem -Path C:\Users\username\Downloads\PDF*.pdf | ForEach-Object {Start-Process $
.FullName -Verb Print}

How to create to save with the same name or “name+prefix” without having to keep typing each file?

I have this is script command too:
$VerbosePreference = “Continue”
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
$newext = “.pdfa”
$Directory = "C:\Users\username\Downloads\PDF"
$files = Get-ChildItem -Path $Directory -Include invoice.pdf, mypdf.pdf |
ForEach-Object {Start-Process $_.FullName -Verb Print}
$focus = 2000

#Foreach ($File in $Files) 
for ($i=0; $i -lt 5; $i++) {
$newfile = $files.BaseName + $newext     
Start-Process $files.FullName -verb Print | out-printer -name "Page Segment Print File"  
start-sleep -Milliseconds $focus
start-sleep -Milliseconds 250
start-sleep -Milliseconds 250
start-sleep -Milliseconds 1500
$focus = 250

Write-Verbose “Print Files Generated”


Welcome to the forum. :wave:t4:

Instead of the built in Windows GUI elements I’d use a command line tool able to do what you need.

Thank you, @Olaf

I’m using this method why I’ll delivery to the user to run on .bat file later

Hmmm … I’m unsure if I get what you want to say. Some things can not be fully automated without using third party tools.

I’m not very open to using third-party tools due to company policies. I chose to use PS because it is a repetitive task for the user in this case. So, as he obtains many files in PDF but scanned (image), today he is opening each file and choosing the print option to be chosen “Microsoft Print to PDF” and saving again because when doing that, the analyst can open it in Word and follow up on what meets the requested requirements.

If a third party tool is needed to accomplish what you or your colleagues are tasked you may ask for a policy change or an exception. :wink:

OK. But if the task you want to automate does not have any API or CLI it will be at least unreliable and error prone to do it this way anyway. :man_shrugging:t4:

What changes actually on this files when you re-print them with the MSFT PDF printer? Wouldn’t be even better to have a proper scan solution able to do the PDF right in the first place?

We receive files that way, including government files.
I need help so that, through the shared powershell script command, I can respond to the request, also accepting the modification of the shared powershell script command or a new solution provided that a directory containing PDF files and save as another PDF file. The other steps to be completed, I have already developed a powershell script command to meet the analyst’s request.

You may wait for someone else to respond to your question here and I’ll keep my fingers crossed for you but I highly recommend for you to at least evaluate another approach using a third party PDF printer with a documented API or Command Line Interface.

Thank you very much @Olaf ,
I’ll look into the possibility of a PDF printer with the responsible department, although it’s quite bureaucratic.

Still, anyone who is interested and can help with the scriptp powershell command that I shared will be welcome and of great aggregation.



Here is the code where I got what I needed.
Thank you, community.

$orders = $SourceFolder
$orders = "C:\Users\**username**\Downloads\PDF\"
$newext = '_1.pdf'
$items = Get-ChildItem -Path $orders -Filter "*.pdf"
$focus = 1500


foreach ($item in $items) {     
    $newfile = $item.BaseName + $newext 
    Start-Process -FilePath $orders\$item -verb Print | out-printer -name "Microsoft Print to PDF"   
    start-sleep -Milliseconds $focus
    start-sleep -Milliseconds 1500
    #[System.Windows.Forms.SendKeys]::SendWait($(Join-Path $destination $newfile)) 
    start-sleep -Milliseconds 1800
    start-sleep -Milliseconds $focus
    $focus = 1500
    Write-Output ($item.BaseName + "_converted" )

Write-Output "$i file(s) converted."
Start-Sleep -Seconds 2

Great that you’ve found a solution. And thanks for sharing. :+1:t4:

1 Like