MainWindow.xaml.cs 24 KB

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