Hello there,
Found this script online which is used to Print Multiple Copies of PDF File.
$PrintPageHandler =
{
param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)
$linesPerPage = 0
$yPos = 0
$count = 0
$leftMargin = $ev.MarginBounds.Left
$topMargin = $ev.MarginBounds.Top
$line = $null
$printFont = New-Object System.Drawing.Font "Arial", 10
# Calculate the number of lines per page.
$linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)
# Print each line of the file.
while ($count -lt $linesPerPage -and (($line = $streamToPrint.ReadLine()) -ne $null))
{
$yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
$ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
$count++
}
# If more lines exist, print another page.
if ($line -ne $null)
{
$ev.HasMorePages = $true
}
else
{
$ev.HasMorePages = $false
}
}
function Out-Pdf
{
param($InputDocument, $OutputFolder)
Add-Type -AssemblyName System.Drawing
$doc = New-Object System.Drawing.Printing.PrintDocument
$doc.DocumentName = $InputDocument.FullName
$doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
$doc.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
$doc.PrinterSettings.PrintToFile = $true
$streamToPrint = New-Object System.IO.StreamReader $InputDocument.FullName
$doc.add_PrintPage($PrintPageHandler)
$doc.PrinterSettings.PrintFileName = "$($InputDocument.DirectoryName)\$($InputDocument.BaseName).pdf"
$doc.Print()
$streamToPrint.Close()
}
Get-Childitem -Path "$PSScriptRoot\TextFiles" -File -Filter "*.txt" |
ForEach-Object { Out-Pdf $_ $_.Directory }
Hope this resolves your Query !!
--If the reply is helpful, please Upvote and Accept it as an answer–
Unable to print to pdf double copies (single file) using powershell
Hi,
I am trying to print a single file with 1 page pdf to 2 pages pdf (single file). However, the below code only produces a blank file.
Can someone help me debug or guide me how to fix the issue?
Load Assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Microsoft Print to PDF
$filePath = "C:\Users\m4800\Google Drive\ForYamuna\Worksheets\Print Font Type\01-A\A\01-A.pdf"
$printerName = "Microsoft Print to PDF"
$printerPort = "PORTPROMPT:"
$numCopies = 2
Create a PrinterSettings object and set the number of copies
$printerSettings = New-Object System.Drawing.Printing.PrinterSettings
#$printerSettings.Copies = $numCopies
Create a PrintDocument object and set the file name and printer
$printDoc = New-Object System.Drawing.Printing.PrintDocument
$printDoc.DocumentName = $filePath
$printDoc.PrinterSettings = $printerSettings
Set the printer to Microsoft Print to PDF and print the document
$printDoc.PrinterSettings.PrinterName = $printerName
$printDoc.PrinterSettings.PrintToFile = $true
$printDoc.PrinterSettings.PrintFileName = "$filePath"
$printDoc.PrinterSettings.Copies = $numCopies
PrinterPort doesn't work
#$printDoc.PrinterSettings.PrinterPort = $printerPort
$printDoc.Print()
Wait for the print job to complete
Start-Sleep -Seconds 10
2 answers
Sort by: Most helpful
-
Limitless Technology 43,951 Reputation points
2023-03-27T12:19:52.66+00:00 -
Limitless Technology 43,951 Reputation points
2023-03-27T12:20:06.7866667+00:00 Double post