MainWindow.xaml.cs 14 KB

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