-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
139 lines (117 loc) · 4.23 KB
/
Form1.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
138
139
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PdfSharp.Pdf;
namespace pdfw
{
public partial class Form1 : Form
{
private List<string> filenames;
public Form1()
{
InitializeComponent();
this.Text = "PDF Merger";
filenames = new List<string>();
typeof(Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(listView1, true, null);
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Test");
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.FileOk += fileDialogOk;
fileDialog.Filter = "Portable Document Format|*.pdf";
fileDialog.Multiselect = true;
fileDialog.ShowDialog();
}
private void fileDialogOk(object sender, CancelEventArgs e)
{
if(!e.Cancel)
{
string[] files = ((OpenFileDialog)sender).FileNames;
foreach (string file in files)
{
filenames.Add(file);
ListViewItem item = new ListViewItem(new FileInfo(file).Name);
item.ToolTipText = file;
item.ImageIndex = 0;
listView1.Items.Add(item);
}
if(filenames.Count > 1)
mergeButton.Enabled = true;
}
}
private string[] getItemsInOrder()
{
string[] ordered = new string[listView1.Items.Count];
SortedDictionary<Tuple<int, int>, string> points = new SortedDictionary<Tuple<int, int>, string>();
foreach(ListViewItem item in listView1.Items)
{
Tuple<int, int> tp = new Tuple<int, int>(item.Position.Y, item.Position.X);
points.Add(tp, item.ToolTipText);
}
int i = 0;
foreach(KeyValuePair<Tuple<int, int>, string> kvp in points)
{
ordered[i] = kvp.Value;
++i;
}
return ordered;
}
private void mergeButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileOk += saveFileOk;
saveFileDialog.Filter = "Portal Document Format|*.pdf";
saveFileDialog.ShowDialog();
}
private void saveFileOk(object sender, CancelEventArgs e)
{
PdfDocument[] docs = new PdfDocument[filenames.Count];
int i = 0;
PdfDocument doc = new PdfDocument();
foreach (string filename in getItemsInOrder())
{
docs[i] = PdfSharp.Pdf.IO.PdfReader.Open(filename, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
foreach (PdfPage page in docs[i].Pages)
{
doc.AddPage(page);
}
++i;
}
doc.Save(((SaveFileDialog)sender).FileName);
filenames.Clear();
listView1.Clear();
mergeButton.Enabled = false;
}
ListViewItem heldDownItem;
Point heldDownPoint;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
listView1.AutoArrange = false;
heldDownItem = listView1.GetItemAt(e.X, e.Y);
if(heldDownItem != null)
{
heldDownPoint = new Point(e.X - heldDownItem.Position.X, e.Y - heldDownItem.Position.Y);
}
}
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
if(heldDownItem != null)
{
heldDownItem.Position = new Point(e.Location.X - heldDownPoint.X, e.Location.Y - heldDownPoint.Y);
}
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
heldDownItem = null;
listView1.AutoArrange = true;
}
}
}