-
Notifications
You must be signed in to change notification settings - Fork 6
/
PDFViewer.cs
467 lines (415 loc) · 15.1 KB
/
PDFViewer.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace XPdfNet
{
public partial class PDFViewer : UserControl
{
private PDFLibNet.PDFWrapper pdfDoc;
private int pdfPageCount;
private int scrollUnitsPerPage;
private int currentPageNumber;
private int lastPageNumber;
private bool pageInitDone;
//private bool continuousPages = true;
private string pdfFileName;
//private string userPassword;
//private string ownerPassword;
//private string password;
private Point panStartPoint;
private bool loading;
private bool doZoom;
private const int DEFAULT_ZOOM_INDEX = 0;
private const double ZOOM_FACTOR = 1.25;
private Dictionary<string, object> zoomLevels = new Dictionary<string, object>() {
{ "Fit To Width", ViewMode.FIT_WIDTH },
{ "Fit To Screen", ViewMode.FIT_TO_SCREEN },
{ "200%", 200D },
{ "150%", 150D },
{ "125%", 125D },
{ "100%", 100D },
{ "75%", 75D },
{ "50%", 50D },
{ "25%", 25D } };
public PDFViewer()
{
InitializeComponent();
this.Disposed += new EventHandler(this.PDFViewer_Disposed);
FlowPanel.MouseWheel += new MouseEventHandler(this.FlowPanel_MouseWheel);
FlowPanel.KeyDown += new KeyEventHandler(FlowPanel_KeyDown);
FlowPanel.KeyUp += new KeyEventHandler(FlowPanel_KeyUp);
}
public string FileName
{
get
{
return pdfFileName;
}
set
{
if (value != null && value != "" && ImageUtil.IsPDF(value))
{
//userPassword = "";
//ownerPassword = "";
//password = "";
ShowLoading = true;
if (pdfDoc != null) pdfDoc.Dispose();
try
{
pdfDoc = new PDFLibNet.PDFWrapper("");
pdfDoc.LoadPDF(value);
}
//catch (System.Security.SecurityException ex)
//{
//}
catch
{
if (pdfDoc != null) pdfDoc.Dispose();
throw;
}
//Initialize zoom dropdown
tscbZoom.Items.Clear();
string[] keys = new string[zoomLevels.Keys.Count];
zoomLevels.Keys.CopyTo(keys, 0);
tscbZoom.Items.AddRange(keys);
pdfPageCount = pdfDoc.PageCount;
currentPageNumber = 1;
tscbZoom.SelectedIndex = DEFAULT_ZOOM_INDEX;
pdfFileName = value;
ShowLoading = false;
this.Enabled = true;
}
else
{
this.Enabled = false;
}
}
}
public int PageCount
{
get
{
return pdfPageCount;
}
}
public bool Print()
{
if (pdfDoc == null)
return false;
else
return PrinterUtil.PrintImagesToPrinter(pdfDoc);
}
public bool Print(string filename)
{
//TODO
if (filename != null && filename != "" && ImageUtil.IsPDF(filename))
{
PDFLibNet.PDFWrapper tmpDoc = new PDFLibNet.PDFWrapper("");
tmpDoc.LoadPDF(filename);
return PrinterUtil.PrintImagesToPrinter(tmpDoc);
}
else
return false;
}
public bool ShowLoading
{
get
{
return loading;
}
set
{
if (value != loading)
{
if (value)
{
Cursor.Current = Cursors.WaitCursor;
FlowPanel.Controls.Clear();
FlowPanel.Controls.Add(LoadingLbl);
FlowPanel.Controls.Add(LoadingBar);
}
else
{
FlowPanel.Controls.Remove(LoadingLbl);
FlowPanel.Controls.Remove(LoadingBar);
Cursor.Current = Cursors.Default;
}
LoadingLbl.Visible = LoadingBar.Visible = loading = value;
if (value) Application.DoEvents();
}
}
}
public bool ShowPrintBtn
{
get
{
return tsPrint.Visible;
}
set
{
tsPrint.Visible = toolStripSeparator2.Visible = value;
}
}
#region Events
private void PDFViewer_Disposed(object sender, EventArgs e)
{
if (pdfDoc != null) pdfDoc.Dispose();
}
private void PDFViewer_ControlRemoved(object sender, ControlEventArgs e)
{
if (pdfDoc != null) pdfDoc.Dispose();
}
private void PDFViewer_Resize(object sender, EventArgs e)
{
if (pageInitDone)
CenterPicBoxesInPanel();
// Center the label and progress bar.
LoadingLbl.Margin = new Padding((FlowPanel.Width - LoadingLbl.Width) / 2,
(FlowPanel.Height - LoadingLbl.Height - LoadingBar.Height - LoadingBar.Margin.Top) / 2,
LoadingLbl.Margin.Right, LoadingLbl.Margin.Bottom);
LoadingBar.Margin = new Padding(LoadingLbl.Margin.Left, LoadingBar.Margin.Top,
LoadingBar.Margin.Right, LoadingLbl.Margin.Top);
}
private void tscbZoom_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyZoom(zoomLevels[tscbZoom.SelectedItem.ToString()]);
}
private void tsFirst_Click(object sender, EventArgs e)
{
GoToPage(1);
}
private void tsPrevious_Click(object sender, EventArgs e)
{
GoToPage(currentPageNumber - 1);
}
private void tsNext_Click(object sender, EventArgs e)
{
GoToPage(currentPageNumber + 1);
}
private void tsLast_Click(object sender, EventArgs e)
{
GoToPage(pdfPageCount);
}
private void tsZoomOut_Click(object sender, EventArgs e)
{
double zoom = pdfDoc.Zoom / ZOOM_FACTOR;
tscbZoom.Text = String.Format("{0:F0}%", zoom);
ApplyZoom(zoom);
}
private void tsZoomIn_Click(object sender, EventArgs e)
{
double zoom = pdfDoc.Zoom * ZOOM_FACTOR;
tscbZoom.Text = String.Format("{0:F0}%", zoom);
ApplyZoom(zoom);
}
private void PicImage_MouseDown(object sender, MouseEventArgs e)
{
Cursor = Cursors.Hand;
panStartPoint = new Point(e.X, e.Y);
}
private void PicImage_MouseUp(object sender, MouseEventArgs e)
{
Cursor = Cursors.Default;
}
private void PicImage_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ScrollableControl parent = (ScrollableControl)((Control)sender).Parent;
parent.AutoScrollPosition = new Point((panStartPoint.X - e.X - parent.AutoScrollPosition.X), (panStartPoint.Y - e.Y - parent.AutoScrollPosition.Y));
CalcCurrentPage();
}
}
private void tsPrint_Click(object sender, EventArgs e)
{
Print();
}
private void FlowPanel_Scroll(object sender, ScrollEventArgs e)
{
CalcCurrentPage();
}
private void FlowPanel_MouseWheel(object sender, MouseEventArgs e)
{
if (doZoom && !ShowLoading)
{
if (e.Delta < 0)
ApplyZoom(-ZOOM_FACTOR);
else if (e.Delta > 0)
ApplyZoom(ZOOM_FACTOR);
}
else
CalcCurrentPage();
}
private void FlowPanel_Click(object sender, EventArgs e)
{
FlowPanel.Focus();
}
void FlowPanel_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
doZoom = false;
}
void FlowPanel_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
doZoom = true;
}
#endregion
#region Helper Functions
private enum ViewMode
{
FIT_TO_SCREEN,
FIT_WIDTH
//ACTUAL_SIZE
}
private PictureBox FindPictureBox(string controlName)
{
return (PictureBox)FindControl(FlowPanel, controlName);
}
private Control FindControl(Control container, string name)
{
Queue<Control> searchQueue = new Queue<Control>();
Control node;
searchQueue.Enqueue(container);
while (searchQueue.Count > 0)
{
node = searchQueue.Dequeue();
if (node.Name == name)
return node;
foreach (Control child in node.Controls)
searchQueue.Enqueue(child);
}
return null;
}
private void CenterPicBoxesInPanel()
{
//if (flowPanel != null)
foreach (Control c in FlowPanel.Controls)
if (c.GetType() == typeof(PictureBox))
ImageUtil.RecalcContinuousPageLocation((PictureBox)c, false);
}
private void InitializePageView(object mode)
{
FlowPanel.SuspendLayout();
foreach (Control c in FlowPanel.Controls)
if (c.GetType() == typeof(PictureBox))
FlowPanel.Controls.Remove(c);
for (int i = 1; i <= pdfPageCount; i++)
{
PictureBox pictureBox = new PictureBox();
pictureBox.Name = i.ToString();
pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox.Height = FlowPanel.Height - pictureBox.Margin.Top * 2;
pictureBox.Width = FlowPanel.Width - pictureBox.Margin.Right * 2 - SystemInformation.VerticalScrollBarWidth;
pictureBox.MouseUp += PicImage_MouseUp;
pictureBox.MouseDown += PicImage_MouseDown;
pictureBox.MouseMove += PicImage_MouseMove;
pictureBox.MouseClick += FlowPanel_Click;
FlowPanel.Controls.Add(pictureBox);
ApplyZoomToPDF(pictureBox, mode);
GetImageFromPDF(i, pictureBox);
}
FlowPanel.ResumeLayout();
scrollUnitsPerPage = (FlowPanel.VerticalScroll.Maximum-FlowPanel.VerticalScroll.LargeChange) / pdfPageCount;
pageInitDone = true;
}
private void UpdatePageLabel()
{
tsPageLabel.Text = "Page " + currentPageNumber + " of " + pdfPageCount;
}
private void CalcCurrentPage()
{
currentPageNumber = (int)(System.Math.Floor((double)FlowPanel.VerticalScroll.Value / scrollUnitsPerPage)) + 1;
UpdatePageLabel();
}
private void ApplyZoom(object mode)
{
ShowLoading = true;
InitializePageView(mode);
ShowLoading = false;
CenterPicBoxesInPanel();
DisplayCurrentPage();
}
private void DisplayCurrentPage()
{
CheckPageBounds();
FlowPanel.ScrollControlIntoView(FindPictureBox(currentPageNumber.ToString()));
UpdatePageLabel();
}
private void CheckPageBounds()
{
if (currentPageNumber > pdfPageCount)
currentPageNumber = pdfPageCount;
else if (currentPageNumber < 1)
currentPageNumber = 1;
if (currentPageNumber < pdfPageCount)
{
tsNext.Enabled = true;
tsLast.Enabled = true;
}
else
{
tsNext.Enabled = false;
tsLast.Enabled = false;
}
if (currentPageNumber > 1)
{
tsPrevious.Enabled = true;
tsFirst.Enabled = true;
}
else
{
tsPrevious.Enabled = false;
tsFirst.Enabled = false;
}
}
private void ApplyZoomToPDF(PictureBox pictureBox, object mode)
{
pictureBox.Width += SystemInformation.VerticalScrollBarWidth;
if (mode.GetType() == typeof(ViewMode))
{
if ((ViewMode)mode == ViewMode.FIT_WIDTH)
pdfDoc.FitToWidth(pictureBox.Handle);
else if ((ViewMode)mode == ViewMode.FIT_TO_SCREEN)
{
pdfDoc.FitToHeight(pictureBox.Handle);
if (pdfDoc.PageWidth > pdfDoc.PageHeight)
pdfDoc.FitToWidth(pictureBox.Handle);
}
}
else if (mode.GetType() == typeof(double))
pdfDoc.Zoom = Math.Abs((double)mode);
pictureBox.Width -= SystemInformation.VerticalScrollBarWidth;
}
private void GetImageFromPDF(int page, PictureBox pictureBox)
{
if (pdfDoc != null)
{
pdfDoc.CurrentPage = page;
pdfDoc.CurrentX = pdfDoc.CurrentY = 0;
pdfDoc.RenderPage(pictureBox.Handle);
pictureBox.Image = new Bitmap(pdfDoc.PageWidth, pdfDoc.PageHeight);
pdfDoc.ClientBounds = new Rectangle(0, 0, pdfDoc.PageWidth, pdfDoc.PageHeight);
using (Graphics g = Graphics.FromImage(pictureBox.Image))
{
pdfDoc.DrawPageHDC(g.GetHdc());
g.ReleaseHdc();
}
}
}
private void GoToPage(int page)
{
lastPageNumber = currentPageNumber;
currentPageNumber = page;
DisplayCurrentPage();
}
#endregion
private void tscbZoom_TextChanged(object sender, EventArgs e)
{
}
}
}