MainWindow.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.Controls.Data;
  3. using ComPDFKit.Controls.Helper;
  4. using ComPDFKit.Controls.PDFControl;
  5. using ComPDFKitViewer;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Runtime.CompilerServices;
  15. using System.Threading;
  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.Controls.PDFView;
  22. using ComPDFKit.Tool;
  23. using System.Reflection;
  24. using System.Linq;
  25. using ComPDFKit.Controls.PDFControlUI;
  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.InitDocument(defaultFilePath);
  100. LoadDocument();
  101. }
  102. private void LoadDocument()
  103. {
  104. if (pdfViewer != null && pdfViewer.PDFViewTool != null)
  105. {
  106. CPDFViewer viewer = pdfViewer.PDFViewTool.GetCPDFViewer();
  107. CPDFDocument pdfDoc = viewer?.GetDocument();
  108. if (pdfDoc == null)
  109. {
  110. return;
  111. }
  112. //pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  113. //pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  114. PDFGrid.Child = annotationControl;
  115. annotationControl.PDFViewControl = pdfViewer;
  116. regularViewerControl.PdfViewControl = pdfViewer;
  117. annotationControl.InitWithPDFViewer(pdfViewer);
  118. annotationControl.ClearAllToolState();
  119. annotationControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  120. annotationControl.OnCanSaveChanged -= AnnotationControl_OnCanSaveChanged;
  121. annotationControl.OnCanSaveChanged += AnnotationControl_OnCanSaveChanged;
  122. annotationControl.OnAnnotEditHandler -= PdfAnnotationControl_RefreshAnnotList;
  123. annotationControl.OnAnnotEditHandler += PdfAnnotationControl_RefreshAnnotList;
  124. //annotationControl.PDFViewControl.PDFView.SetFormFieldHighlight(true);
  125. PasswordUI.Closed -= PasswordUI_Closed;
  126. PasswordUI.Canceled -= PasswordUI_Canceled;
  127. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  128. PasswordUI.Closed += PasswordUI_Closed;
  129. PasswordUI.Canceled += PasswordUI_Canceled;
  130. PasswordUI.Confirmed += PasswordUI_Confirmed;
  131. ModeComboBox.SelectedIndex = 1;
  132. //annotationControl.PDFViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  133. CPDFSaclingControl.InitWithPDFViewer(annotationControl.PDFViewControl);
  134. //CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(annotationControl.PDFViewControl.PDFView.ZoomFactor * 100)));
  135. displaySettingsControl.InitWithPDFViewer(annotationControl.PDFViewControl);
  136. ViewSettingBtn.IsChecked = false;
  137. botaBarControl.InitWithPDFViewer(annotationControl.PDFViewControl);
  138. botaBarControl.AddBOTAContent(new[]
  139. {
  140. BOTATools.Thumbnail, BOTATools.Outline, BOTATools.Bookmark, BOTATools.Annotation, BOTATools.Search
  141. });
  142. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  143. annotationControl.SetBOTAContainer(botaBarControl);
  144. annotationControl.InitialPDFViewControl(annotationControl.PDFViewControl);
  145. panelState.PropertyChanged -= PanelState_PropertyChanged;
  146. panelState.PropertyChanged += PanelState_PropertyChanged;
  147. displaySettingsControl.SplitModeChanged -= DisplaySettingsControl_SplitModeChanged;
  148. displaySettingsControl.SplitModeChanged += DisplaySettingsControl_SplitModeChanged;
  149. annotationControl.PDFViewControl.PDFViewTool.DocumentModifiedChanged -= PDFViewTool_DocumentModifiedChanged;
  150. annotationControl.PDFViewControl.PDFViewTool.DocumentModifiedChanged += PDFViewTool_DocumentModifiedChanged;
  151. }
  152. }
  153. private void PDFViewTool_DocumentModifiedChanged(object sender, EventArgs e)
  154. {
  155. CanSave = annotationControl.PDFViewControl.PDFViewTool.IsDocumentModified;
  156. }
  157. private void DisplaySettingsControl_SplitModeChanged(object sender, CPDFViewModeUI.SplitMode e)
  158. {
  159. pdfViewer.SetSplitViewMode(e);
  160. }
  161. private void OpenFile()
  162. {
  163. string filePath = CommonHelper.GetExistedPathOrEmpty();
  164. if (!string.IsNullOrEmpty(filePath) && annotationControl.PDFViewControl != null)
  165. {
  166. if (pdfViewer != null && pdfViewer.PDFToolManager != null)
  167. {
  168. string oldFilePath = pdfViewer.PDFToolManager.GetDocument().FilePath;
  169. if (oldFilePath.ToLower() == filePath.ToLower())
  170. {
  171. return;
  172. }
  173. }
  174. passwordViewer = new PDFViewControl();
  175. passwordViewer.InitDocument(filePath);
  176. if (passwordViewer.PDFToolManager.GetDocument() == null)
  177. {
  178. MessageBox.Show("Open File Failed");
  179. return;
  180. }
  181. if (passwordViewer.PDFToolManager.GetDocument().IsLocked)
  182. {
  183. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  184. PasswordUI.ClearPassword();
  185. PopupBorder.Visibility = Visibility.Visible;
  186. PasswordUI.Visibility = Visibility.Visible;
  187. }
  188. else
  189. {
  190. pdfViewer?.PDFToolManager?.GetDocument().Release();
  191. pdfViewer = passwordViewer;
  192. LoadDocument();
  193. }
  194. }
  195. }
  196. #endregion
  197. #region Password
  198. private void PasswordUI_Confirmed(object sender, string e)
  199. {
  200. if (passwordViewer != null && passwordViewer.PDFToolManager != null && passwordViewer.PDFToolManager.GetDocument() != null)
  201. {
  202. passwordViewer.PDFToolManager.GetDocument().UnlockWithPassword(e);
  203. if (passwordViewer.PDFToolManager.GetDocument().IsLocked == false)
  204. {
  205. PasswordUI.SetShowError("", Visibility.Collapsed);
  206. PasswordUI.ClearPassword();
  207. PasswordUI.Visibility = Visibility.Collapsed;
  208. PopupBorder.Visibility = Visibility.Collapsed;
  209. pdfViewer = passwordViewer;
  210. LoadDocument();
  211. }
  212. else
  213. {
  214. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  215. }
  216. }
  217. }
  218. private void PasswordUI_Canceled(object sender, EventArgs e)
  219. {
  220. PopupBorder.Visibility = Visibility.Collapsed;
  221. PasswordUI.Visibility = Visibility.Collapsed;
  222. }
  223. private void PasswordUI_Closed(object sender, EventArgs e)
  224. {
  225. PopupBorder.Visibility = Visibility.Collapsed;
  226. PasswordUI.Visibility = Visibility.Collapsed;
  227. }
  228. #endregion
  229. #region Load Unload custom control
  230. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  231. {
  232. pdfViewer = new PDFViewControl();
  233. LoadDefaultDocument();
  234. }
  235. private void LoadCustomControl()
  236. {
  237. regularViewerControl.InitWithPDFViewer(pdfViewer);
  238. regularViewerControl.PdfViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
  239. regularViewerControl.SetBOTAContainer(null);
  240. regularViewerControl.SetBOTAContainer(botaBarControl);
  241. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  242. PDFGrid.Child = regularViewerControl;
  243. }
  244. #endregion
  245. #region Event handle
  246. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  247. {
  248. if (e.Key == "Zoom")
  249. {
  250. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  251. }
  252. }
  253. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  254. {
  255. if (e.PropertyName == "RightPanel")
  256. {
  257. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  258. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  259. }
  260. }
  261. private void AnnotationControl_OnCanSaveChanged(object sender, bool e)
  262. {
  263. this.CanSave = e;
  264. }
  265. private void PdfAnnotationControl_RefreshAnnotList(object sender, EventArgs e)
  266. {
  267. botaBarControl.LoadAnnotationList();
  268. }
  269. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  270. {
  271. SaveFile();
  272. pdfViewer.PDFViewTool.IsDocumentModified = false;
  273. }
  274. private void OpenFile_Click(object sender, RoutedEventArgs e)
  275. {
  276. OpenFile();
  277. }
  278. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  279. {
  280. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  281. }
  282. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  283. {
  284. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  285. if (item.Content as string == currentMode)
  286. {
  287. return;
  288. }
  289. ClearPanelState();
  290. if (currentMode == "Viewer")
  291. {
  292. regularViewerControl.ClearViewerControl();
  293. }
  294. else if (currentMode == "Annotation")
  295. {
  296. annotationControl.ClearViewerControl();
  297. }
  298. if (item.Content as string == "Viewer")
  299. {
  300. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFViewTool != null)
  301. {
  302. PDFGrid.Child = regularViewerControl;
  303. regularViewerControl.PdfViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
  304. regularViewerControl.InitWithPDFViewer(pdfViewer);
  305. regularViewerControl.SetBOTAContainer(botaBarControl);
  306. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  307. }
  308. }
  309. else if (item.Content as string == "Annotation")
  310. {
  311. if (annotationControl.PDFViewControl != null && annotationControl.PDFViewControl.PDFViewTool != null)
  312. {
  313. PDFGrid.Child = annotationControl;
  314. annotationControl.PDFViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
  315. annotationControl.InitWithPDFViewer(pdfViewer);
  316. annotationControl.SetBOTAContainer(botaBarControl);
  317. annotationControl.SetDisplaySettingsControl(displaySettingsControl);
  318. }
  319. }
  320. currentMode = item.Content as string;
  321. }
  322. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  323. {
  324. PasswordUI.Visibility = Visibility.Collapsed;
  325. FileInfoUI.Visibility = Visibility.Visible;
  326. FileInfoControl.InitWithPDFViewer(pdfViewer);
  327. PopupBorder.Visibility = Visibility.Visible;
  328. }
  329. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  330. {
  331. panelState.RightPanel =
  332. ((sender as ToggleButton).IsChecked == true) ?
  333. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  334. }
  335. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  336. {
  337. panelState.RightPanel =
  338. ((sender as ToggleButton).IsChecked == true) ?
  339. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  340. }
  341. private void ClearPanelState()
  342. {
  343. LeftToolPanelButtonIsChecked = false;
  344. ViewSettingBtnIsChecked = false;
  345. RightToolPanelButtonIsChecked = false;
  346. }
  347. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  348. {
  349. LeftToolPanelButton.IsChecked = true;
  350. LeftToolPanelButtonIsChecked = true;
  351. botaBarControl.SelectBotaTool(BOTATools.Search);
  352. }
  353. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  354. {
  355. PopupBorder.Visibility = Visibility.Collapsed;
  356. }
  357. public event PropertyChangedEventHandler PropertyChanged;
  358. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  359. {
  360. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  361. }
  362. #endregion
  363. #region Save file
  364. /// <summary>
  365. /// Save the file to another PDF file.
  366. /// </summary>
  367. public void SaveAsFile()
  368. {
  369. {
  370. if (pdfViewer != null && pdfViewer.PDFToolManager != null && pdfViewer.PDFToolManager.GetDocument() != null)
  371. {
  372. CPDFDocument pdfDoc = pdfViewer.PDFToolManager.GetDocument();
  373. SaveFileDialog saveDialog = new SaveFileDialog();
  374. saveDialog.Filter = "(*.pdf)|*.pdf";
  375. saveDialog.DefaultExt = ".pdf";
  376. saveDialog.OverwritePrompt = true;
  377. if (saveDialog.ShowDialog() == true)
  378. {
  379. pdfDoc.WriteToFilePath(saveDialog.FileName);
  380. }
  381. }
  382. }
  383. }
  384. /// <summary>
  385. /// Save the file in the current path.
  386. /// </summary>
  387. private void SaveFile()
  388. {
  389. if (pdfViewer != null && pdfViewer.PDFToolManager != null && pdfViewer.PDFToolManager.GetDocument() != null)
  390. {
  391. try
  392. {
  393. CPDFDocument pdfDoc = pdfViewer.PDFToolManager.GetDocument();
  394. if (pdfDoc.WriteToLoadedPath())
  395. {
  396. return;
  397. }
  398. SaveFileDialog saveDialog = new SaveFileDialog();
  399. saveDialog.Filter = "(*.pdf)|*.pdf";
  400. saveDialog.DefaultExt = ".pdf";
  401. saveDialog.OverwritePrompt = true;
  402. if (saveDialog.ShowDialog() == true)
  403. {
  404. pdfDoc.WriteToFilePath(saveDialog.FileName);
  405. }
  406. }
  407. catch (Exception ex)
  408. {
  409. }
  410. }
  411. }
  412. #endregion
  413. }
  414. }