MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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();
  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 = "ComPDFKit_Sample_File_Windows.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.AddBOTAContent(new[] { BOTATools.Thumbnail, BOTATools.Outline, BOTATools.Bookmark, BOTATools.Annotation, BOTATools.Search });
  115. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  116. contentEditControl.SetBOTAContainer(botaBarControl);
  117. displaySettingsControl.InitWithPDFViewer(contentEditControl.PdfViewControl.PDFView);
  118. panelState.PropertyChanged -= PanelState_PropertyChanged;
  119. panelState.PropertyChanged += PanelState_PropertyChanged;
  120. }
  121. private void OpenFile()
  122. {
  123. string filePath = CommonHelper.GetExistedPathOrEmpty();
  124. if (!string.IsNullOrEmpty(filePath) && contentEditControl.PdfViewControl != null)
  125. {
  126. if (viewControl.PDFView != null && viewControl.PDFView.Document != null)
  127. {
  128. string oldFilePath = viewControl.PDFView.Document.FilePath;
  129. if (oldFilePath.ToLower() == filePath.ToLower())
  130. {
  131. return;
  132. }
  133. }
  134. passwordViewer = new PDFViewControl();
  135. passwordViewer.PDFView.InitDocument(filePath);
  136. if (passwordViewer.PDFView.Document == null)
  137. {
  138. MessageBox.Show("Open File Failed");
  139. return;
  140. }
  141. if (passwordViewer.PDFView.Document.IsLocked)
  142. {
  143. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  144. PasswordUI.ClearPassword();
  145. PopupBorder.Visibility = Visibility.Visible;
  146. PasswordUI.Visibility = Visibility.Visible;
  147. }
  148. else
  149. {
  150. viewControl.PDFView.Document.Release();
  151. viewControl = passwordViewer;
  152. LoadDocument();
  153. }
  154. }
  155. }
  156. #endregion
  157. #region Password
  158. private void PasswordUI_Confirmed(object sender, string e)
  159. {
  160. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  161. {
  162. CPDFViewer pdfViewer=passwordViewer.PDFViewTool.GetCPDFViewer();
  163. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  164. if (passwordViewer.PDFView.Document.IsLocked == false)
  165. {
  166. PasswordUI.SetShowError("", Visibility.Collapsed);
  167. PasswordUI.ClearPassword();
  168. PasswordUI.Visibility = Visibility.Collapsed;
  169. PopupBorder.Visibility = Visibility.Collapsed;
  170. viewControl = passwordViewer;
  171. LoadDocument();
  172. }
  173. else
  174. {
  175. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  176. }
  177. }
  178. }
  179. private void PasswordUI_Canceled(object sender, EventArgs e)
  180. {
  181. PopupBorder.Visibility = Visibility.Collapsed;
  182. PasswordUI.Visibility = Visibility.Collapsed;
  183. }
  184. private void PasswordUI_Closed(object sender, EventArgs e)
  185. {
  186. PopupBorder.Visibility = Visibility.Collapsed;
  187. PasswordUI.Visibility = Visibility.Collapsed;
  188. }
  189. #endregion
  190. #region Load Unload custom control
  191. private void Window_Loaded(object sender, RoutedEventArgs e)
  192. {
  193. viewControl = new PDFViewControl();
  194. LoadDefaultDocument();
  195. }
  196. #endregion
  197. #region Annotation
  198. private void InitialPDFViewControl()
  199. {
  200. contentEditControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  201. }
  202. #endregion
  203. #region Event handle
  204. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  205. {
  206. if (e.Key == "Zoom")
  207. {
  208. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  209. }
  210. }
  211. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  212. {
  213. SaveFile();
  214. }
  215. private void OpenFile_Click(object sender, RoutedEventArgs e)
  216. {
  217. OpenFile();
  218. }
  219. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  220. {
  221. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  222. pdfViewer.PDFView.GoToPage(pageIndex: 1, new Point(100, 100));
  223. }
  224. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  225. {
  226. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  227. if (item.Content as string == currentMode)
  228. {
  229. return;
  230. }
  231. ClearPanelState();
  232. if (currentMode == "Viewer")
  233. {
  234. // regularViewerControl.ClearViewerControl();
  235. }
  236. else if (currentMode == "Content Edit")
  237. {
  238. contentEditControl.ClearViewerControl();
  239. }
  240. if ((string)item.Content == "Viewer")
  241. {
  242. //if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
  243. //{
  244. // PDFGrid.Child = regularViewerControl;
  245. // regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  246. // regularViewerControl.PdfViewControl = viewControl;
  247. // regularViewerControl.InitWithPDFViewer(viewControl.PDFView);
  248. // regularViewerControl.SetBOTAContainer(botaBarControl);
  249. // regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  250. //}
  251. }
  252. else if ((string)item.Content == "Content Edit")
  253. {
  254. //if (contentEditControl.PdfViewControl != null && contentEditControl.PdfViewControl.PDFView != null)
  255. //{
  256. // viewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  257. // viewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  258. // viewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  259. // viewControl.PDFView?.SetSplitMode(SplitMode.None);
  260. // PDFGrid.Child = contentEditControl;
  261. // contentEditControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.PDFEdit);
  262. // contentEditControl.PdfViewControl = viewControl;
  263. // contentEditControl.InitWithPDFViewer(viewControl.PDFView);
  264. // contentEditControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  265. // contentEditControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  266. // contentEditControl.SetBOTAContainer(botaBarControl);
  267. // contentEditControl.SetDisplaySettingsControl(displaySettingsControl);
  268. //}
  269. }
  270. currentMode = item.Content as string;
  271. }
  272. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  273. {
  274. PasswordUI.Visibility = Visibility.Collapsed;
  275. FileInfoUI.Visibility = Visibility.Visible;
  276. FileInfoControl.InitWithPDFViewer(viewControl);
  277. PopupBorder.Visibility = Visibility.Visible;
  278. }
  279. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  280. {
  281. panelState.RightPanel =
  282. ((sender as ToggleButton).IsChecked == true) ?
  283. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  284. }
  285. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  286. {
  287. panelState.RightPanel =
  288. ((sender as ToggleButton).IsChecked == true) ?
  289. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  290. }
  291. private void ClearPanelState()
  292. {
  293. LeftToolPanelButtonIsChecked = false;
  294. ViewSettingBtnIsChecked = false;
  295. RightToolPanelButtonIsChecked = false;
  296. }
  297. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  298. {
  299. LeftToolPanelButton.IsChecked = true;
  300. LeftToolPanelButtonIsChecked = true;
  301. botaBarControl.SelectBotaTool(BOTATools.Search);
  302. }
  303. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  304. {
  305. if (e.PropertyName == "RightPanel")
  306. {
  307. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  308. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  309. }
  310. }
  311. private void PdfContentEditControlRefreshAnnotList(object sender, EventArgs e)
  312. {
  313. botaBarControl.LoadAnnotationList();
  314. }
  315. private void ControlOnCanSaveChanged(object sender, bool e)
  316. {
  317. this.CanSave = e;
  318. }
  319. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  320. {
  321. PopupBorder.Visibility = Visibility.Collapsed;
  322. }
  323. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  324. {
  325. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  326. }
  327. #endregion
  328. #region Save file
  329. /// <summary>
  330. /// Save the file to another PDF file.
  331. /// </summary>
  332. public void SaveAsFile()
  333. {
  334. if (viewControl != null && viewControl.PDFViewTool != null )
  335. {
  336. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  337. CPDFDocument pdfDoc= pdfViewer?.GetDocument();
  338. if (pdfDoc == null)
  339. {
  340. return;
  341. }
  342. SaveFileDialog saveDialog = new SaveFileDialog();
  343. saveDialog.Filter = "(*.pdf)|*.pdf";
  344. saveDialog.DefaultExt = ".pdf";
  345. saveDialog.OverwritePrompt = true;
  346. if (saveDialog.ShowDialog() == true)
  347. {
  348. pdfDoc.WriteToFilePath(saveDialog.FileName);
  349. }
  350. }
  351. }
  352. /// <summary>
  353. /// Save the file in the current path.
  354. /// </summary>
  355. private void SaveFile()
  356. {
  357. if (viewControl != null && viewControl.PDFViewTool != null)
  358. {
  359. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  360. if (pdfViewer == null)
  361. {
  362. return;
  363. }
  364. try
  365. {
  366. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  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. }