Skip to content

Commit eda15c9

Browse files
authored
Update Convert-Image-To-PDF.ps1
1 parent affe0e3 commit eda15c9

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

Scripts/Convert-Image-To-PDF.ps1

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ Add-Type -AssemblyName System.Drawing
22
Add-Type -AssemblyName System.Windows.Forms
33

44
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
5-
$OpenFileDialog.Filter = "Images (*.jpg)|*.jpg"
5+
$FileDialog.Filter = "Images|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tiff;*.ico"
66
$OpenFileDialog.Multiselect = $false
77

8-
if ($OpenFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
9-
$imgPath = $OpenFileDialog.FileName
10-
Write-Host "File selected: $imgPath" -ForegroundColor Green
8+
if ($FileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
9+
$selectedImagePath = $FileDialog.FileName
10+
Write-Host "File selected: $selectedImagePath" -ForegroundColor Green
1111
Write-Host " "
1212
} else {
13-
Write-Host "No file selected." -ForegroundColor Yellow
13+
Write-Host "No File selected." -ForegroundColor Yellow
1414
Write-Host " "
1515
return
1616
}
1717

18-
$doc = New-Object System.Drawing.Printing.PrintDocument
19-
$doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
20-
$doc.PrinterSettings.PrinterName = "Microsoft Print to PDF"
21-
$doc.PrinterSettings.PrintToFile = $true
22-
$outFile = [System.IO.Path]::ChangeExtension($imgPath, ".pdf")
23-
$doc.PrinterSettings.PrintFileName = $outFile
18+
$printDocument = New-Object System.Drawing.Printing.PrintDocument
19+
$printDocument.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
20+
$printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
21+
$printDocument.PrinterSettings.PrintToFile = $true
22+
$outputFilePath = [System.IO.Path]::ChangeExtension($selectedImagePath, ".pdf")
23+
$printDocument.PrinterSettings.PrintFileName = $outputFilePath
2424

25-
$script:_pageIndex = 0
26-
$doc.add_PrintPage({
25+
$pageIndex = 0
26+
$printDocument.add_PrintPage({
2727
param($sender, [System.Drawing.Printing.PrintPageEventArgs] $e)
28-
$image = [System.Drawing.Image]::FromFile($imgPath)
28+
$image = [System.Drawing.Image]::FromFile($selectedImagePath)
2929
try {
3030
$e.Graphics.DrawImage($image, $e.PageBounds)
3131
$e.HasMorePages = $false
@@ -34,7 +34,6 @@ $doc.add_PrintPage({
3434
}
3535
})
3636

37-
$doc.PrintController = New-Object System.Drawing.Printing.StandardPrintController
38-
39-
$doc.Print()
40-
Write-Host "File printed to $outFile" -ForegroundColor Green
37+
$printDocument.PrintController = New-Object System.Drawing.Printing.StandardPrintController
38+
$printDocument.Print()
39+
Write-Host "File saved in $outputFilePath" -ForegroundColor Green

0 commit comments

Comments
 (0)