-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialogMergeFiles.vb
2794 lines (2794 loc) · 145 KB
/
dialogMergeFiles.vb
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.Windows.Forms
Imports System.Net
Imports System.IO
Imports Microsoft.VisualBasic
Imports iTextSharp.text.pdf
Public Class dialogMergeFiles
''' <summary>
''' PdForms.net - An open source pdf form editor
''' Copyright 2018 NK-INC.COM All Rights reserved.
''' PdForms.net utilizes iTextSharp technologies.
''' Website: www.pdforms.net (source code), www.pdforms.com (about)
''' </summary>
Public _frm As frmMain = Nothing
Public _fileBytes() As Byte = Nothing
Public _fileName As String = ""
Public colSortDirection As System.ComponentModel.ListSortDirection
Public Property frm() As frmMain
Get
If _frm Is Nothing Then
If Me.Owner.GetType Is GetType(frmMain) Then
_frm = DirectCast(Me.Owner, frmMain)
End If
End If
Return _frm
End Get
Set(ByVal value As frmMain)
_frm = value
End Set
End Property
Public mergedDocumentFileName As String = ""
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
MergePDFFiles()
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Function MergePDFFiles() As Byte()
Dim MemStream As New MemoryStream
Try
If True = True Then
Dim files As New List(Of String)
Dim pages As New List(Of String)
For r As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Columns.Contains("chkSelected") Then
Dim chk As New DataGridViewCheckBoxCell
chk = DirectCast(DataGridView1.Rows(r).Cells("chkSelected"), DataGridViewCheckBoxCell)
If chk.Value = True Then
Dim f As String = fsItems(r).FullPath.ToString
If Not String.IsNullOrEmpty(f & "") Then
If frm.GetFileExtension(f & "").ToString.Replace(".", "").ToLower() = "pdf" Then
If Not files.Contains(f & "") Then
Try
Dim pageSel As String = CStr(DataGridView1.Rows(r).Cells("PageSelection").Value)
pages.Add(pageSel & "")
files.Add(f & "")
Catch ex As Exception
frm.TimeStampAdd(ex, frm.debugMode)
End Try
End If
End If
End If
End If
End If
Next
If files.Count > 0 Then
Select Case MessageBox.Show("Flatten all form fields?", "Flatten?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case DialogResult.Yes, DialogResult.OK
MemStream = New MemoryStream(frm.MergePDFs(files.ToArray(), pages.ToArray(), True))
Case Else
Select Case MessageBox.Show("Rename identical field names?", "Rename Fields?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case DialogResult.Yes, DialogResult.OK
MemStream = New MemoryStream(frm.MergePDFs(files.ToArray(), pages.ToArray(), False, True, True))
Case Else
MemStream = New MemoryStream(frm.MergePDFs(files.ToArray(), pages.ToArray(), False, False, True))
End Select
End Select
Else
Return Nothing
End If
If Not MemStream Is Nothing Then
If MemStream.Length <= 0 Then Return Nothing
Dim b() As Byte = frm.GetUsedBytesOnly(MemStream, True)
frm.preventClickDialog = True
If String.IsNullOrEmpty(frm.fpath & "") Then
frm.SaveFileDialog1.InitialDirectory = frm.ApplicationDataFolder(True, "") & "\" & ""
Else
frm.SaveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(frm.fpath)
End If
frm.SaveFileDialog1.Filter = "PDF|*.pdf"
If String.IsNullOrEmpty(frm.fpath & "") Then
frm.SaveFileDialog1.FileName = ""
Else
frm.SaveFileDialog1.FileName = System.IO.Path.GetFileName(frm.fpath & "")
End If
frm.SaveFileDialog1.AutoUpgradeEnabled = True
frm.SaveFileDialog1.DefaultExt = ".pdf"
frm.SaveFileDialog1.FilterIndex = 1
frm.SaveFileDialog1.Title = "Save As"
frm.SaveFileDialog1.ValidateNames = True
frm.SaveFileDialog1.OverwritePrompt = True
Try
Select Case frm.SaveFileDialog1.ShowDialog(Me)
Case Windows.Forms.DialogResult.OK, Windows.Forms.DialogResult.Yes
If Not String.IsNullOrEmpty(frm.SaveFileDialog1.FileName) Then
Dim fn As String = frm.SaveFileDialog1.FileName & ""
Select Case frm.GetFileExtension(fn).ToString.Replace(".", "").ToLower
Case "pdf"
File.WriteAllBytes(fn, b)
mergedDocumentFileName = fn
Case "png"
b = frm.A0_LoadImage(b)
File.WriteAllBytes(fn, b)
Case "jpg"
b = frm.A0_LoadImage(b)
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(b))
i.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg)
Case "bmp"
b = frm.A0_LoadImage(b)
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(b))
i.Save(fn, System.Drawing.Imaging.ImageFormat.Bmp)
Case "gif"
b = frm.A0_LoadImage(b)
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(b))
i.Save(fn, System.Drawing.Imaging.ImageFormat.Gif)
Case "tiff"
b = frm.A0_LoadImage(b)
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(b))
i.Save(fn, System.Drawing.Imaging.ImageFormat.Tiff)
Case Else
File.WriteAllBytes(fn, b)
End Select
End If
Case Else
Return Nothing
End Select
Return b
Catch ex As Exception
frm.TimeStampAdd(ex, frm.debugMode)
Finally
frm.timerPreventDefaultExpires.Enabled = True
End Try
End If
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
Return Nothing
End Function
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFTP.Click
Try
ListDirectoryDetails()
Catch ex As Exception
Throw ex
End Try
End Sub
Public cancelProgress As Boolean = False
Public Function UploadFile(ByVal fname As String, ByVal fbytes() As Byte, Optional ByVal overwriteFile As Boolean = False, Optional ByVal refreshList As Boolean = True) As Boolean
Try
If String.IsNullOrEmpty(CStr(fname & "").Trim()) Then Return False
If Not fbytes Is Nothing Then
If fbytes.Length <= 0 Then Return False
Else
Return False
End If
If fname.Contains("\"c) Then
Return False
End If
btnFileUpload.Text = "Cancel Upload"
cancelProgress = False
_fileName = CStr(fname & "").Trim() & ""
If overwriteFile = False Then
For r As Integer = 0 To DataGridView1.RowCount - 1
If DataGridView1.Rows(r).Cells(1).Value.ToString.ToLower = fname.ToString.ToLower Then
Select Case MsgBox("Overwrite: " & fname & "", MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question Or MsgBoxStyle.ApplicationModal, "Overwrite?")
Case MsgBoxResult.Yes, MsgBoxResult.Ok
Exit For
Case Else
Return False
End Select
End If
Next
End If
Dim strComplete As String = "Success"
Dim ftpUploadPath As String = getFTPPath() & _fileName.ToString
File.WriteAllBytes(ftpUploadPath, _fileBytes)
GOTO_CLOSE:
Console.WriteLine("Upload File " & strComplete & ", status {0}" & Environment.NewLine & "Uploaded File Name:{1}", "Success", _fileName.ToString)
TextBox2.Text = String.Format("Upload File " & strComplete & ", status {0}" & Environment.NewLine & "Uploaded File Name:{1}", "Success", _fileName.ToString) & Environment.NewLine & TextBox2.Text
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
Finally
ProgressBar1.Visible = False
btnFileUpload.Text = "Upload File"
cancelProgress = False
End Try
If refreshList Then
ListDirectoryDetails()
End If
Return True
End Function
Public Function FTP_FileGetByteLength(ByVal fname As String) As Long
Try
If String.IsNullOrEmpty(CStr(fname & "").Trim()) Then Return -1
If fname.Contains("\"c) Then
Return -1
End If
fname = CStr(fname & "").Trim() & ""
Dim ftpUploadPath As String = getFTPPath() & fname.ToString
Using fs As New FileStream(ftpUploadPath, FileMode.Open)
Try
Return fs.Length
Catch ex As Exception
Err.Clear()
Finally
fs.Close()
fs.Dispose()
End Try
End Using
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
Return -1
End Function
Public Sub RenameFile(ByVal old_fname As String, ByVal new_fname As String)
Try
If String.IsNullOrEmpty(CStr(old_fname & "").Trim()) Or String.IsNullOrEmpty(CStr(new_fname & "").Trim()) Then Return
If new_fname.Contains("\"c) Then
Return
End If
_fileName = CStr(old_fname & "").Trim() & ""
Dim ftpUploadPath As String = getFTPPath() & new_fname & ""
If File.Exists(ftpUploadPath) Then
Select Case MsgBox("Overwrite existing file: " & new_fname & "", MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Confirm Rename:")
Case MsgBoxResult.Yes, MsgBoxResult.Ok
File.WriteAllBytes(ftpUploadPath, File.ReadAllBytes(getFTPPath() & old_fname))
End Select
Else
File.WriteAllBytes(ftpUploadPath, File.ReadAllBytes(getFTPPath() & old_fname))
End If
Console.WriteLine("Renamed File Complete, status {0}" & Environment.NewLine & "Old File Name: {1}, New File Name:{2}", "Success", old_fname.ToString, new_fname.ToString)
TextBox2.Text = String.Format("Renamed File Complete, status {0}" & Environment.NewLine & "Old File Name: {1}, New File Name:{2}", "Success", old_fname.ToString, new_fname.ToString) & Environment.NewLine & TextBox2.Text
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
ListDirectoryDetails()
End Sub
Public Sub DeleteFile(ByVal old_fname As String, Optional ByVal confirmDelete As Boolean = True, Optional ByVal refreshDirectoryList As Boolean = True)
Try
If cancelProgress = True Then
Return
End If
If String.IsNullOrEmpty(CStr(old_fname & "").Trim()) Then Return
If confirmDelete Then
Select Case MsgBox("Confirm delete: " & old_fname, MsgBoxStyle.Question Or MsgBoxStyle.YesNoCancel Or MsgBoxStyle.ApplicationModal, "Confirm Delete:")
Case MsgBoxResult.Yes
Exit Select
Case Else
Return
End Select
End If
_fileName = CStr(old_fname & "").Trim() & ""
Dim ftpUploadPath As String = _fileName & ""
If Not old_fname.ToLower.StartsWith(CStr(getFTPPath()).ToLower) Then
ftpUploadPath = CStr(getFTPPath() & _fileName.ToString)
End If
If cancelProgress = True Then
Return
End If
File.Delete(ftpUploadPath)
Console.WriteLine("Deleted File Complete, status {0}" & Environment.NewLine & "Old File Name: {1}", "Success", old_fname.ToString)
TextBox2.Text = String.Format("Deleted File Complete, status {0}" & Environment.NewLine & "Old File Name: {1}", "Success", old_fname.ToString) & Environment.NewLine & TextBox2.Text
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
If refreshDirectoryList Then
ListDirectoryDetails()
End If
End Sub
Public Sub RemoveDirectory(ByVal old_directory As String, Optional ByVal confirmRemove As Boolean = True, Optional ByVal refreshDirectoryList As Boolean = True)
If cancelProgress = True Then
Return
End If
Try
If String.IsNullOrEmpty(CStr(old_directory & "").Trim()) Then Return
If confirmRemove Then
Select Case MsgBox("Confirm delete: " & old_directory, MsgBoxStyle.Question Or MsgBoxStyle.YesNoCancel Or MsgBoxStyle.ApplicationModal, "Confirm Delete:")
Case MsgBoxResult.Yes
Exit Select
Case Else
Return
End Select
End If
_fileName = CStr(old_directory & "").Trim() & ""
Dim ftpUploadPath As String = old_directory.ToString.TrimStart("\"c).TrimEnd("\"c) & "\"
If Not old_directory.ToLower.StartsWith(CStr(getFTPPath()).ToLower) Then
ftpUploadPath = CStr(getFTPPath()) & old_directory.ToString.TrimStart("\"c).TrimEnd("\"c) & "\"
End If
If cancelProgress = True Then
Return
End If
Dim status As String = "Success"
If Not DeleteFolder(ftpUploadPath) Then
status = "Failed"
Else
status = "Success"
End If
Console.WriteLine("Removed Directory Complete, status {0}" & Environment.NewLine & "Old Directory Name: {1}", status, old_directory.ToString)
TextBox2.Text = String.Format("Removed Directory Complete, status {0}" & Environment.NewLine & "Old Directory Name: {1}", status, old_directory.ToString) & Environment.NewLine & TextBox2.Text
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
If refreshDirectoryList Then
ListDirectoryDetails()
End If
End Sub
Public Sub RenameFolder(ByVal old_fname As String, ByVal new_fname As String)
Try
If String.IsNullOrEmpty(CStr(old_fname & "").Trim()) Or String.IsNullOrEmpty(CStr(new_fname & "").Trim()) Then Return
If new_fname.Contains("\"c) Then
Return
End If
_fileName = getFTPPath() & CStr(old_fname & "").Trim() & "\"c
Dim ftpUploadPath As String = getFTPPath() & new_fname.ToString.TrimEnd("\"c) & "\"c
If Not String.IsNullOrEmpty(ftpUploadPath) Then
If Directory.Exists(_fileName) Then
If Directory.Exists(ftpUploadPath) Then
Select Case MsgBox("Delete existing folder: " & new_fname & "", MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Confirm Rename")
Case MsgBoxResult.Yes, MsgBoxResult.Ok
Directory.Delete(ftpUploadPath)
End Select
End If
Directory.Move(getFTPPath() & old_fname, getFTPPath() & new_fname)
End If
End If
TextBox2.Text = String.Format("Renamed Folder Complete, status {0}" & Environment.NewLine & "Old Folder Name: {1}, New Folder Name:{2}", "Success", old_fname.ToString, new_fname.ToString) & Environment.NewLine & TextBox2.Text
Catch ex As Exception
TextBox2.Text = String.Format("Message: Renaming Folder - Error: {0}" & Environment.NewLine & "Old Folder Name: {1}, New Folder Name:{2}", ex.Message.ToString(), old_fname.ToString, new_fname.ToString) & Environment.NewLine & TextBox2.Text
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
ListDirectoryDetails()
End Sub
Public Sub MakeNewFolder(ByVal new_fname As String)
Try
If String.IsNullOrEmpty(CStr(new_fname & "").Trim()) Then Return
If new_fname.Contains("\"c) Then
Return
End If
For Each item As FileSystemItemVB In fsItems
If item.IsFolder Then
If item.FileName.ToString.ToLower = new_fname.ToString.ToLower Then
Exit Sub
End If
End If
Next
_fileName = CStr(new_fname & "").Trim() & ""
Dim ftpUploadPath As String = getFTPPath() & _fileName.ToString
Directory.CreateDirectory(ftpUploadPath)
Console.WriteLine("New Folder Complete, status {0}" & Environment.NewLine & "New Folder Name: {1}", "Success", new_fname.ToString)
TextBox2.Text = String.Format("New Folder Complete, status {0}" & Environment.NewLine & "New Folder Name: {1}", "Success", new_fname.ToString) & Environment.NewLine & TextBox2.Text
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
ListDirectoryDetails()
End Sub
Public Function DownloadFile(ByVal filePath As String, ByVal destinationFilePath As String, Optional ByVal openDirectory As Boolean = False, Optional ByVal openDefaultViewer As Boolean = False, Optional ByVal openPdfEditor As Boolean = False, Optional ByVal overWrite As Boolean = False) As Byte()
Dim bytesDownload() As Byte = Nothing
Try
Dim strComplete As String = "Complete"
cancelProgress = False
Using fs As New IO.MemoryStream(File.ReadAllBytes(filePath))
btnFileDownload.Text = "Cancel Download"
DownloadFileToolStripMenuItem.Text = btnFileDownload.Text
Application.DoEvents()
If cancelProgress Then
strComplete = "CANCELLED"
fs.Flush()
GoTo GOTO_CLOSE
End If
Try
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
Application.DoEvents()
If cancelProgress Then
strComplete = "CANCELLED"
fs.Flush()
GoTo GOTO_CLOSE
End If
fs.Flush()
bytesDownload = fs.ToArray
If File.Exists(destinationFilePath) Then
If overWrite Then
File.WriteAllBytes(destinationFilePath, bytesDownload)
If openDirectory Then
Process.Start(IO.Path.GetDirectoryName(destinationFilePath))
End If
If openDefaultViewer Then
Process.Start(destinationFilePath)
End If
If openPdfEditor Then
frm.OpenFile(destinationFilePath, False)
End If
Else
If SaveAs(destinationFilePath, bytesDownload) Then
If openDirectory Then
Process.Start(IO.Path.GetDirectoryName(destinationFilePath))
End If
If openDefaultViewer Then
Process.Start(destinationFilePath)
End If
If openPdfEditor Then
frm.OpenFile(destinationFilePath, False)
End If
End If
End If
Else
File.WriteAllBytes(destinationFilePath, bytesDownload)
If openDirectory Then
Process.Start(IO.Path.GetDirectoryName(destinationFilePath))
End If
If openDefaultViewer Then
Process.Start(destinationFilePath)
End If
If openPdfEditor Then
frm.OpenFile(destinationFilePath, False)
End If
End If
GOTO_CLOSE:
fs.Close()
fs.Dispose()
End Using
TextBox2.Text = String.Format("Download " & strComplete & ", status {0}", "Success!") & Environment.NewLine & TextBox2.Text
Return bytesDownload
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
Finally
btnFileDownload.Text = "Download File"
DownloadFileToolStripMenuItem.Text = btnFileDownload.Text
cancelProgress = False
ProgressBar1.Visible = False
End Try
Return Nothing
End Function
Public Function SaveAs(ByVal fpath As String, ByVal fileBytes() As Byte) As Boolean
Dim SaveAsFileDialog1 As New frmSaveAs()
Try
If String.IsNullOrEmpty(fpath & "") Then
SaveAsFileDialog1.o.InitialDirectory = frm.ApplicationDataFolder(True, "") & "\" & ""
Else
SaveAsFileDialog1.o.InitialDirectory = System.IO.Path.GetDirectoryName(fpath)
End If
Dim defExt As String = ""
Try
defExt = Path.GetExtension(fpath).ToString.TrimStart("."c) & ""
Catch exExt As Exception
defExt = "pdf"
If frm.debugMode Then Throw exExt Else Err.Clear()
End Try
SaveAsFileDialog1.o.Filter = defExt.ToUpper & "|*." & defExt & "|All files|*.*"
SaveAsFileDialog1.o.FilterIndex = 1
If String.IsNullOrEmpty(fpath & "") Then
SaveAsFileDialog1.o.FileName = ""
SaveAsFileDialog1.FilePath = ""
SaveAsFileDialog1.frmSaveAs_TextFilePath.Text = ""
Else
SaveAsFileDialog1.o.FileName = System.IO.Path.GetFileName(fpath & "")
SaveAsFileDialog1.FilePath = fpath
SaveAsFileDialog1.frmSaveAs_TextFilePath.Text = fpath
End If
SaveAsFileDialog1.o.AutoUpgradeEnabled = True
SaveAsFileDialog1.o.FilterIndex = 0
SaveAsFileDialog1.o.Title = "Save As"
SaveAsFileDialog1.o.ValidateNames = True
SaveAsFileDialog1.o.OverwritePrompt = True
Try
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
Select Case SaveAsFileDialog1.o.ShowDialog(Me)
Case Windows.Forms.DialogResult.OK, Windows.Forms.DialogResult.Yes
If Not String.IsNullOrEmpty(SaveAsFileDialog1.frmSaveAs_TextFilePath.Text) Then
Dim fn As String = SaveAsFileDialog1.frmSaveAs_TextFilePath.Text & ""
File.WriteAllBytes(fn, fileBytes.ToArray)
Return True
End If
End Select
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
Return False
End Function
Public Function modelPopup_DoEvents(ByRef blnDone As MsgBoxResult) As Boolean
Do While blnDone = MsgBoxResult.Ignore
Application.DoEvents()
Loop
Return True
End Function
Public Function DoUntil_Boolean(ByRef blnValue As Boolean, Optional ByVal blnBreakValue As Boolean = False) As Boolean
Do Until blnValue = blnBreakValue
Application.DoEvents()
Loop
Return True
End Function
Private Function modelPopupFrmSaveAs_DoEvents(ByRef frmSaveAsDialog As frmSaveAs) As Boolean
Do While frmSaveAsDialog.dialogResult_1 = Windows.Forms.DialogResult.None
Application.DoEvents()
Loop
Return True
End Function
Public Function DoEvents_Wait(ByVal WaitTimeMilliseconds As Integer) As Boolean
Try
Dim dt As DateTime = DateTime.Now
Dim dtSave As DateTime = DateTime.Now
Do While dt <= dtSave.AddMilliseconds(CInt(WaitTimeMilliseconds + 0))
Application.DoEvents()
dt = DateTime.Now
Loop
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
Return True
End Function
Public Function getFTPPath() As String
Dim ftpPath As String = ""
If Not String.IsNullOrEmpty(txtFTPRoot.Text.ToString.Replace(Environment.NewLine, "") & "") Then
ftpPath = CStr(txtFTPRoot.Text.ToString.Replace(Environment.NewLine, "") & "").ToString.TrimEnd("\"c) & "\"
Else
ftpPath = frm.ApplicationDataFolder(True, "") & "\" & ""
txtFTPRoot.Text = ftpPath
End If
Return ftpPath.ToString.TrimEnd("\"c) & "\"
End Function
Public fsItems As New SortedBindingList(Of FileSystemItemVB)
Public Function getParentFolder() As String
Dim ftpPath As String = ""
ftpPath = getFTPPath()
ftpPath = ftpPath.TrimEnd("\"c)
If String.IsNullOrEmpty(ftpPath) Then
ftpPath = "\"
End If
Dim i As Integer = ftpPath.LastIndexOf("\"c)
If i > 0 Then
ftpPath = ftpPath.Substring(0, i)
Else
ftpPath = "\"
End If
If String.IsNullOrEmpty(ftpPath) Then
ftpPath = "\"
End If
Return ftpPath
End Function
Public Class SortedBindingList(Of T)
Inherits System.ComponentModel.BindingList(Of T)
Private m_bIsSorted As Boolean = False
Private m_SortDirection As System.ComponentModel.ListSortDirection
Private m_SortProperty As System.ComponentModel.PropertyDescriptor
Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides ReadOnly Property IsSortedCore() As Boolean
Get
Return m_bIsSorted
End Get
End Property
Protected Overrides Sub RemoveSortCore()
m_bIsSorted = False
End Sub
Protected Overrides Sub ApplySortCore(ByVal prop As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection)
Dim myitems As List(Of T) = TryCast(Me.Items, List(Of T))
If myitems IsNot Nothing Then
m_SortDirection = direction
m_SortProperty = prop
Dim pc As PropertyComparer(Of T) =
New PropertyComparer(Of T)(prop, direction)
myitems.Sort(pc)
m_bIsSorted = True
Else
m_bIsSorted = False
End If
Me.OnListChanged(New System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.Reset, -1))
End Sub
Protected Overrides ReadOnly Property SortPropertyCore() As System.ComponentModel.PropertyDescriptor
Get
Return m_SortProperty
End Get
End Property
Protected Overrides ReadOnly Property SortDirectionCore() As System.ComponentModel.ListSortDirection
Get
Return m_SortDirection
End Get
End Property
End Class
Public Class PropertyComparer(Of T)
Inherits System.Collections.Generic.Comparer(Of T)
Private _property As System.ComponentModel.PropertyDescriptor
Private _direction As System.ComponentModel.ListSortDirection
Public Sub New(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection)
_property = [property]
_direction = direction
End Sub
#Region "IComparer<T>"
Public Overrides Function Compare(ByVal xWord As T, ByVal yWord As T) As Integer
Dim xValue As Object = GetPropertyValue(xWord, _property.Name)
Dim yValue As Object = GetPropertyValue(yWord, _property.Name)
If _direction = System.ComponentModel.ListSortDirection.Ascending Then
Return CompareAscending(xValue, yValue)
Else
Return CompareDescending(xValue, yValue)
End If
End Function
Public Overloads Function Equals(ByVal xWord As T, ByVal yWord As T) As Boolean
Return xWord.Equals(yWord)
End Function
Public Overloads Function GetHashCode(ByVal obj As T) As Integer
Return obj.GetHashCode()
End Function
#End Region
Private Function CompareAscending(ByVal xValue As Object, ByVal yValue As Object) As Integer
Dim result As Integer
If TypeOf xValue Is IComparable Then
result = (DirectCast(xValue, IComparable)).CompareTo(yValue)
Else
If xValue.Equals(yValue) Then
result = 0
Else
result = xValue.ToString().CompareTo(yValue.ToString())
End If
End If
Return result
End Function
Private Function CompareDescending(ByVal xValue As Object, ByVal yValue As Object) As Integer
Return CompareAscending(xValue, yValue) * -1
End Function
Private Function GetPropertyValue(ByVal value As T, ByVal [property] As String) As Object
Dim propertyInfo1 As System.Reflection.PropertyInfo = value.[GetType]().GetProperty([property])
Return propertyInfo1.GetValue(value, Nothing)
End Function
End Class
Public Sub ListDirectoryDetails()
Try
fsItems.Clear()
fsItems = New SortedBindingList(Of FileSystemItemVB)
DataGridView1.DataSource = fsItems
DataGridView1.Refresh()
Dim strReader As String = ""
Dim line As String = ""
Dim lines As New List(Of String)
Dim files As New List(Of FileSystemItemVB)
Dim folders As New List(Of FileSystemItemVB)
Dim item As FileSystemItemVB
Try
Dim backUp As New FileSystemItemVB(frm, getFTPPath() & "", "File Folder", 1, "owner", "group", "0", DateTime.Now.AddDays(1).Month.ToString, DateTime.Now.AddDays(1).Day.ToString, DateTime.Now.AddDays(1).Year.ToString, "▲ Up")
fsItems.Add(backUp)
For Each line In Directory.GetDirectories(getFTPPath())
Try
If Not lines.Contains(line) Then
lines.Add(line)
Dim fi As New DirectoryInfo(line)
If fi.Exists Then
item = New FileSystemItemVB(frm, getFTPPath() & "", "File Folder", CInt("1"), "owner", "group", "0", fi.LastWriteTime().Month.ToString & "", fi.LastWriteTime().Day.ToString & "", fi.LastWriteTime().Year.ToString & "", GetDirectoryFileName1(line) & "")
folders.Add(item)
fsItems.Add(item)
End If
If Not line Is Nothing Then
strReader &= Environment.NewLine
End If
End If
Catch exReadLines As Exception
If frm.debugMode Then Throw exReadLines Else Err.Clear()
End Try
Next
Catch ex As Exception
Err.Clear()
End Try
Try
For Each line In Directory.GetFiles(getFTPPath())
Try
If Not lines.Contains(line) Then
lines.Add(line)
Dim fi As New FileInfo(line)
If fi.Exists Then
item = New FileSystemItemVB(frm, getFTPPath() & "", fi.Extension.ToString() & "", CInt("1"), "owner", "group", fi.Length.ToString & "", fi.LastWriteTime().Month.ToString & "", fi.LastWriteTime().Day.ToString & "", fi.LastWriteTime().Year.ToString & "", Path.GetFileName(line) & "")
If fi.Extension.ToString().TrimStart("."c) = "pdf" Then
If listSelectedPaths.Keys.Contains(line) Then
Try
If listSelectedPaths(line) Is Nothing Or String.IsNullOrEmpty(listSelectedPaths(line)) Or listSelectedPaths(line) = "" Then
Dim r As PdfReader = New PdfReader(frm.UnlockSecurePDF(File.ReadAllBytes(fi.FullName)))
item.PageSelection = "1-" & r.NumberOfPages.ToString() & ""
r.Close()
r.Dispose()
Else
item.PageSelection = listSelectedPaths(line)
End If
Catch ex As Exception
Err.Clear()
End Try
Else
Try
Dim r As PdfReader = New PdfReader(frm.UnlockSecurePDF(File.ReadAllBytes(fi.FullName)))
item.PageSelection = "1-" & r.NumberOfPages.ToString() & ""
r.Close()
r.Dispose()
Catch ex As Exception
Err.Clear()
End Try
End If
files.Add(item)
fsItems.Add(item)
End If
End If
If Not line Is Nothing Then
strReader &= Environment.NewLine
End If
End If
Catch exReadLines As Exception
If frm.debugMode Then Throw exReadLines Else Err.Clear()
End Try
Next
Catch ex As Exception
Err.Clear()
End Try
Try
For Each line In listSelectedPaths.Keys.ToArray()
Try
If Not lines.Contains(line) Then
lines.Add(line)
Dim fi As New FileInfo(line)
If fi.Exists Then
item = New FileSystemItemVB(frm, Path.GetDirectoryName(line) & "", fi.Extension.ToString() & "", CInt("1"), "owner", "group", fi.Length.ToString & "", fi.LastWriteTime().Month.ToString & "", fi.LastWriteTime().Day.ToString & "", fi.LastWriteTime().Year.ToString & "", Path.GetFileName(line) & "")
If fi.Extension.ToString().TrimStart("."c) = "pdf" Then
Try
If listSelectedPaths(line) Is Nothing Or String.IsNullOrEmpty(listSelectedPaths(line)) Or listSelectedPaths(line) = "" Then
Dim r As PdfReader = New PdfReader(frm.UnlockSecurePDF(File.ReadAllBytes(fi.FullName)))
item.PageSelection = "1-" & r.NumberOfPages.ToString() & ""
r.Close()
r.Dispose()
Else
item.PageSelection = listSelectedPaths(line)
End If
Catch ex As Exception
Err.Clear()
End Try
files.Add(item)
fsItems.Add(item)
End If
End If
If Not line Is Nothing Then
strReader &= Environment.NewLine
End If
End If
Catch exReadLines As Exception
If frm.debugMode Then Throw exReadLines Else Err.Clear()
End Try
Next
Catch ex As Exception
Err.Clear()
End Try
If folders.Count > 0 Then
End If
If files.Count > 0 Then
End If
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = fsItems
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
If DataGridView1.Columns.Contains("FullPath") Then
DataGridView1.Columns.Remove("FullPath")
End If
If DataGridView1.Columns(1).CellType Is GetType(DataGridViewImageCell) Then
Try
DataGridView1.AutoSize = False
DataGridView1.AllowUserToResizeColumns = True
DataGridView1.AllowUserToOrderColumns = True
Dim colIcon As DataGridViewImageColumn = DirectCast(DataGridView1.Columns(1), DataGridViewImageColumn)
colIcon.Name = "FileIcon"
colIcon.HeaderText = "ICO"
colIcon.ImageLayout = DataGridViewImageCellLayout.Zoom
colIcon.DefaultCellStyle.SelectionBackColor = Color.White
colIcon.DefaultCellStyle.Padding = New System.Windows.Forms.Padding(2, 2, 1, 2)
colIcon.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
colIcon.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
Try
colIcon.Width = 18
colIcon.DividerWidth = 0
Catch ex2 As Exception
Err.Clear()
End Try
DataGridView1.BorderStyle = BorderStyle.None
DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Catch ex As Exception
If frm.debugMode Then Throw ex Else Err.Clear()
End Try
End If
Dim chk As New DataGridViewCheckBoxColumn()
chk.HeaderText = ""
chk.Name = "chkSelected"
chk.Width = 50
chk.ReadOnly = False
chk.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
If Not DataGridView1.Columns.Contains("chkSelected") Then
DataGridView1.Columns.Insert(0, chk)
End If
Dim btn As New DataGridViewButtonColumn()
btn.HeaderText = ""
btn.Name = "btnUp"
btn.Width = 27
btn.ReadOnly = False
btn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
btn.Text = "▲"
btn.UseColumnTextForButtonValue = True
If Not DataGridView1.Columns.Contains("btnUp") Then
DataGridView1.Columns.Insert(7, btn)
Else
DataGridView1.Columns.Remove(DataGridView1.Columns("btnUp"))
DataGridView1.Columns.Insert(8, btn)
End If
btn = New DataGridViewButtonColumn()
btn.HeaderText = ""
btn.Name = "btnDown"
btn.Width = 27
btn.ReadOnly = False
btn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
btn.Text = "▼"
btn.UseColumnTextForButtonValue = True
If Not DataGridView1.Columns.Contains("btnDown") Then
DataGridView1.Columns.Insert(8, btn)
Else
DataGridView1.Columns.Remove(DataGridView1.Columns("btnDown"))
DataGridView1.Columns.Insert(8, btn)
End If
DataGridView1.Columns(0).ReadOnly = False
For c As Integer = 1 To DataGridView1.Columns.Count - 1
If Not (DataGridView1.Rows(0).Cells(c).GetType()) Is GetType(DataGridViewButtonCell) Then
DataGridView1.Columns(c).ReadOnly = True
End If
Next
For r As Integer = 0 To DataGridView1.RowCount - 1
DirectCast(DataGridView1.Rows(r).Cells(0), DataGridViewCheckBoxCell).ReadOnly = False
If listSelectedPaths.Keys.Contains(CStr(getFTPPath() & DirectCast(DataGridView1.Rows(r).Cells("FileName"), DataGridViewCell).Value.ToString).ToString()) Then
DirectCast(DataGridView1.Rows(r).Cells(0), DataGridViewCheckBoxCell).Value = True
ElseIf listSelectedPaths.Keys.Contains(fsItems(r).FullPath.ToString) Then
DirectCast(DataGridView1.Rows(r).Cells(0), DataGridViewCheckBoxCell).Value = True
Else
DirectCast(DataGridView1.Rows(r).Cells(0), DataGridViewCheckBoxCell).Value = False
End If
Next
Console.WriteLine(strReader)
Console.WriteLine("Directory List Complete, status {0}", "Success")
TextBox1.Text &= Environment.NewLine & strReader
Catch exMain As Exception
MsgBox(exMain.Message)
If frm.debugMode Then Throw exMain Else Err.Clear()
Finally
GroupBoxFolder1.Visible = False
GroupBoxFile1.Visible = False
If Not _fileBytes Is Nothing Then
If _fileBytes.Length > 0 Then
If Not String.IsNullOrEmpty(_fileName & "") Then
GroupBoxFile1.Visible = True
End If
End If
End If
If GroupBoxFile1.Visible = False Then
GroupBoxFile1.Visible = True
txtFolderName.Text = ""
If txtFolderName.Text.ToString.Contains("\"c) Then
txtFolderName.Text = txtFolderName.Text.Substring(txtFolderName.Text.ToString.LastIndexOf("\"c), txtFolderName.Text.Length - txtFolderName.Text.ToString.LastIndexOf("\"c))
txtFolderName.Text = txtFolderName.Text.TrimStart("\"c).TrimEnd("\"c)
End If
End If
If DataGridView1.RowCount > 1 Then
DataGridView1.Focus()
DataGridView1.Select()
End If
Dim dcc As Integer = DataGridView1.Columns.Count
For c As Integer = 9 To dcc - 1
Dim strPages As String = DataGridView1.Columns(7).HeaderText
Dim btnCell As New DataGridViewButtonCell
If Not (DataGridView1.Rows(0).Cells(9).GetType()) Is GetType(DataGridViewButtonCell) Then
strPages = strPages
DataGridView1.Columns.Remove(DataGridView1.Columns(9))
End If
Next
dcc = DataGridView1.Columns.Count
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
For c As Integer = 0 To dcc - 1
DataGridView1.Columns(c).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
Next
DataGridView1.Columns(0).Width = 24
DataGridView1.Columns(0).HeaderText = ""
DataGridView1.Columns(1).Width = 36
DataGridView1.Columns(1).HeaderText = ""
DataGridView1.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(2).MinimumWidth = 100
DataGridView1.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(3).MinimumWidth = 80
DataGridView1.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(4).MinimumWidth = 80
DataGridView1.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(5).MinimumWidth = 80
DataGridView1.Columns(6).Width = 100
DataGridView1.Columns(7).Width = 27
DataGridView1.Columns(7).HeaderText = "▲"
DataGridView1.Columns(7).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
DataGridView1.Columns(8).Width = 27
DataGridView1.Columns(8).HeaderText = "▼"
DataGridView1.Columns(8).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
DataGridView1.AutoResizeColumn(2)
DataGridView1.AutoResizeColumn(7)
DataGridView1.AutoResizeColumn(8)
End Try
End Sub
Public Sub ListFileDetails(ByVal lst As List(Of String))
Try
fsItems.Clear()
fsItems = New SortedBindingList(Of FileSystemItemVB)
Dim strReader As String = ""
Dim line As String = ""
Dim lines As New List(Of String)
Dim files As New List(Of FileSystemItemVB)
Dim folders As New List(Of FileSystemItemVB)
Dim item As FileSystemItemVB
Try
Dim backUp As New FileSystemItemVB(frm, getFTPPath() & "", "File Folder", 1, "owner", "group", "0", DateTime.Now.AddDays(1).Month.ToString, DateTime.Now.AddDays(1).Day.ToString, DateTime.Now.AddDays(1).Year.ToString, "▲ Up")
fsItems.Add(backUp)
For Each line In lst.ToArray()
If Directory.Exists(line & "") Then
Try
For Each lineFile As String In Directory.GetFiles(line)
Try
If Not lines.Contains(lineFile) Then
lines.Add(lineFile)
Dim fi As New FileInfo(lineFile)
If fi.Exists Then
item = New FileSystemItemVB(frm, Path.GetDirectoryName(lineFile), fi.Extension.ToString() & "", CInt("1"), "owner", "group", fi.Length.ToString & "", fi.LastWriteTime().Month.ToString & "", fi.LastWriteTime().Day.ToString & "", fi.LastWriteTime().Year.ToString & "", Path.GetFileName(lineFile) & "")
files.Add(item)
fsItems.Add(item)
End If
If Not line Is Nothing Then
strReader &= Environment.NewLine
End If
End If
Catch exReadLines As Exception
If frm.debugMode Then Throw exReadLines Else Err.Clear()
End Try
Next
Catch ex As Exception
Err.Clear()
End Try
ElseIf File.Exists(line & "") Then
Try
If Not lines.Contains(line) Then
lines.Add(line)
Dim fi As New FileInfo(line)
If fi.Exists Then
item = New FileSystemItemVB(frm, Path.GetDirectoryName(line), fi.Extension.ToString() & "", CInt("1"), "owner", "group", fi.Length.ToString & "", fi.LastWriteTime().Month.ToString & "", fi.LastWriteTime().Day.ToString & "", fi.LastWriteTime().Year.ToString & "", Path.GetFileName(line) & "")
files.Add(item)
fsItems.Add(item)
End If
If Not line Is Nothing Then
strReader &= Environment.NewLine
End If
End If
Catch exReadLines As Exception
If frm.debugMode Then Throw exReadLines Else Err.Clear()
End Try
End If
Next
Catch exTRy As Exception
End Try
If folders.Count > 0 Then
End If
If files.Count > 0 Then
End If
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = fsItems
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
For c As Integer = 6 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.RemoveAt(6)
Next
If DataGridView1.Columns(1).CellType Is GetType(DataGridViewImageCell) Then
Try
DataGridView1.AutoSize = False
DataGridView1.AllowUserToResizeColumns = True
DataGridView1.AllowUserToOrderColumns = True
Dim colIcon As DataGridViewImageColumn = DirectCast(DataGridView1.Columns(1), DataGridViewImageColumn)
colIcon.Name = "FileIcon"
colIcon.HeaderText = ""
colIcon.ImageLayout = DataGridViewImageCellLayout.Zoom
colIcon.DefaultCellStyle.SelectionBackColor = Color.White
colIcon.DefaultCellStyle.Padding = New System.Windows.Forms.Padding(2, 2, 1, 2)
colIcon.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter