MainWindow.xaml.cs 16 KB

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