$form.ShowDialog() # add option for user to customize merged file name, default name is displayed
To allow the user to customize the merged file name, you can add a new text box for the output file name and modify the "Merge" button click event to use the custom name provided by the user. Below is an updated version of your script with the necessary changes:
powershell
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create the main form window
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Merge Images to PDF'
$form.Size = New-Object System.Drawing.Size(900,550) # Increase height to accommodate new controls
$form.StartPosition = 'CenterScreen'
# Label for the folder selection prompt
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(380,20)
$label.Text = 'Select the folder containing images:'
$form.Controls.Add($label)
# TextBox for displaying the selected folder path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
# Button for browsing the folder
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(280,38)
$button.Size = New-Object System.Drawing.Size(100,25)
$button.Text = 'Browse'
$button.Add_Click({
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
if ($folderBrowser.ShowDialog() -eq 'OK') {
$textBox.Text = $folderBrowser.SelectedPath
Update-ListView $textBox.Text
}
})
$form.Controls.Add($button)
# Label for the output file name
$outputLabel = New-Object System.Windows.Forms.Label
$outputLabel.Location = New-Object System.Drawing.Point(10,65)
$outputLabel.Size = New-Object System.Drawing.Size(260,20)
$outputLabel.Text = 'Enter the output file name (without extension):'
$form.Controls.Add($outputLabel)
# TextBox for entering the output file name
$outputTextBox = New-Object System.Windows.Forms.TextBox
$outputTextBox.Location = New-Object System.Drawing.Point(10,85)
$outputTextBox.Size = New-Object System.Drawing.Size(260,20)
$outputTextBox.Text = 'MergedImages' # Default name
$form.Controls.Add($outputTextBox)
# "Select All" button
$selectAllButton = New-Object System.Windows.Forms.Button
$selectAllButton.Location = New-Object System.Drawing.Point(10, 435) # Adjust position
$selectAllButton.Size = New-Object System.Drawing.Size(75,23)
$selectAllButton.Text = 'Select All'
$selectAllButton.Add_Click({
foreach ($item in $listView.Items) {
$item.Checked = $true
}
})
$form.Controls.Add($selectAllButton)
# "Deselect All" button
$deselectAllButton = New-Object System.Windows.Forms.Button
$deselectAllButton.Location = New-Object System.Drawing.Point(100, 435) # Adjust position
$deselectAllButton.Size = New-Object System.Drawing.Size(85,23)
$deselectAllButton.Text = 'Deselect All'
$deselectAllButton.Add_Click({
foreach ($item in $listView.Items) {
$item.Checked = $false
}
})
$form.Controls.Add($deselectAllButton)
# ListView for displaying the image files
$listView = New-Object System.Windows.Forms.ListView
$listView.Location = New-Object System.Drawing.Point(10,110) # Adjust position
$listView.Size = New-Object System.Drawing.Size(870,300)
$listView.View = [System.Windows.Forms.View]::Details
$listView.CheckBoxes = $true
$listView.FullRowSelect = $true
$listView.Columns.Add('Image Files', -2, [System.Windows.Forms.HorizontalAlignment]::Left)
$form.Controls.Add($listView)
# "Merge" button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,460) # Adjust position
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'Merge'
$okButton.Add_Click({
$selectedFiles = $listView.Items | Where-Object { $_.Checked } | ForEach-Object { $_.Tag }
if ($selectedFiles.Count -eq 0) {
[System.Windows.Forms.MessageBox]::Show("No files selected.", "No Selection", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
return
}
$outputPath = Join-Path -Path $textBox.Text -ChildPath (($outputTextBox.Text + ".pdf"))
Merge-ImageToPDF -ImagePaths $selectedFiles -OutputPath $outputPath -Show -ResizeToFitA4
$form.Close()
})
$form.Controls.Add($okButton)
# ComboBox for selecting the sort order
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Location = New-Object System.Drawing.Point(390, 40)
$comboBox.Size = New-Object System.Drawing.Size(120, 20)
$comboBox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$comboBox.Items.Add('Name')
$comboBox.Items.Add('Date Modified')
$comboBox.Items.Add('Size')
$comboBox.SelectedIndex = 0
$form.Controls.Add($comboBox)
# Add an event handler to the ComboBox to refresh the ListView on selection change
$comboBox.add_SelectedIndexChanged({
if ($textBox.Text -ne "") { # Only refresh if a folder is already selected
Update-ListView $textBox.Text
}
})
# "Move Up" button
$moveUpButton = New-Object System.Windows.Forms.Button
$moveUpButton.Location = New-Object System.Drawing.Point(200, 435) # Adjust position
$moveUpButton.Size = New-Object System.Drawing.Size(75,23)
$moveUpButton.Text = 'Move Up'
$moveUpButton.Add_Click({
$selectedItems = $listView.SelectedItems
if ($selectedItems.Count -eq 1) {
$selectedItem = $selectedItems[0]
$index = $selectedItem.Index
if ($index -gt 0) {
$listView.Items.RemoveAt($index)
$listView.Items.Insert($index - 1, $selectedItem)
$selectedItem.Selected = $true
$selectedItem.Focused = $true
}
}
})
$form.Controls.Add($moveUpButton)
# "Move Down" button
$moveDownButton = New-Object System.Windows.Forms.Button
$moveDownButton.Location = New-Object System.Drawing.Point(290, 435) # Adjust position
$moveDownButton.Size = New-Object System.Drawing.Size(75,23)
$moveDownButton.Text = 'Move Down'
$moveDownButton.Add_Click({
$selectedItems = $listView.SelectedItems