MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Data;
  3. using Compdfkit_Tools.Helper;
  4. using Compdfkit_Tools.PDFControl;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.AnnotEvent;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Drawing;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Runtime.CompilerServices;
  17. using System.Threading;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Controls.Primitives;
  21. using System.Windows.Input;
  22. using System.Windows.Media.Imaging;
  23. using Compdfkit_Tools.PDFView;
  24. using System.Reflection;
  25. using System.Linq;
  26. namespace AnnotationViewControl
  27. {
  28. /// <summary>
  29. /// Interaction logic for MainWindow.xaml
  30. /// </summary>
  31. public partial class MainWindow: Window, INotifyPropertyChanged
  32. {
  33. #region Properties
  34. private string currentMode = "Annotation";
  35. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  36. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  37. private PDFViewControl pdfViewer;
  38. private PDFViewControl passwordViewer;
  39. private AnnotationControl annotationControl = new AnnotationControl();
  40. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
  41. private PanelState panelState = PanelState.GetInstance();
  42. private bool _canSave = false;
  43. public bool CanSave
  44. {
  45. get => _canSave;
  46. set
  47. {
  48. _canSave = value;
  49. OnPropertyChanged();
  50. }
  51. }
  52. public bool LeftToolPanelButtonIsChecked
  53. {
  54. get => panelState.IsLeftPanelExpand;
  55. set
  56. {
  57. panelState.IsLeftPanelExpand = value;
  58. OnPropertyChanged();
  59. }
  60. }
  61. public bool RightToolPanelButtonIsChecked
  62. {
  63. get
  64. {
  65. return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
  66. }
  67. set
  68. {
  69. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  70. OnPropertyChanged();
  71. }
  72. }
  73. public bool ViewSettingBtnIsChecked
  74. {
  75. get
  76. {
  77. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  78. }
  79. set
  80. {
  81. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  82. OnPropertyChanged();
  83. }
  84. }
  85. public string AppInfo
  86. {
  87. get { return Assembly.GetExecutingAssembly().GetName().Name + " " + string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3)); }
  88. }
  89. #endregion
  90. public MainWindow()
  91. {
  92. InitializeComponent();
  93. DataContext = this;
  94. }
  95. #region Load document
  96. private void LoadDefaultDocument()
  97. {
  98. string defaultFilePath = "ComPDFKit_Annotations_Sample_File.pdf";
  99. pdfViewer.PDFView.InitDocument(defaultFilePath);
  100. LoadDocument();
  101. }
  102. private void LoadDocument()
  103. {
  104. pdfViewer.PDFView.Load();
  105. pdfViewer.PDFView.SetShowLink(true);
  106. pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  107. pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  108. PDFGrid.Child = annotationControl;
  109. annotationControl.PDFViewControl = pdfViewer;
  110. annotationControl.InitWithPDFViewer(pdfViewer.PDFView);
  111. annotationControl.ClearAllToolState();
  112. annotationControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  113. annotationControl.OnCanSaveChanged -= AnnotationControl_OnCanSaveChanged;
  114. annotationControl.OnCanSaveChanged += AnnotationControl_OnCanSaveChanged;
  115. annotationControl.OnAnnotEditHandler -= PdfAnnotationControl_RefreshAnnotList;
  116. annotationControl.OnAnnotEditHandler += PdfAnnotationControl_RefreshAnnotList;
  117. annotationControl.PDFViewControl.PDFView.SetFormFieldHighlight(true);
  118. PasswordUI.Closed -= PasswordUI_Closed;
  119. PasswordUI.Canceled -= PasswordUI_Canceled;
  120. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  121. PasswordUI.Closed += PasswordUI_Closed;
  122. PasswordUI.Canceled += PasswordUI_Canceled;
  123. PasswordUI.Confirmed += PasswordUI_Confirmed;
  124. ModeComboBox.SelectedIndex = 1;
  125. annotationControl.PDFViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  126. CPDFSaclingControl.InitWithPDFViewer(annotationControl.PDFViewControl.PDFView);
  127. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(annotationControl.PDFViewControl.PDFView.ZoomFactor * 100)));
  128. ViewSettingBtn.IsChecked = false;
  129. botaBarControl.InitWithPDFViewer(annotationControl.PDFViewControl.PDFView);
  130. botaBarControl.AddBOTAContent(new []{BOTATools.Thumbnail , BOTATools.Outline , BOTATools.Bookmark , BOTATools.Annotation , BOTATools.Search});
  131. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  132. annotationControl.SetBOTAContainer(botaBarControl);
  133. annotationControl.InitialPDFViewControl(annotationControl.PDFViewControl);
  134. panelState.PropertyChanged -= PanelState_PropertyChanged;
  135. panelState.PropertyChanged += PanelState_PropertyChanged;
  136. }
  137. private void OpenFile()
  138. {
  139. string filePath = CommonHelper.GetExistedPathOrEmpty();
  140. if (!string.IsNullOrEmpty(filePath) && annotationControl.PDFViewControl != null)
  141. {
  142. if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  143. {
  144. string oldFilePath = pdfViewer.PDFView.Document.FilePath;
  145. if (oldFilePath.ToLower() == filePath.ToLower())
  146. {
  147. return;
  148. }
  149. }
  150. passwordViewer = new PDFViewControl();
  151. passwordViewer.PDFView.InitDocument(filePath);
  152. if (passwordViewer.PDFView.Document == null)
  153. {
  154. MessageBox.Show("Open File Failed");
  155. return;
  156. }
  157. if (passwordViewer.PDFView.Document.IsLocked)
  158. {
  159. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  160. PasswordUI.ClearPassword();
  161. PopupBorder.Visibility = Visibility.Visible;
  162. PasswordUI.Visibility = Visibility.Visible;
  163. }
  164. else
  165. {
  166. pdfViewer.PDFView.Document.Release();
  167. pdfViewer = passwordViewer;
  168. LoadDocument();
  169. }
  170. }
  171. }
  172. #endregion
  173. #region Password
  174. private void PasswordUI_Confirmed(object sender, string e)
  175. {
  176. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  177. {
  178. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  179. if (passwordViewer.PDFView.Document.IsLocked == false)
  180. {
  181. PasswordUI.SetShowError("", Visibility.Collapsed);
  182. PasswordUI.ClearPassword();
  183. PasswordUI.Visibility = Visibility.Collapsed;
  184. PopupBorder.Visibility = Visibility.Collapsed;
  185. pdfViewer = passwordViewer;
  186. LoadDocument();
  187. }
  188. else
  189. {
  190. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  191. }
  192. }
  193. }
  194. private void PasswordUI_Canceled(object sender, EventArgs e)
  195. {
  196. PopupBorder.Visibility = Visibility.Collapsed;
  197. PasswordUI.Visibility = Visibility.Collapsed;
  198. }
  199. private void PasswordUI_Closed(object sender, EventArgs e)
  200. {
  201. PopupBorder.Visibility = Visibility.Collapsed;
  202. PasswordUI.Visibility = Visibility.Collapsed;
  203. }
  204. #endregion
  205. #region Load Unload custom control
  206. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  207. {
  208. pdfViewer = new PDFViewControl();
  209. LoadDefaultDocument();
  210. }
  211. private void LoadCustomControl()
  212. {
  213. regularViewerControl.PdfViewControl = pdfViewer;
  214. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  215. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  216. regularViewerControl.SetBOTAContainer(null);
  217. regularViewerControl.SetBOTAContainer(botaBarControl);
  218. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  219. PDFGrid.Child = regularViewerControl;
  220. }
  221. #endregion
  222. #region Event handle
  223. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  224. {
  225. if (e.Key == "Zoom")
  226. {
  227. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  228. }
  229. }
  230. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  231. {
  232. if (e.PropertyName == "RightPanel")
  233. {
  234. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  235. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  236. }
  237. }
  238. private void AnnotationControl_OnCanSaveChanged(object sender, bool e)
  239. {
  240. this.CanSave = e;
  241. }
  242. private void PdfAnnotationControl_RefreshAnnotList(object sender, EventArgs e)
  243. {
  244. botaBarControl.LoadAnnotationList();
  245. }
  246. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  247. {
  248. SaveFile();
  249. pdfViewer.PDFView.UndoManager.CanSave = false;
  250. }
  251. private void OpenFile_Click(object sender, RoutedEventArgs e)
  252. {
  253. OpenFile();
  254. }
  255. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  256. {
  257. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  258. }
  259. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  260. {
  261. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  262. if (item.Content as string == currentMode)
  263. {
  264. return;
  265. }
  266. ClearPanelState();
  267. if (currentMode == "Viewer")
  268. {
  269. regularViewerControl.ClearViewerControl();
  270. }
  271. else if (currentMode == "Annotation")
  272. {
  273. annotationControl.ClearViewerControl();
  274. }
  275. if (item.Content as string == "Viewer")
  276. {
  277. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
  278. {
  279. PDFGrid.Child = regularViewerControl;
  280. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  281. regularViewerControl.PdfViewControl = pdfViewer;
  282. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  283. regularViewerControl.SetBOTAContainer(botaBarControl);
  284. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  285. }
  286. }
  287. else if (item.Content as string == "Annotation")
  288. {
  289. if (annotationControl.PDFViewControl != null && annotationControl.PDFViewControl.PDFView != null)
  290. {
  291. PDFGrid.Child = annotationControl;
  292. annotationControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  293. annotationControl.PDFViewControl = pdfViewer;
  294. annotationControl.InitWithPDFViewer(pdfViewer.PDFView);
  295. annotationControl.SetBOTAContainer(botaBarControl);
  296. annotationControl.SetDisplaySettingsControl(displaySettingsControl);
  297. }
  298. }
  299. currentMode = item.Content as string;
  300. }
  301. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  302. {
  303. PasswordUI.Visibility = Visibility.Collapsed;
  304. FileInfoUI.Visibility = Visibility.Visible;
  305. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  306. PopupBorder.Visibility = Visibility.Visible;
  307. }
  308. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  309. {
  310. panelState.RightPanel =
  311. ((sender as ToggleButton).IsChecked == true) ?
  312. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  313. }
  314. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  315. {
  316. panelState.RightPanel =
  317. ((sender as ToggleButton).IsChecked == true) ?
  318. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  319. }
  320. private void ClearPanelState()
  321. {
  322. LeftToolPanelButtonIsChecked = false;
  323. ViewSettingBtnIsChecked = false;
  324. RightToolPanelButtonIsChecked = false;
  325. }
  326. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  327. {
  328. LeftToolPanelButton.IsChecked = true;
  329. LeftToolPanelButtonIsChecked = true;
  330. botaBarControl.SelectBotaTool(BOTATools.Search);
  331. }
  332. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  333. {
  334. PopupBorder.Visibility = Visibility.Collapsed;
  335. }
  336. public event PropertyChangedEventHandler PropertyChanged;
  337. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  338. {
  339. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  340. }
  341. #endregion
  342. #region Save file
  343. /// <summary>
  344. /// Save the file to another PDF file.
  345. /// </summary>
  346. public void SaveAsFile()
  347. {
  348. {
  349. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  350. {
  351. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  352. SaveFileDialog saveDialog = new SaveFileDialog();
  353. saveDialog.Filter = "(*.pdf)|*.pdf";
  354. saveDialog.DefaultExt = ".pdf";
  355. saveDialog.OverwritePrompt = true;
  356. if (saveDialog.ShowDialog() == true)
  357. {
  358. pdfDoc.WriteToFilePath(saveDialog.FileName);
  359. }
  360. }
  361. }
  362. }
  363. /// <summary>
  364. /// Save the file in the current path.
  365. /// </summary>
  366. private void SaveFile()
  367. {
  368. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  369. {
  370. try
  371. {
  372. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  373. if (pdfDoc.WriteToLoadedPath())
  374. {
  375. return;
  376. }
  377. SaveFileDialog saveDialog = new SaveFileDialog();
  378. saveDialog.Filter = "(*.pdf)|*.pdf";
  379. saveDialog.DefaultExt = ".pdf";
  380. saveDialog.OverwritePrompt = true;
  381. if (saveDialog.ShowDialog() == true)
  382. {
  383. pdfDoc.WriteToFilePath(saveDialog.FileName);
  384. }
  385. }
  386. catch (Exception ex)
  387. {
  388. }
  389. }
  390. }
  391. #endregion
  392. }
  393. }