MainWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using Compdfkit_Tools.PDFControl;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.IO;
  16. using System.ComponentModel;
  17. using System.Runtime.CompilerServices;
  18. using Dragablz;
  19. using Compdfkit_Tools.Helper;
  20. using ComPDFKitViewer;
  21. using ComPDFKit.PDFDocument;
  22. namespace PDFViewer
  23. {
  24. /// <summary>
  25. /// Interaction logic for MainWindow.xaml
  26. /// </summary>
  27. public partial class MainWindow : Window
  28. {
  29. #region Property
  30. private PDFViewControl passwordViewer;
  31. private PDFViewControl pdfViewControl = new PDFViewControl();
  32. private string[] oldAndNewFilePath;
  33. #endregion
  34. public MainWindow()
  35. {
  36. InitializeComponent();
  37. Loaded += MainWindow_Loaded; ;
  38. }
  39. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  40. {
  41. PasswordUI.Closed -= PasswordUI_Closed;
  42. PasswordUI.Canceled -= PasswordUI_Canceled;
  43. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  44. PasswordUI.Confirmed += PasswordUI_Confirmed;
  45. PasswordUI.Canceled += PasswordUI_Canceled;
  46. PasswordUI.Closed += PasswordUI_Closed;
  47. if (App.DefaultPDFLoaded == false)
  48. {
  49. LoadDefaultDocument();
  50. App.DefaultPDFLoaded = true;
  51. }
  52. }
  53. private void LoadDefaultDocument()
  54. {
  55. string defaultFilePath = "PDF32000_2008.pdf";
  56. TabControlLoadDocument(defaultFilePath);
  57. }
  58. private void TabControlLoadDocument(string filePath)
  59. {
  60. if (App.OpenedFilePathList.Contains(filePath))
  61. {
  62. for (int i = 0; i < App.Current.Windows.Count; i++)
  63. {
  64. MainWindow win = App.Current.Windows[i] as MainWindow;
  65. if (win != null)
  66. {
  67. var items = win.TabControl.Items;
  68. foreach (TabItemExt item in items)
  69. {
  70. if (item.Tag.ToString().ToLower() == filePath.ToLower())
  71. {
  72. win.Activate();
  73. item.IsSelected = true;
  74. return;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. TabItemExt tabItem = new TabItemExt();
  81. MainPage viewPage = new MainPage();
  82. viewPage.CheckExistBeforeOpenFileEvent -= ViewPage_CheckExistBeforeOpenFileEvent;
  83. viewPage.FileChangeEvent -= ViewPage_FileChangeEvent;
  84. viewPage.CheckExistBeforeOpenFileEvent += ViewPage_CheckExistBeforeOpenFileEvent;
  85. viewPage.FileChangeEvent += ViewPage_FileChangeEvent;
  86. passwordViewer = new PDFViewControl();
  87. passwordViewer.InitDocument(filePath);
  88. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  89. {
  90. CPDFViewer tempViewer = passwordViewer.PDFViewTool.GetCPDFViewer();
  91. CPDFDocument pdfDoc = tempViewer?.GetDocument();
  92. if (pdfDoc == null)
  93. {
  94. MessageBox.Show("Open File Failed");
  95. return;
  96. }
  97. if (pdfDoc.IsLocked)
  98. {
  99. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  100. PasswordUI.ClearPassword();
  101. PopupBorder.Visibility = Visibility.Visible;
  102. PasswordUI.Visibility = Visibility.Visible;
  103. }
  104. else
  105. {
  106. viewPage.InitWithFilePath(filePath);
  107. tabItem.Content = viewPage;
  108. tabItem.IsSelected = true;
  109. tabItem.FileName = Path.GetFileName(filePath);
  110. tabItem.Tag = filePath;
  111. App.OpenedFilePathList.Add(filePath);
  112. TabControl.Items.Add(tabItem);
  113. }
  114. }
  115. }
  116. private void PasswordUI_Closed(object sender, EventArgs e)
  117. {
  118. PopupBorder.Visibility = Visibility.Collapsed;
  119. PasswordUI.Visibility = Visibility.Collapsed;
  120. }
  121. private void PasswordUI_Canceled(object sender, EventArgs e)
  122. {
  123. PopupBorder.Visibility = Visibility.Collapsed;
  124. PasswordUI.Visibility = Visibility.Collapsed;
  125. }
  126. private void PasswordUI_Confirmed(object sender, string e)
  127. {
  128. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  129. {
  130. CPDFViewer tempViewer=passwordViewer.PDFViewTool.GetCPDFViewer();
  131. CPDFDocument pdfDoc=tempViewer?.GetDocument();
  132. if(pdfDoc==null)
  133. {
  134. return;
  135. }
  136. pdfDoc.UnlockWithPassword(e);
  137. if (pdfDoc.IsLocked == false)
  138. {
  139. PasswordUI.SetShowError("", Visibility.Collapsed);
  140. PasswordUI.ClearPassword();
  141. PasswordUI.Visibility = Visibility.Collapsed;
  142. PopupBorder.Visibility = Visibility.Collapsed;
  143. pdfViewControl = passwordViewer;
  144. string filePath = pdfDoc.FilePath;
  145. TabItemExt tabItem = new TabItemExt();
  146. MainPage viewPage = new MainPage();
  147. tabItem.Content = viewPage;
  148. tabItem.IsSelected = true;
  149. tabItem.FileName = Path.GetFileName(filePath);
  150. tabItem.Tag = filePath;
  151. viewPage.SetPDFViewer(pdfViewControl);
  152. App.OpenedFilePathList.Add(filePath);
  153. TabControl.Items.Add(tabItem);
  154. }
  155. else
  156. {
  157. PasswordUI.SetShowError("error", Visibility.Visible);
  158. }
  159. }
  160. }
  161. private void ViewPage_FileChangeEvent(object sender, EventArgs e)
  162. {
  163. for (int i = 0; i < App.Current.Windows.Count; i++)
  164. {
  165. MainWindow win = App.Current.Windows[i] as MainWindow;
  166. if (win != null)
  167. {
  168. var items = win.TabControl.Items;
  169. foreach (TabItemExt item in items)
  170. {
  171. if (item.Tag.ToString().ToLower() == oldAndNewFilePath[1].ToLower())
  172. {
  173. item.IsSelected = true;
  174. item.FileName = Path.GetFileName(oldAndNewFilePath[0]);
  175. item.IsSelected = true;
  176. item.Tag = oldAndNewFilePath[0];
  177. }
  178. }
  179. }
  180. }
  181. for (int i = 0; i < App.OpenedFilePathList.Count; i++)
  182. {
  183. if (oldAndNewFilePath[1].ToLower() == App.OpenedFilePathList[i].ToLower())
  184. {
  185. App.OpenedFilePathList[i] = oldAndNewFilePath[0];
  186. break;
  187. }
  188. }
  189. }
  190. private bool ViewPage_CheckExistBeforeOpenFileEvent(string[] arg)
  191. {
  192. if (App.OpenedFilePathList.Contains(arg[0]))
  193. {
  194. for (int i = 0; i < App.Current.Windows.Count; i++)
  195. {
  196. MainWindow win = App.Current.Windows[i] as MainWindow;
  197. if (win != null)
  198. {
  199. var items = win.TabControl.Items;
  200. foreach (TabItemExt item in items)
  201. {
  202. if (item.Tag.ToString().ToLower() == arg[0].ToLower())
  203. {
  204. win.Activate();
  205. item.IsSelected = true;
  206. return true;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. oldAndNewFilePath = arg;
  213. return false;
  214. }
  215. public class TabItemExt : TabItem, INotifyPropertyChanged
  216. {
  217. public event PropertyChangedEventHandler PropertyChanged;
  218. private string _fileName;
  219. public string FileName
  220. {
  221. get
  222. {
  223. return _fileName;
  224. }
  225. set
  226. {
  227. if (_fileName != value)
  228. {
  229. _fileName = value;
  230. OnPropertyChanged();
  231. }
  232. }
  233. }
  234. private Visibility _fileChanged = Visibility.Hidden;
  235. public Visibility FileChanged
  236. {
  237. get
  238. {
  239. return _fileChanged;
  240. }
  241. set
  242. {
  243. if (_fileChanged != value)
  244. {
  245. _fileChanged = value;
  246. OnPropertyChanged();
  247. }
  248. }
  249. }
  250. public void OnPropertyChanged([CallerMemberName] string name = null)
  251. {
  252. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  253. }
  254. }
  255. private DragablzItem FindParentDragablzItem(DependencyObject element)
  256. {
  257. while (element != null && !(element is DragablzItem))
  258. {
  259. element = VisualTreeHelper.GetParent(element);
  260. }
  261. return element as DragablzItem;
  262. }
  263. private TabControl FindParentTabControl(DependencyObject element)
  264. {
  265. while (element != null && !(element is TabControl))
  266. {
  267. element = VisualTreeHelper.GetParent(element);
  268. }
  269. return element as TabControl;
  270. }
  271. private void CloseFileButton_Click(object sender, RoutedEventArgs e)
  272. {
  273. var button = sender as Button;
  274. var dragablzItem = FindParentDragablzItem(button);
  275. var tabControl = FindParentTabControl(dragablzItem);
  276. MainPage mainPage = (dragablzItem.Content as TabItemExt).Content as MainPage;
  277. if (mainPage.CanSave)
  278. {
  279. string fileName = (dragablzItem.Content as TabItemExt).FileName;
  280. var message = string.Format("Do you want to save the changes in {0}?", fileName);
  281. var result = MessageBox.Show(message, "Alert", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  282. if (result == MessageBoxResult.Yes)
  283. {
  284. mainPage.SaveFile();
  285. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  286. tabControl.Items.Remove(dragablzItem.Content);
  287. }
  288. else if (result == MessageBoxResult.No)
  289. {
  290. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  291. tabControl.Items.Remove(dragablzItem.Content);
  292. }
  293. else
  294. {
  295. }
  296. }
  297. else
  298. {
  299. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  300. tabControl.Items.Remove(dragablzItem.Content);
  301. }
  302. if (tabControl.Items.Count == 0)
  303. {
  304. SystemCommands.CloseWindow(this);
  305. }
  306. }
  307. private void DefaultAddButton_Click(object sender, RoutedEventArgs e)
  308. {
  309. string filePath = CommonHelper.GetExistedPathOrEmpty();
  310. if (filePath != string.Empty)
  311. {
  312. TabControlLoadDocument(filePath);
  313. }
  314. }
  315. private void MinimizeButton_Click(object sender, RoutedEventArgs e)
  316. {
  317. SystemCommands.MinimizeWindow(this);
  318. }
  319. private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
  320. {
  321. if (this.WindowState == WindowState.Maximized)
  322. {
  323. SystemCommands.RestoreWindow(this);
  324. }
  325. else
  326. {
  327. SystemCommands.MaximizeWindow(this);
  328. }
  329. }
  330. private void CloseButton_Click(object sender, RoutedEventArgs e)
  331. {
  332. int count = TabControl.Items.Count;
  333. while (count > 0)
  334. {
  335. TabItemExt item = TabControl.Items[0] as TabItemExt;
  336. MainPage mainPage = item.Content as MainPage;
  337. if (mainPage.CanSave)
  338. {
  339. string fileName = item.FileName;
  340. var message = string.Format("Do you want to save the changes in {0}?", fileName);
  341. var result = MessageBox.Show(message, "Alert", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  342. if (result == MessageBoxResult.Yes)
  343. {
  344. mainPage.SaveFile();
  345. App.OpenedFilePathList.Remove(item.Tag.ToString());
  346. TabControl.Items.Remove(item);
  347. count--;
  348. }
  349. else if (result == MessageBoxResult.No)
  350. {
  351. App.OpenedFilePathList.Remove(item.Tag.ToString());
  352. TabControl.Items.Remove(item);
  353. count--;
  354. }
  355. else
  356. {
  357. break;
  358. }
  359. }
  360. else
  361. {
  362. App.OpenedFilePathList.Remove(item.Tag.ToString());
  363. TabControl.Items.Remove(item);
  364. count--;
  365. }
  366. }
  367. if (count == 0)
  368. {
  369. SystemCommands.CloseWindow(this);
  370. }
  371. }
  372. }
  373. }