MainWindow.xaml.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.IO;
  7. using System.ComponentModel;
  8. using System.Runtime.CompilerServices;
  9. using Dragablz;
  10. using ComPDFKit.Controls.Helper;
  11. using ComPDFKitViewer;
  12. using ComPDFKit.PDFDocument;
  13. using ComPDFKit.Controls.Data;
  14. using ComPDFKit.Controls.Common;
  15. using System.Reflection;
  16. using ComPDFKit.Controls.PDFControl;
  17. namespace PDFViewer
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window, INotifyPropertyChanged
  23. {
  24. #region Property
  25. private PDFViewControl passwordViewer;
  26. private string[] oldAndNewFilePath;
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. public string AppInfo
  29. {
  30. get { return Assembly.GetExecutingAssembly().GetName().Name + " " + string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3)); }
  31. }
  32. #endregion
  33. public MainWindow()
  34. {
  35. InitializeComponent();
  36. Loaded += MainWindow_Loaded;
  37. DataContext = this;
  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. HomePageControl.OpenFileEvent -= OpenFileEventHandler;
  48. HomePageControl.OpenFileEvent += OpenFileEventHandler;
  49. TabControl.SelectionChanged -= TabControl_SelectionChanged;
  50. TabControl.SelectionChanged += TabControl_SelectionChanged;
  51. FirstLoadFile();
  52. CPDFAnnotationData.Author = Properties.Settings.Default.DocumentAuthor;
  53. }
  54. private void FirstLoadFile()
  55. {
  56. if (Properties.Settings.Default.IsLoadLastFileNeeded)
  57. {
  58. if (LoadLastOpenedDocuments())
  59. {
  60. TabControl.SelectedIndex = Properties.Settings.Default.LastSelectedFileIndex;
  61. if (TabControl.SelectedIndex == -1)
  62. {
  63. HomePageButton.IsToggled = true;
  64. }
  65. Properties.Settings.Default.IsLoadLastFileNeeded = false;
  66. Properties.Settings.Default.Save();
  67. }
  68. else
  69. {
  70. HomePageButton.IsToggled = true;
  71. }
  72. }
  73. HomePageButton.IsToggled = true;
  74. }
  75. private bool LoadLastOpenedDocuments()
  76. {
  77. if (Properties.Settings.Default.LastOpenedFiles != null && Properties.Settings.Default.LastOpenedFiles.Count > 0)
  78. {
  79. foreach (var filePath in Properties.Settings.Default.LastOpenedFiles)
  80. {
  81. TabControlLoadDocument(filePath);
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  88. {
  89. var tabablz = sender as Dragablz.TabablzControl;
  90. if (tabablz.SelectedIndex != -1)
  91. {
  92. HomePageButton.IsToggled = false;
  93. }
  94. }
  95. private void OpenFileEventHandler(object sender, OpenFileEventArgs args)
  96. {
  97. if (args.OperationType == FileOperationType.OpenFileDirectly)
  98. {
  99. TabControlLoadDocument(args.FilePath, args.FeatureName);
  100. }
  101. else if (args.OperationType == FileOperationType.CreateNewFile)
  102. {
  103. bool confirmCreate = false;
  104. BlankPageSetting blankPageSetting = new BlankPageSetting();
  105. CreateBlankPageSettingDialog createBlankPageSettingDialog = new CreateBlankPageSettingDialog()
  106. {
  107. Owner = this
  108. };
  109. createBlankPageSettingDialog.CreateBlankPage += (o, setting) =>
  110. {
  111. blankPageSetting = setting;
  112. confirmCreate = true;
  113. };
  114. createBlankPageSettingDialog.ShowDialog();
  115. if (!confirmCreate)
  116. {
  117. return;
  118. }
  119. TabItemExt tabItem = new TabItemExt();
  120. MainPage viewPage = new MainPage();
  121. CPDFDocument document = CPDFDocument.CreateDocument();
  122. document.SetInfo(new CPDFInfo
  123. {
  124. Author = Properties.Settings.Default.DocumentAuthor,
  125. Creator = Assembly.GetExecutingAssembly().GetName().Name,
  126. CreationDate = DateTime.Now.ToString(),
  127. Subject = "Document",
  128. Producer = Assembly.GetExecutingAssembly().GetName().Name,
  129. Keywords = "Document",
  130. Version = string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3))
  131. });
  132. document.InsertPage(0, (float)(blankPageSetting.Size.Width * 7.2 / 2.54), (float)(blankPageSetting.Size.Height * 7.2 / 2.54), "");
  133. if (blankPageSetting.Orientation == Orientation.Horizontal)
  134. {
  135. document.RotatePage(0, 1);
  136. }
  137. viewPage.CheckExistBeforeOpenFileEvent -= ViewPage_CheckExistBeforeOpenFileEvent;
  138. viewPage.FileChangeEvent -= ViewPage_FileChangeEvent;
  139. viewPage.AfterSaveAsFileEvent -= ViewPage_AfterSaveAsFileEvent;
  140. viewPage.CheckExistBeforeOpenFileEvent += ViewPage_CheckExistBeforeOpenFileEvent;
  141. viewPage.FileChangeEvent += ViewPage_FileChangeEvent;
  142. viewPage.AfterSaveAsFileEvent += ViewPage_AfterSaveAsFileEvent;
  143. viewPage.InitWithDocument(document);
  144. tabItem.Content = viewPage;
  145. tabItem.IsSelected = true;
  146. tabItem.FileName = "Blank Page.pdf";
  147. tabItem.Tag = "Blank Page.pdf";
  148. TabControl.Items.Add(tabItem);
  149. viewPage.CanSave = true;
  150. }
  151. else
  152. {
  153. string filePath = CommonHelper.GetExistedPathOrEmpty();
  154. if (filePath != string.Empty)
  155. {
  156. TabControlLoadDocument(filePath);
  157. }
  158. }
  159. }
  160. private void ViewPage_AfterSaveAsFileEvent(object sender, string e)
  161. {
  162. if (sender is MainPage mainPage)
  163. {
  164. var tabItem = (from object t in TabControl.Items select t as TabItemExt).FirstOrDefault(item => Equals(item.Content, mainPage));
  165. if (tabItem != null)
  166. {
  167. tabItem.FileName = Path.GetFileName(e);
  168. tabItem.Tag = e;
  169. }
  170. mainPage.GetPDFViewControl().GetCPDFViewer().CloseDocument();
  171. mainPage.GetPDFViewControl().InitDocument(e);
  172. App.OpenedFilePathList.Add(e);
  173. }
  174. }
  175. private void TabControlLoadDocument(string filePath, string featureName = "")
  176. {
  177. if (App.OpenedFilePathList.Contains(filePath))
  178. {
  179. for (int i = 0; i < App.Current.Windows.Count; i++)
  180. {
  181. MainWindow win = App.Current.Windows[i] as MainWindow;
  182. if (win != null)
  183. {
  184. var items = win.TabControl.Items;
  185. foreach (TabItemExt item in items)
  186. {
  187. if (item.Tag.ToString().ToLower() == filePath.ToLower())
  188. {
  189. win.Activate();
  190. item.IsSelected = true;
  191. (item.Content as MainPage).SetFeatureMode(featureName);
  192. return;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. TabItemExt tabItem = new TabItemExt();
  199. MainPage viewPage = new MainPage();
  200. viewPage.CheckExistBeforeOpenFileEvent -= ViewPage_CheckExistBeforeOpenFileEvent;
  201. viewPage.FileChangeEvent -= ViewPage_FileChangeEvent;
  202. viewPage.AfterSaveAsFileEvent -= ViewPage_AfterSaveAsFileEvent;
  203. viewPage.CheckExistBeforeOpenFileEvent += ViewPage_CheckExistBeforeOpenFileEvent;
  204. viewPage.FileChangeEvent += ViewPage_FileChangeEvent;
  205. viewPage.AfterSaveAsFileEvent += ViewPage_AfterSaveAsFileEvent;
  206. passwordViewer = new PDFViewControl();
  207. passwordViewer.InitDocument(filePath);
  208. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  209. {
  210. CPDFViewer tempViewer = passwordViewer.PDFViewTool.GetCPDFViewer();
  211. CPDFDocument pdfDoc = tempViewer?.GetDocument();
  212. if (pdfDoc == null)
  213. {
  214. MessageBox.Show("Open File Failed");
  215. return;
  216. }
  217. if (pdfDoc.IsLocked)
  218. {
  219. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  220. PasswordUI.ClearPassword();
  221. PopupBorder.Visibility = Visibility.Visible;
  222. PasswordUI.Visibility = Visibility.Visible;
  223. }
  224. else
  225. {
  226. viewPage.InitWithFilePath(filePath);
  227. tabItem.Content = viewPage;
  228. tabItem.IsSelected = true;
  229. tabItem.FileName = Path.GetFileName(filePath);
  230. tabItem.Tag = filePath;
  231. App.OpenedFilePathList.Add(filePath);
  232. TabControl.Items.Add(tabItem);
  233. viewPage.Loaded += (sender, e) =>
  234. {
  235. viewPage.SetFeatureMode(featureName);
  236. };
  237. }
  238. }
  239. }
  240. private void PasswordUI_Closed(object sender, EventArgs e)
  241. {
  242. PopupBorder.Visibility = Visibility.Collapsed;
  243. PasswordUI.Visibility = Visibility.Collapsed;
  244. }
  245. private void PasswordUI_Canceled(object sender, EventArgs e)
  246. {
  247. PopupBorder.Visibility = Visibility.Collapsed;
  248. PasswordUI.Visibility = Visibility.Collapsed;
  249. }
  250. private void PasswordUI_Confirmed(object sender, string e)
  251. {
  252. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  253. {
  254. CPDFViewer tempViewer=passwordViewer.PDFViewTool.GetCPDFViewer();
  255. CPDFDocument pdfDoc=tempViewer?.GetDocument();
  256. if(pdfDoc==null)
  257. {
  258. return;
  259. }
  260. pdfDoc.UnlockWithPassword(e);
  261. if (pdfDoc.IsLocked == false)
  262. {
  263. PasswordUI.SetShowError("", Visibility.Collapsed);
  264. PasswordUI.ClearPassword();
  265. PasswordUI.Visibility = Visibility.Collapsed;
  266. PopupBorder.Visibility = Visibility.Collapsed;
  267. string filePath = pdfDoc.FilePath;
  268. TabItemExt tabItem = new TabItemExt();
  269. MainPage viewPage = new MainPage();
  270. tabItem.Content = viewPage;
  271. tabItem.IsSelected = true;
  272. tabItem.FileName = Path.GetFileName(filePath);
  273. tabItem.Tag = filePath;
  274. passwordViewer.Password = e;
  275. viewPage.SetPDFViewer(passwordViewer);
  276. App.OpenedFilePathList.Add(filePath);
  277. TabControl.Items.Add(tabItem);
  278. }
  279. else
  280. {
  281. PasswordUI.SetShowError("error", Visibility.Visible);
  282. }
  283. }
  284. }
  285. private void ViewPage_FileChangeEvent(object sender, EventArgs e)
  286. {
  287. for (int i = 0; i < App.Current.Windows.Count; i++)
  288. {
  289. MainWindow win = App.Current.Windows[i] as MainWindow;
  290. if (win != null)
  291. {
  292. var items = win.TabControl.Items;
  293. foreach (TabItemExt item in items)
  294. {
  295. if (item.Tag.ToString().ToLower() == oldAndNewFilePath[1].ToLower())
  296. {
  297. item.IsSelected = true;
  298. item.FileName = Path.GetFileName(oldAndNewFilePath[0]);
  299. item.IsSelected = true;
  300. item.Tag = oldAndNewFilePath[0];
  301. }
  302. }
  303. }
  304. }
  305. for (int i = 0; i < App.OpenedFilePathList.Count; i++)
  306. {
  307. if (oldAndNewFilePath[1].ToLower() == App.OpenedFilePathList[i].ToLower())
  308. {
  309. App.OpenedFilePathList[i] = oldAndNewFilePath[0];
  310. break;
  311. }
  312. }
  313. }
  314. private bool ViewPage_CheckExistBeforeOpenFileEvent(string[] arg)
  315. {
  316. if (App.OpenedFilePathList.Contains(arg[0]))
  317. {
  318. for (int i = 0; i < App.Current.Windows.Count; i++)
  319. {
  320. MainWindow win = App.Current.Windows[i] as MainWindow;
  321. if (win != null)
  322. {
  323. var items = win.TabControl.Items;
  324. foreach (TabItemExt item in items)
  325. {
  326. if (item.Tag.ToString().ToLower() == arg[0].ToLower())
  327. {
  328. win.Activate();
  329. item.IsSelected = true;
  330. return true;
  331. }
  332. }
  333. }
  334. }
  335. }
  336. oldAndNewFilePath = arg;
  337. return false;
  338. }
  339. public class TabItemExt : TabItem, INotifyPropertyChanged
  340. {
  341. public event PropertyChangedEventHandler PropertyChanged;
  342. private string _fileName;
  343. public string FileName
  344. {
  345. get
  346. {
  347. return _fileName;
  348. }
  349. set
  350. {
  351. if (_fileName != value)
  352. {
  353. _fileName = value;
  354. OnPropertyChanged();
  355. }
  356. }
  357. }
  358. public void OnPropertyChanged([CallerMemberName] string name = null)
  359. {
  360. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  361. }
  362. //protected override void OnSelected(RoutedEventArgs e)
  363. //{
  364. // string filePath = Tag?.ToString();
  365. // if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
  366. // {
  367. // this.IsSelected = true;
  368. // }
  369. //}
  370. }
  371. private DragablzItem FindParentDragablzItem(DependencyObject element)
  372. {
  373. while (element != null && !(element is DragablzItem))
  374. {
  375. element = VisualTreeHelper.GetParent(element);
  376. }
  377. return element as DragablzItem;
  378. }
  379. private TabControl FindParentTabControl(DependencyObject element)
  380. {
  381. while (element != null && !(element is TabControl))
  382. {
  383. element = VisualTreeHelper.GetParent(element);
  384. }
  385. return element as TabControl;
  386. }
  387. private void CloseFileButton_Click(object sender, RoutedEventArgs e)
  388. {
  389. var button = sender as Button;
  390. var dragablzItem = FindParentDragablzItem(button);
  391. var tabControl = FindParentTabControl(dragablzItem);
  392. MainPage mainPage = (dragablzItem.Content as TabItemExt).Content as MainPage;
  393. if (mainPage == null)
  394. {
  395. return;
  396. }
  397. if (mainPage.CanSave)
  398. {
  399. string fileName = (dragablzItem.Content as TabItemExt).FileName;
  400. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  401. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  402. if (result == MessageBoxResult.Yes)
  403. {
  404. mainPage.SaveFile();
  405. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  406. tabControl.Items.Remove(dragablzItem.Content);
  407. }
  408. else if (result == MessageBoxResult.No)
  409. {
  410. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  411. tabControl.Items.Remove(dragablzItem.Content);
  412. }
  413. else
  414. {
  415. }
  416. }
  417. else
  418. {
  419. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  420. tabControl.Items.Remove(dragablzItem.Content);
  421. }
  422. mainPage.CloseFile();
  423. if (tabControl.Items.Count == 0)
  424. {
  425. HomePageButton.IsToggled = true;
  426. }
  427. }
  428. private void DefaultAddButton_Click(object sender, RoutedEventArgs e)
  429. {
  430. string filePath = CommonHelper.GetExistedPathOrEmpty();
  431. if (filePath != string.Empty)
  432. {
  433. TabControlLoadDocument(filePath);
  434. }
  435. }
  436. private void MinimizeButton_Click(object sender, RoutedEventArgs e)
  437. {
  438. SystemCommands.MinimizeWindow(this);
  439. }
  440. private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
  441. {
  442. if (this.WindowState == WindowState.Maximized)
  443. {
  444. SystemCommands.RestoreWindow(this);
  445. }
  446. else
  447. {
  448. SystemCommands.MaximizeWindow(this);
  449. }
  450. }
  451. private void CloseButton_Click(object sender, RoutedEventArgs e)
  452. {
  453. int count = TabControl.Items.Count;
  454. while (count > 0)
  455. {
  456. TabItemExt item = TabControl.Items[0] as TabItemExt;
  457. MainPage mainPage = item.Content as MainPage;
  458. if (mainPage == null)
  459. {
  460. count--;
  461. continue;
  462. }
  463. if (mainPage.CanSave)
  464. {
  465. string fileName = item.FileName;
  466. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  467. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  468. if (result == MessageBoxResult.Yes)
  469. {
  470. mainPage.SaveFile();
  471. App.OpenedFilePathList.Remove(item.Tag.ToString());
  472. TabControl.Items.Remove(item);
  473. count--;
  474. }
  475. else if (result == MessageBoxResult.No)
  476. {
  477. App.OpenedFilePathList.Remove(item.Tag.ToString());
  478. TabControl.Items.Remove(item);
  479. count--;
  480. }
  481. else
  482. {
  483. break;
  484. }
  485. }
  486. else
  487. {
  488. App.OpenedFilePathList.Remove(item.Tag.ToString());
  489. TabControl.Items.Remove(item);
  490. count--;
  491. }
  492. mainPage.CloseFile();
  493. }
  494. if (count == 0)
  495. {
  496. SystemCommands.CloseWindow(this);
  497. }
  498. }
  499. private void SettingsBtn_Click(object sender, RoutedEventArgs e)
  500. {
  501. SettingsDialog settingsDialog = new SettingsDialog();
  502. settingsDialog.LanguageChanged -= SettingsDialog_LanguageChanged;
  503. settingsDialog.LanguageChanged += SettingsDialog_LanguageChanged;
  504. settingsDialog.Owner = this;
  505. settingsDialog.ShowDialog();
  506. }
  507. private void SettingsDialog_LanguageChanged(object sender, string e)
  508. {
  509. foreach (var tab in TabControl.Items)
  510. {
  511. var item = tab as TabItemExt;
  512. if (item?.Content is MainPage mainPage)
  513. {
  514. if (mainPage.CanSave)
  515. {
  516. string fileName = item.FileName;
  517. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  518. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  519. if (result == MessageBoxResult.Yes)
  520. {
  521. mainPage.SaveFile();
  522. }
  523. else
  524. {
  525. break;
  526. }
  527. }
  528. mainPage.GetPDFViewControl().GetCPDFViewer().CloseDocument();
  529. }
  530. }
  531. if (Properties.Settings.Default.LastOpenedFiles == null)
  532. {
  533. Properties.Settings.Default.LastOpenedFiles = new System.Collections.Specialized.StringCollection();
  534. }
  535. Properties.Settings.Default.LastOpenedFiles.Clear();
  536. foreach (var filePath in App.OpenedFilePathList)
  537. {
  538. Properties.Settings.Default.LastOpenedFiles.Add(filePath);
  539. }
  540. Properties.Settings.Default.LastSelectedFileIndex = TabControl.SelectedIndex;
  541. Properties.Settings.Default.IsLoadLastFileNeeded = true;
  542. Properties.Settings.Default.Save();
  543. System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  544. this.Close();
  545. }
  546. private void HomePageButton_Toggled(object sender, RoutedEventArgs e)
  547. {
  548. if (sender is HomePageButton homePageButton && homePageButton.IsToggled)
  549. {
  550. TabControl.SelectedIndex = -1;
  551. }
  552. else
  553. {
  554. TabControl.SelectedIndex = 0;
  555. }
  556. }
  557. protected virtual void OnPropertyChanged(string propertyName = null)
  558. {
  559. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  560. }
  561. protected bool UpdateProper<T>(ref T properValue, T newValue, [CallerMemberName] string properName = "")
  562. {
  563. if (object.Equals(properValue, newValue))
  564. return false;
  565. properValue = newValue;
  566. OnPropertyChanged(properName);
  567. return true;
  568. }
  569. private bool HomePageButton_QueryLock(object sender, HomePageButton.QueryLockEventArgs e)
  570. {
  571. return TabControl.Items.Count <= 0;
  572. }
  573. }
  574. }