MainWindow.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. using System.Reflection;
  26. using System.Net.Http;
  27. namespace PDFViewer
  28. {
  29. /// <summary>
  30. /// Interaction logic for MainWindow.xaml
  31. /// </summary>
  32. public partial class MainWindow : Window, INotifyPropertyChanged
  33. {
  34. #region Property
  35. private PDFViewControl passwordViewer;
  36. private PDFViewControl pdfViewControl = new PDFViewControl();
  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, blankPageSetting.Size.Width * 7.2 / 2.54, 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. viewPage.pdfViewer.PDFView.UndoManager.CanSave = true;
  161. }
  162. else
  163. {
  164. string filePath = CommonHelper.GetExistedPathOrEmpty();
  165. if (filePath != string.Empty)
  166. {
  167. TabControlLoadDocument(filePath);
  168. }
  169. }
  170. }
  171. private void ViewPage_AfterSaveAsFileEvent(object sender, string e)
  172. {
  173. if (sender is MainPage mainPage)
  174. {
  175. var tabItem = (from object t in TabControl.Items select t as TabItemExt).FirstOrDefault(item => Equals(item.Content, mainPage));
  176. if (tabItem != null)
  177. {
  178. tabItem.FileName = Path.GetFileName(e);
  179. tabItem.Tag = e;
  180. }
  181. mainPage.pdfViewer.PDFView.CloseDocument();
  182. mainPage.pdfViewer.PDFView.InitDocument(e);
  183. mainPage.pdfViewer.PDFView.Load();
  184. App.OpenedFilePathList.Add(e);
  185. }
  186. }
  187. private void TabControlLoadDocument(string filePath, string featureName = "")
  188. {
  189. if (App.OpenedFilePathList.Contains(filePath))
  190. {
  191. for (int i = 0; i < App.Current.Windows.Count; i++)
  192. {
  193. MainWindow win = App.Current.Windows[i] as MainWindow;
  194. if (win != null)
  195. {
  196. var items = win.TabControl.Items;
  197. foreach (TabItemExt item in items)
  198. {
  199. if (item.Tag.ToString().ToLower() == filePath.ToLower())
  200. {
  201. win.Activate();
  202. item.IsSelected = true;
  203. (item.Content as MainPage).SetFeatureMode(featureName);
  204. return;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. TabItemExt tabItem = new TabItemExt();
  211. MainPage viewPage = new MainPage();
  212. viewPage.CheckExistBeforeOpenFileEvent -= ViewPage_CheckExistBeforeOpenFileEvent;
  213. viewPage.FileChangeEvent -= ViewPage_FileChangeEvent;
  214. viewPage.AfterSaveAsFileEvent -= ViewPage_AfterSaveAsFileEvent;
  215. viewPage.CheckExistBeforeOpenFileEvent += ViewPage_CheckExistBeforeOpenFileEvent;
  216. viewPage.FileChangeEvent += ViewPage_FileChangeEvent;
  217. viewPage.AfterSaveAsFileEvent += ViewPage_AfterSaveAsFileEvent;
  218. passwordViewer = new PDFViewControl();
  219. passwordViewer.PDFView.InitDocument(filePath);
  220. if (passwordViewer.PDFView.Document == null)
  221. {
  222. MessageBox.Show("Open File Failed");
  223. return;
  224. }
  225. if (passwordViewer.PDFView.Document.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. private void PasswordUI_Closed(object sender, EventArgs e)
  248. {
  249. PopupBorder.Visibility = Visibility.Collapsed;
  250. PasswordUI.Visibility = Visibility.Collapsed;
  251. }
  252. private void PasswordUI_Canceled(object sender, EventArgs e)
  253. {
  254. PopupBorder.Visibility = Visibility.Collapsed;
  255. PasswordUI.Visibility = Visibility.Collapsed;
  256. }
  257. private void PasswordUI_Confirmed(object sender, string e)
  258. {
  259. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  260. {
  261. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  262. if (passwordViewer.PDFView.Document.IsLocked == false)
  263. {
  264. PasswordUI.SetShowError("", Visibility.Collapsed);
  265. PasswordUI.ClearPassword();
  266. PasswordUI.Visibility = Visibility.Collapsed;
  267. PopupBorder.Visibility = Visibility.Collapsed;
  268. pdfViewControl = passwordViewer;
  269. string filePath = passwordViewer.PDFView.Document.FilePath;
  270. TabItemExt tabItem = new TabItemExt();
  271. MainPage viewPage = new MainPage();
  272. tabItem.Content = viewPage;
  273. tabItem.IsSelected = true;
  274. tabItem.FileName = Path.GetFileName(filePath);
  275. tabItem.Tag = filePath;
  276. viewPage.SetPDFViewer(pdfViewControl);
  277. App.OpenedFilePathList.Add(filePath);
  278. TabControl.Items.Add(tabItem);
  279. }
  280. else
  281. {
  282. PasswordUI.SetShowError("error", Visibility.Visible);
  283. }
  284. }
  285. }
  286. private void ViewPage_FileChangeEvent(object sender, EventArgs e)
  287. {
  288. for (int i = 0; i < App.Current.Windows.Count; i++)
  289. {
  290. MainWindow win = App.Current.Windows[i] as MainWindow;
  291. if (win != null)
  292. {
  293. var items = win.TabControl.Items;
  294. foreach (TabItemExt item in items)
  295. {
  296. if (item.Tag.ToString().ToLower() == oldAndNewFilePath[1].ToLower())
  297. {
  298. item.IsSelected = true;
  299. item.FileName = Path.GetFileName(oldAndNewFilePath[0]);
  300. item.IsSelected = true;
  301. item.Tag = oldAndNewFilePath[0];
  302. }
  303. }
  304. }
  305. }
  306. for (int i = 0; i < App.OpenedFilePathList.Count; i++)
  307. {
  308. if (oldAndNewFilePath[1].ToLower() == App.OpenedFilePathList[i].ToLower())
  309. {
  310. App.OpenedFilePathList[i] = oldAndNewFilePath[0];
  311. break;
  312. }
  313. }
  314. }
  315. private bool ViewPage_CheckExistBeforeOpenFileEvent(string[] arg)
  316. {
  317. if (App.OpenedFilePathList.Contains(arg[0]))
  318. {
  319. for (int i = 0; i < App.Current.Windows.Count; i++)
  320. {
  321. MainWindow win = App.Current.Windows[i] as MainWindow;
  322. if (win != null)
  323. {
  324. var items = win.TabControl.Items;
  325. foreach (TabItemExt item in items)
  326. {
  327. if (item.Tag.ToString().ToLower() == arg[0].ToLower())
  328. {
  329. win.Activate();
  330. item.IsSelected = true;
  331. return true;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. oldAndNewFilePath = arg;
  338. return false;
  339. }
  340. public class TabItemExt : TabItem, INotifyPropertyChanged
  341. {
  342. public event PropertyChangedEventHandler PropertyChanged;
  343. private string _fileName;
  344. public string FileName
  345. {
  346. get
  347. {
  348. return _fileName;
  349. }
  350. set
  351. {
  352. if (_fileName != value)
  353. {
  354. _fileName = value;
  355. OnPropertyChanged();
  356. }
  357. }
  358. }
  359. public void OnPropertyChanged([CallerMemberName] string name = null)
  360. {
  361. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  362. }
  363. }
  364. private DragablzItem FindParentDragablzItem(DependencyObject element)
  365. {
  366. while (element != null && !(element is DragablzItem))
  367. {
  368. element = VisualTreeHelper.GetParent(element);
  369. }
  370. return element as DragablzItem;
  371. }
  372. private TabControl FindParentTabControl(DependencyObject element)
  373. {
  374. while (element != null && !(element is TabControl))
  375. {
  376. element = VisualTreeHelper.GetParent(element);
  377. }
  378. return element as TabControl;
  379. }
  380. private void CloseFileButton_Click(object sender, RoutedEventArgs e)
  381. {
  382. var button = sender as Button;
  383. var dragablzItem = FindParentDragablzItem(button);
  384. var tabControl = FindParentTabControl(dragablzItem);
  385. MainPage mainPage = (dragablzItem.Content as TabItemExt).Content as MainPage;
  386. if (mainPage == null)
  387. {
  388. return;
  389. }
  390. if (mainPage.CanSave)
  391. {
  392. string fileName = (dragablzItem.Content as TabItemExt).FileName;
  393. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  394. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  395. if (result == MessageBoxResult.Yes)
  396. {
  397. mainPage.SaveFile();
  398. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  399. tabControl.Items.Remove(dragablzItem.Content);
  400. }
  401. else if (result == MessageBoxResult.No)
  402. {
  403. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  404. tabControl.Items.Remove(dragablzItem.Content);
  405. }
  406. else
  407. {
  408. }
  409. }
  410. else
  411. {
  412. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  413. tabControl.Items.Remove(dragablzItem.Content);
  414. }
  415. if (tabControl.Items.Count == 0)
  416. {
  417. HomePageButton.IsToggled = true;
  418. }
  419. }
  420. private void DefaultAddButton_Click(object sender, RoutedEventArgs e)
  421. {
  422. string filePath = CommonHelper.GetExistedPathOrEmpty();
  423. if (filePath != string.Empty)
  424. {
  425. TabControlLoadDocument(filePath);
  426. }
  427. }
  428. private void MinimizeButton_Click(object sender, RoutedEventArgs e)
  429. {
  430. SystemCommands.MinimizeWindow(this);
  431. }
  432. private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
  433. {
  434. if (this.WindowState == WindowState.Maximized)
  435. {
  436. SystemCommands.RestoreWindow(this);
  437. }
  438. else
  439. {
  440. SystemCommands.MaximizeWindow(this);
  441. }
  442. }
  443. private void CloseButton_Click(object sender, RoutedEventArgs e)
  444. {
  445. int count = TabControl.Items.Count;
  446. while (count > 0)
  447. {
  448. TabItemExt item = TabControl.Items[0] as TabItemExt;
  449. MainPage mainPage = item.Content as MainPage;
  450. if (mainPage == null)
  451. {
  452. count--;
  453. continue;
  454. }
  455. if (mainPage.CanSave)
  456. {
  457. string fileName = item.FileName;
  458. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  459. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  460. if (result == MessageBoxResult.Yes)
  461. {
  462. mainPage.SaveFile();
  463. App.OpenedFilePathList.Remove(item.Tag.ToString());
  464. TabControl.Items.Remove(item);
  465. count--;
  466. }
  467. else if (result == MessageBoxResult.No)
  468. {
  469. App.OpenedFilePathList.Remove(item.Tag.ToString());
  470. TabControl.Items.Remove(item);
  471. count--;
  472. }
  473. else
  474. {
  475. break;
  476. }
  477. }
  478. else
  479. {
  480. App.OpenedFilePathList.Remove(item.Tag.ToString());
  481. TabControl.Items.Remove(item);
  482. count--;
  483. }
  484. }
  485. if (count == 0)
  486. {
  487. SystemCommands.CloseWindow(this);
  488. }
  489. }
  490. private void SettingsBtn_Click(object sender, RoutedEventArgs e)
  491. {
  492. SettingsDialog settingsDialog = new SettingsDialog();
  493. settingsDialog.LanguageChanged -= SettingsDialog_LanguageChanged;
  494. settingsDialog.LanguageChanged += SettingsDialog_LanguageChanged;
  495. settingsDialog.Owner = this;
  496. settingsDialog.ShowDialog();
  497. }
  498. private void SettingsDialog_LanguageChanged(object sender, string e)
  499. {
  500. foreach (var tab in TabControl.Items)
  501. {
  502. var item = tab as TabItemExt;
  503. if (item?.Content is MainPage mainPage)
  504. {
  505. if (mainPage.CanSave)
  506. {
  507. string fileName = item.FileName;
  508. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  509. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  510. if (result == MessageBoxResult.Yes)
  511. {
  512. mainPage.SaveFile();
  513. }
  514. else
  515. {
  516. break;
  517. }
  518. }
  519. mainPage.pdfViewer.PDFView.CloseDocument();
  520. }
  521. }
  522. if (Properties.Settings.Default.LastOpenedFiles == null)
  523. {
  524. Properties.Settings.Default.LastOpenedFiles = new System.Collections.Specialized.StringCollection();
  525. }
  526. Properties.Settings.Default.LastOpenedFiles.Clear();
  527. foreach (var filePath in App.OpenedFilePathList)
  528. {
  529. Properties.Settings.Default.LastOpenedFiles.Add(filePath);
  530. }
  531. Properties.Settings.Default.LastSelectedFileIndex = TabControl.SelectedIndex;
  532. Properties.Settings.Default.IsLoadLastFileNeeded = true;
  533. Properties.Settings.Default.Save();
  534. System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  535. this.Close();
  536. }
  537. private void HomePageButton_Toggled(object sender, RoutedEventArgs e)
  538. {
  539. if (sender is HomePageButton homePageButton && homePageButton.IsToggled)
  540. {
  541. TabControl.SelectedIndex = -1;
  542. }
  543. else
  544. {
  545. TabControl.SelectedIndex = 0;
  546. }
  547. }
  548. public event PropertyChangedEventHandler PropertyChanged;
  549. protected virtual void OnPropertyChanged(string propertyName = null)
  550. {
  551. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  552. }
  553. protected bool UpdateProper<T>(ref T properValue,
  554. T newValue,
  555. [CallerMemberName] string properName = "")
  556. {
  557. if (object.Equals(properValue, newValue))
  558. return false;
  559. properValue = newValue;
  560. OnPropertyChanged(properName);
  561. return true;
  562. }
  563. private bool HomePageButton_QueryLock(object sender, HomePageButton.QueryLockEventArgs e)
  564. {
  565. return TabControl.Items.Count <= 0;
  566. }
  567. }
  568. }