gist: Remove PDF Pages

File: Remove_PDF_Pages.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<#
Remove_PDF_Pages.ps1
-John Taylor
Jan-7-2020

This is a simple, GUI frontend to pdfcpu.
https://github.com/pdfcpu/pdfcpu

The resulting PDF will have the filename appended with "--RemovedPages.pdf"
#>

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Open File dialog box
Function Get-FileName($label) {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = [Environment]::GetFolderPath('Desktop')
    $OpenFileDialog.filter = "Adobe (*.pdf)|*.pdf"
    $OpenFileDialog.ShowDialog() | Out-Null
    if ($OpenFileDialog.FileName -like '*--PagesRemoved*') {
        [System.Windows.Forms.MessageBox]::Show($OpenFileDialog.FileName + "`n`nThis file already has already had pages removed.", "Error")
        return
     }
    $label.Text = "File: " + $OpenFileDialog.FileName
    $OpenFileDialog.FileName
}

# GUI Controls
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Remove PDF Pages'
$form.Size = New-Object System.Drawing.Size(500,300)
$form.StartPosition = 'CenterScreen'

if( $args.count -eq 0) {
    $SelectFileButton = New-Object System.Windows.Forms.Button
    $SelectFileButton.Location = New-Object System.Drawing.Point(10,20)
    $SelectFileButton.Size = New-Object System.Drawing.Size(95,20)
    $SelectFileButton.Text = 'Select PDF File'
    $global:pdfFileName = $null
    $SelectFileButton.add_Click({$global:pdfFileName = Get-FileName($FileSelectedLabel)})
    $form.Controls.Add($SelectFileButton)
} else {
    $global:pdfFileName = $args[0]
}

$FileSelectedLabel = New-Object System.Windows.Forms.Label
$FileSelectedLabel.Location = New-Object System.Drawing.Point(10,50)
$FileSelectedLabel.Size = New-Object System.Drawing.Size(490,40)
$FileSelectedLabel.Text = ''
if($args.count -eq 1) {
    $FileSelectedLabel.Text = 'File: ' + $global:pdfFileName
}
$form.Controls.Add($FileSelectedLabel)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,100)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Which pages do you want to remove?'
$form.Controls.Add($label)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,120)
$label.Size = New-Object System.Drawing.Size(320,20)
$label.Text = 'Example: To remove pages 1-3 and page 5, input: 1-3,5'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,140)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,200)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,200)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

# When 'OK' is pressed
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $pagesToRemove = $textBox.Text
    if( $global:pdfFileName -notlike '*pdf') {
        [System.Windows.Forms.MessageBox]::Show($global:pdfFileName + "`n`nThis is not an Adobe PDF file.", "Error")
        exit
    }
    if( $pagesToRemove.Length -eq 0 ) {
        [System.Windows.Forms.MessageBox]::Show("You did not select any pages to be removed.","Error")
        exit
    }
    $newPdfFileName = $global:pdfFileName -replace ".pdf", "--PagesRemoved.pdf"
    pdfcpu.exe pages remove -pages $pagesToRemove "$global:pdfFileName" "$newPdfFileName"
    if( $? -eq $False ) {
        [System.Windows.Forms.MessageBox]::Show("An error occured when trying to remove pages.", "Error")
        return
    }
    # Show resulting file in File Explorer
    explorer.exe /select,"$newPdfFileName"
    [System.Windows.Forms.MessageBox]::Show("Created File:`n`n" + $newPdfFileName, "Remove PDF Pages")
}

# end of script