MainWindow.xaml.cs 16 KB

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