MainWindow.xaml.cs 16 KB

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