MainWindow.xaml.cs 16 KB

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