MainWindow.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. passwordViewer.Password = e;
  269. pdfViewControl = passwordViewer;
  270. string filePath = passwordViewer.PDFView.Document.FilePath;
  271. TabItemExt tabItem = new TabItemExt();
  272. MainPage viewPage = new MainPage();
  273. tabItem.Content = viewPage;
  274. tabItem.IsSelected = true;
  275. tabItem.FileName = Path.GetFileName(filePath);
  276. tabItem.Tag = filePath;
  277. viewPage.SetPDFViewer(pdfViewControl);
  278. App.OpenedFilePathList.Add(filePath);
  279. TabControl.Items.Add(tabItem);
  280. }
  281. else
  282. {
  283. PasswordUI.SetShowError("error", Visibility.Visible);
  284. }
  285. }
  286. }
  287. private void ViewPage_FileChangeEvent(object sender, EventArgs e)
  288. {
  289. for (int i = 0; i < App.Current.Windows.Count; i++)
  290. {
  291. MainWindow win = App.Current.Windows[i] as MainWindow;
  292. if (win != null)
  293. {
  294. var items = win.TabControl.Items;
  295. foreach (TabItemExt item in items)
  296. {
  297. if (item.Tag.ToString().ToLower() == oldAndNewFilePath[1].ToLower())
  298. {
  299. item.IsSelected = true;
  300. item.FileName = Path.GetFileName(oldAndNewFilePath[0]);
  301. item.IsSelected = true;
  302. item.Tag = oldAndNewFilePath[0];
  303. }
  304. }
  305. }
  306. }
  307. for (int i = 0; i < App.OpenedFilePathList.Count; i++)
  308. {
  309. if (oldAndNewFilePath[1].ToLower() == App.OpenedFilePathList[i].ToLower())
  310. {
  311. App.OpenedFilePathList[i] = oldAndNewFilePath[0];
  312. break;
  313. }
  314. }
  315. }
  316. private bool ViewPage_CheckExistBeforeOpenFileEvent(string[] arg)
  317. {
  318. if (App.OpenedFilePathList.Contains(arg[0]))
  319. {
  320. for (int i = 0; i < App.Current.Windows.Count; i++)
  321. {
  322. MainWindow win = App.Current.Windows[i] as MainWindow;
  323. if (win != null)
  324. {
  325. var items = win.TabControl.Items;
  326. foreach (TabItemExt item in items)
  327. {
  328. if (item.Tag.ToString().ToLower() == arg[0].ToLower())
  329. {
  330. win.Activate();
  331. item.IsSelected = true;
  332. return true;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. oldAndNewFilePath = arg;
  339. return false;
  340. }
  341. public class TabItemExt : TabItem, INotifyPropertyChanged
  342. {
  343. public event PropertyChangedEventHandler PropertyChanged;
  344. private string _fileName;
  345. public string FileName
  346. {
  347. get
  348. {
  349. return _fileName;
  350. }
  351. set
  352. {
  353. if (_fileName != value)
  354. {
  355. _fileName = value;
  356. OnPropertyChanged();
  357. }
  358. }
  359. }
  360. public void OnPropertyChanged([CallerMemberName] string name = null)
  361. {
  362. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  363. }
  364. }
  365. private DragablzItem FindParentDragablzItem(DependencyObject element)
  366. {
  367. while (element != null && !(element is DragablzItem))
  368. {
  369. element = VisualTreeHelper.GetParent(element);
  370. }
  371. return element as DragablzItem;
  372. }
  373. private TabControl FindParentTabControl(DependencyObject element)
  374. {
  375. while (element != null && !(element is TabControl))
  376. {
  377. element = VisualTreeHelper.GetParent(element);
  378. }
  379. return element as TabControl;
  380. }
  381. private void CloseFileButton_Click(object sender, RoutedEventArgs e)
  382. {
  383. var button = sender as Button;
  384. var dragablzItem = FindParentDragablzItem(button);
  385. var tabControl = FindParentTabControl(dragablzItem);
  386. MainPage mainPage = (dragablzItem.Content as TabItemExt).Content as MainPage;
  387. if (mainPage == null)
  388. {
  389. return;
  390. }
  391. if (mainPage.CanSave)
  392. {
  393. string fileName = (dragablzItem.Content as TabItemExt).FileName;
  394. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  395. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  396. if (result == MessageBoxResult.Yes)
  397. {
  398. mainPage.SaveFile();
  399. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  400. tabControl.Items.Remove(dragablzItem.Content);
  401. }
  402. else if (result == MessageBoxResult.No)
  403. {
  404. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  405. tabControl.Items.Remove(dragablzItem.Content);
  406. }
  407. else
  408. {
  409. }
  410. }
  411. else
  412. {
  413. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  414. tabControl.Items.Remove(dragablzItem.Content);
  415. }
  416. if (tabControl.Items.Count == 0)
  417. {
  418. HomePageButton.IsToggled = true;
  419. }
  420. }
  421. private void DefaultAddButton_Click(object sender, RoutedEventArgs e)
  422. {
  423. string filePath = CommonHelper.GetExistedPathOrEmpty();
  424. if (filePath != string.Empty)
  425. {
  426. TabControlLoadDocument(filePath);
  427. }
  428. }
  429. private void MinimizeButton_Click(object sender, RoutedEventArgs e)
  430. {
  431. SystemCommands.MinimizeWindow(this);
  432. }
  433. private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
  434. {
  435. if (this.WindowState == WindowState.Maximized)
  436. {
  437. SystemCommands.RestoreWindow(this);
  438. }
  439. else
  440. {
  441. SystemCommands.MaximizeWindow(this);
  442. }
  443. }
  444. private void CloseButton_Click(object sender, RoutedEventArgs e)
  445. {
  446. int count = TabControl.Items.Count;
  447. while (count > 0)
  448. {
  449. TabItemExt item = TabControl.Items[0] as TabItemExt;
  450. MainPage mainPage = item.Content as MainPage;
  451. if (mainPage == null)
  452. {
  453. count--;
  454. continue;
  455. }
  456. if (mainPage.CanSave)
  457. {
  458. string fileName = item.FileName;
  459. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  460. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  461. if (result == MessageBoxResult.Yes)
  462. {
  463. mainPage.SaveFile();
  464. App.OpenedFilePathList.Remove(item.Tag.ToString());
  465. TabControl.Items.Remove(item);
  466. count--;
  467. }
  468. else if (result == MessageBoxResult.No)
  469. {
  470. App.OpenedFilePathList.Remove(item.Tag.ToString());
  471. TabControl.Items.Remove(item);
  472. count--;
  473. }
  474. else
  475. {
  476. break;
  477. }
  478. }
  479. else
  480. {
  481. App.OpenedFilePathList.Remove(item.Tag.ToString());
  482. TabControl.Items.Remove(item);
  483. count--;
  484. }
  485. }
  486. if (count == 0)
  487. {
  488. SystemCommands.CloseWindow(this);
  489. }
  490. }
  491. private void SettingsBtn_Click(object sender, RoutedEventArgs e)
  492. {
  493. SettingsDialog settingsDialog = new SettingsDialog();
  494. settingsDialog.LanguageChanged -= SettingsDialog_LanguageChanged;
  495. settingsDialog.LanguageChanged += SettingsDialog_LanguageChanged;
  496. settingsDialog.Owner = this;
  497. settingsDialog.ShowDialog();
  498. }
  499. private void SettingsDialog_LanguageChanged(object sender, string e)
  500. {
  501. foreach (var tab in TabControl.Items)
  502. {
  503. var item = tab as TabItemExt;
  504. if (item?.Content is MainPage mainPage)
  505. {
  506. if (mainPage.CanSave)
  507. {
  508. string fileName = item.FileName;
  509. var message = fileName + " " + LanguageHelper.CommonManager.GetString("Warn_NotSave");
  510. var result = MessageBox.Show(message, LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  511. if (result == MessageBoxResult.Yes)
  512. {
  513. mainPage.SaveFile();
  514. }
  515. else
  516. {
  517. break;
  518. }
  519. }
  520. mainPage.pdfViewer.PDFView.CloseDocument();
  521. }
  522. }
  523. if (Properties.Settings.Default.LastOpenedFiles == null)
  524. {
  525. Properties.Settings.Default.LastOpenedFiles = new System.Collections.Specialized.StringCollection();
  526. }
  527. Properties.Settings.Default.LastOpenedFiles.Clear();
  528. foreach (var filePath in App.OpenedFilePathList)
  529. {
  530. Properties.Settings.Default.LastOpenedFiles.Add(filePath);
  531. }
  532. Properties.Settings.Default.LastSelectedFileIndex = TabControl.SelectedIndex;
  533. Properties.Settings.Default.IsLoadLastFileNeeded = true;
  534. Properties.Settings.Default.Save();
  535. System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  536. this.Close();
  537. }
  538. private void HomePageButton_Toggled(object sender, RoutedEventArgs e)
  539. {
  540. if (sender is HomePageButton homePageButton && homePageButton.IsToggled)
  541. {
  542. TabControl.SelectedIndex = -1;
  543. }
  544. else
  545. {
  546. TabControl.SelectedIndex = 0;
  547. }
  548. }
  549. public event PropertyChangedEventHandler PropertyChanged;
  550. protected virtual void OnPropertyChanged(string propertyName = null)
  551. {
  552. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  553. }
  554. protected bool UpdateProper<T>(ref T properValue,
  555. T newValue,
  556. [CallerMemberName] string properName = "")
  557. {
  558. if (object.Equals(properValue, newValue))
  559. return false;
  560. properValue = newValue;
  561. OnPropertyChanged(properName);
  562. return true;
  563. }
  564. private bool HomePageButton_QueryLock(object sender, HomePageButton.QueryLockEventArgs e)
  565. {
  566. return TabControl.Items.Count <= 0;
  567. }
  568. }
  569. }