MainWindow.xaml.cs 23 KB

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