-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
137 lines (117 loc) · 4.53 KB
/
MainWindow.xaml.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Win32;
namespace PDFMerge {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
#region Declarations
public static RoutedUICommand Add = new RoutedUICommand("Add", "Add", typeof(MainWindow), new InputGestureCollection() { new KeyGesture(Key.A, ModifierKeys.Control) });
public static RoutedUICommand MoveUp = new RoutedUICommand("MoveUp", "MoveUp", typeof(MainWindow), new InputGestureCollection() { new KeyGesture(Key.Up, ModifierKeys.Alt) });
public static RoutedUICommand MoveDown = new RoutedUICommand("MoveDown", "MoveDown", typeof(MainWindow), new InputGestureCollection() { new KeyGesture(Key.Down, ModifierKeys.Alt) });
public static RoutedUICommand Combine = new RoutedUICommand("Combine", "Combine", typeof(MainWindow));
#endregion
#region Constructors
public MainWindow() {
InitializeComponent();
}
#endregion
#region Methods
protected void MergePDFs(List<string> filenames) {
string outputPath = string.Concat(this.tbxOutputPath.Text, "\\output.pdf");
File.Delete(outputPath);
bool initialized = false;
int pageOffset = 0;
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream(outputPath, FileMode.Create));
document.Open();
PdfReader reader = null;
foreach (string filename in filenames) {
FileInfo file = new FileInfo(filename);
if (file.Exists) {
reader = new PdfReader(file.FullName);
int pages = reader.NumberOfPages;
pageOffset += pages;
if (!initialized) {
// First time through
initialized = true;
document.SetPageSize(reader.GetPageSizeWithRotation(1));
}
PdfImportedPage page = null;
for (int i = 1; i <= pages; i++) {
page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
}
reader.Close();
}
}
document.Close();
copy.Close();
}
protected void MoveItem(bool moveUp) {
int addValue = (moveUp) ? -1 : 1;
int currentIndex = this.listBox1.SelectedIndex;
int newIndex = currentIndex + addValue;
string selectedItem = (string)this.listBox1.SelectedItem;
this.listBox1.Items.RemoveAt(currentIndex);
this.listBox1.Items.Insert(currentIndex + addValue, selectedItem);
this.listBox1.SelectedItem = this.listBox1.Items[newIndex];
}
#endregion
#region Events
private void ListBox_Delete_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = (this.listBox1.SelectedItem != null);
}
private void ListBox_Delete_Executed(object sender, ExecutedRoutedEventArgs e) {
int selectedItemsCount = this.listBox1.SelectedItems.Count;
for (int i = 0; i < selectedItemsCount; i++) {
string item = (string)this.listBox1.SelectedItems[0];
this.listBox1.Items.Remove(item);
}
}
private void ListBox_Add_Executed(object sender, ExecutedRoutedEventArgs e) {
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;
dialog.Filter = "PDF Files (*.pdf)|*.pdf";
if (dialog.ShowDialog(this) == true) {
foreach (string filename in dialog.FileNames) {
this.listBox1.Items.Add(filename);
}
if (string.IsNullOrEmpty(this.tbxOutputPath.Text)) {
this.tbxOutputPath.Text = Path.GetDirectoryName((string)this.listBox1.Items[0]);
}
}
}
private void ListBox_MoveUp_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = (this.listBox1.SelectedItems.Count == 1 && this.listBox1.SelectedIndex > 0);
}
private void ListBox_MoveUp_Executed(object sender, ExecutedRoutedEventArgs e) {
this.MoveItem(true);
}
private void ListBox_MoveDown_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = (this.listBox1.SelectedItems.Count == 1 && this.listBox1.SelectedIndex < this.listBox1.Items.Count - 1);
}
private void ListBox_MoveDown_Executed(object sender, ExecutedRoutedEventArgs e) {
this.MoveItem(false);
}
private void Combine_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = (this.listBox1.Items.Count > 1);
}
private void Combine_Executed(object sender, ExecutedRoutedEventArgs e) {
List<string> items = new List<string>();
foreach (var item in this.listBox1.Items) {
items.Add((string)item);
}
this.MergePDFs(items);
MessageBox.Show("PDF Combine completed");
Process.Start("explorer", this.tbxOutputPath.Text);
}
#endregion
}
}