MainWindow.xaml.cs 24 KB

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