MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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, annotationControl.PDFViewControl.Password);
  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. passwordViewer.Password = e;
  186. pdfViewer = passwordViewer;
  187. LoadDocument();
  188. }
  189. else
  190. {
  191. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  192. }
  193. }
  194. }
  195. private void PasswordUI_Canceled(object sender, EventArgs e)
  196. {
  197. PopupBorder.Visibility = Visibility.Collapsed;
  198. PasswordUI.Visibility = Visibility.Collapsed;
  199. }
  200. private void PasswordUI_Closed(object sender, EventArgs e)
  201. {
  202. PopupBorder.Visibility = Visibility.Collapsed;
  203. PasswordUI.Visibility = Visibility.Collapsed;
  204. }
  205. #endregion
  206. #region Load Unload custom control
  207. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  208. {
  209. pdfViewer = new PDFViewControl();
  210. LoadDefaultDocument();
  211. }
  212. private void LoadCustomControl()
  213. {
  214. regularViewerControl.PdfViewControl = pdfViewer;
  215. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  216. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  217. regularViewerControl.SetBOTAContainer(null);
  218. regularViewerControl.SetBOTAContainer(botaBarControl);
  219. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  220. PDFGrid.Child = regularViewerControl;
  221. }
  222. #endregion
  223. #region Event handle
  224. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  225. {
  226. if (e.Key == "Zoom")
  227. {
  228. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  229. }
  230. }
  231. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  232. {
  233. if (e.PropertyName == "RightPanel")
  234. {
  235. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  236. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  237. }
  238. }
  239. private void AnnotationControl_OnCanSaveChanged(object sender, bool e)
  240. {
  241. this.CanSave = e;
  242. }
  243. private void PdfAnnotationControl_RefreshAnnotList(object sender, EventArgs e)
  244. {
  245. botaBarControl.LoadAnnotationList();
  246. }
  247. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  248. {
  249. SaveFile();
  250. pdfViewer.PDFView.UndoManager.CanSave = false;
  251. }
  252. private void OpenFile_Click(object sender, RoutedEventArgs e)
  253. {
  254. OpenFile();
  255. }
  256. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  257. {
  258. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  259. }
  260. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  261. {
  262. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  263. if (item.Content as string == currentMode)
  264. {
  265. return;
  266. }
  267. ClearPanelState();
  268. if (currentMode == "Viewer")
  269. {
  270. regularViewerControl.ClearViewerControl();
  271. }
  272. else if (currentMode == "Annotation")
  273. {
  274. annotationControl.ClearViewerControl();
  275. }
  276. if (item.Content as string == "Viewer")
  277. {
  278. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
  279. {
  280. PDFGrid.Child = regularViewerControl;
  281. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  282. regularViewerControl.PdfViewControl = pdfViewer;
  283. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  284. regularViewerControl.SetBOTAContainer(botaBarControl);
  285. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  286. }
  287. }
  288. else if (item.Content as string == "Annotation")
  289. {
  290. if (annotationControl.PDFViewControl != null && annotationControl.PDFViewControl.PDFView != null)
  291. {
  292. PDFGrid.Child = annotationControl;
  293. annotationControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  294. annotationControl.PDFViewControl = pdfViewer;
  295. annotationControl.InitWithPDFViewer(pdfViewer.PDFView);
  296. annotationControl.SetBOTAContainer(botaBarControl);
  297. annotationControl.SetDisplaySettingsControl(displaySettingsControl);
  298. }
  299. }
  300. currentMode = item.Content as string;
  301. }
  302. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  303. {
  304. PasswordUI.Visibility = Visibility.Collapsed;
  305. FileInfoUI.Visibility = Visibility.Visible;
  306. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  307. PopupBorder.Visibility = Visibility.Visible;
  308. }
  309. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  310. {
  311. panelState.RightPanel =
  312. ((sender as ToggleButton).IsChecked == true) ?
  313. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  314. }
  315. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  316. {
  317. panelState.RightPanel =
  318. ((sender as ToggleButton).IsChecked == true) ?
  319. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  320. }
  321. private void ClearPanelState()
  322. {
  323. LeftToolPanelButtonIsChecked = false;
  324. ViewSettingBtnIsChecked = false;
  325. RightToolPanelButtonIsChecked = false;
  326. }
  327. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  328. {
  329. LeftToolPanelButton.IsChecked = true;
  330. LeftToolPanelButtonIsChecked = true;
  331. botaBarControl.SelectBotaTool(BOTATools.Search);
  332. }
  333. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  334. {
  335. PopupBorder.Visibility = Visibility.Collapsed;
  336. }
  337. public event PropertyChangedEventHandler PropertyChanged;
  338. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  339. {
  340. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  341. }
  342. #endregion
  343. #region Save file
  344. /// <summary>
  345. /// Save the file to another PDF file.
  346. /// </summary>
  347. public void SaveAsFile()
  348. {
  349. {
  350. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  351. {
  352. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  353. SaveFileDialog saveDialog = new SaveFileDialog();
  354. saveDialog.Filter = "(*.pdf)|*.pdf";
  355. saveDialog.DefaultExt = ".pdf";
  356. saveDialog.OverwritePrompt = true;
  357. if (saveDialog.ShowDialog() == true)
  358. {
  359. pdfDoc.WriteToFilePath(saveDialog.FileName);
  360. }
  361. }
  362. }
  363. }
  364. /// <summary>
  365. /// Save the file in the current path.
  366. /// </summary>
  367. private void SaveFile()
  368. {
  369. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  370. {
  371. try
  372. {
  373. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  374. if (pdfDoc.WriteToLoadedPath())
  375. {
  376. return;
  377. }
  378. SaveFileDialog saveDialog = new SaveFileDialog();
  379. saveDialog.Filter = "(*.pdf)|*.pdf";
  380. saveDialog.DefaultExt = ".pdf";
  381. saveDialog.OverwritePrompt = true;
  382. if (saveDialog.ShowDialog() == true)
  383. {
  384. pdfDoc.WriteToFilePath(saveDialog.FileName);
  385. }
  386. }
  387. catch (Exception ex)
  388. {
  389. }
  390. }
  391. }
  392. #endregion
  393. }
  394. }