MainWindow.xaml.cs 14 KB

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