MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. using Compdfkit_Tools.PDFControlUI;
  15. using ComPDFKit.PDFPage;
  16. using ComPDFKit.Tool;
  17. namespace ContentEditorViewControl
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow: Window, INotifyPropertyChanged
  23. {
  24. #region Properties
  25. private PanelState panelState = PanelState.GetInstance();
  26. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  27. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  28. private PDFViewControl viewControl;
  29. private PDFViewControl passwordViewer;
  30. private ContentEditControl contentEditControl = new ContentEditControl();
  31. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
  32. public event PropertyChangedEventHandler PropertyChanged;
  33. private string currentMode = "Viewer";
  34. private bool _canSave = false;
  35. public bool CanSave
  36. {
  37. get => _canSave;
  38. set
  39. {
  40. _canSave = value;
  41. OnPropertyChanged();
  42. }
  43. }
  44. public bool LeftToolPanelButtonIsChecked
  45. {
  46. get => panelState.IsLeftPanelExpand;
  47. set
  48. {
  49. panelState.IsLeftPanelExpand = value;
  50. OnPropertyChanged();
  51. }
  52. }
  53. public bool ViewSettingBtnIsChecked
  54. {
  55. get
  56. {
  57. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  58. }
  59. set
  60. {
  61. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  62. OnPropertyChanged();
  63. }
  64. }
  65. public bool RightToolPanelButtonIsChecked
  66. {
  67. get
  68. {
  69. return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
  70. }
  71. set
  72. {
  73. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  74. OnPropertyChanged();
  75. }
  76. }
  77. public string AppInfo
  78. {
  79. get { return Assembly.GetExecutingAssembly().GetName().Name + " " + string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3)); }
  80. }
  81. #endregion
  82. public MainWindow()
  83. {
  84. InitializeComponent();
  85. DataContext = this;
  86. }
  87. #region Load document
  88. private void LoadDefaultDocument()
  89. {
  90. string defaultFilePath = "ComPDFKit_Sample_File_Windows.pdf";
  91. viewControl.InitDocument(defaultFilePath);
  92. LoadDocument();
  93. }
  94. private void LoadDocument()
  95. {
  96. PDFGrid.Child = contentEditControl;
  97. regularViewerControl.PdfViewControl = viewControl;
  98. contentEditControl.PdfViewControl = viewControl;
  99. contentEditControl.InitWithPDFViewer(viewControl);
  100. InitialPDFViewControl();
  101. contentEditControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  102. contentEditControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  103. contentEditControl.OnAnnotEditHandler -= PdfContentEditControlRefreshAnnotList;
  104. contentEditControl.OnAnnotEditHandler += PdfContentEditControlRefreshAnnotList;
  105. PasswordUI.Closed -= PasswordUI_Closed;
  106. PasswordUI.Canceled -= PasswordUI_Canceled;
  107. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  108. PasswordUI.Closed += PasswordUI_Closed;
  109. PasswordUI.Canceled += PasswordUI_Canceled;
  110. PasswordUI.Confirmed += PasswordUI_Confirmed;
  111. ViewComboBox.SelectedIndex = 1;
  112. contentEditControl.PdfViewControl.GetCPDFViewer().SetFitMode(FitMode.FitWidth);
  113. CPDFSaclingControl.InitWithPDFViewer(contentEditControl.PdfViewControl);
  114. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(contentEditControl.PdfViewControl.GetCPDFViewer().GetZoom() * 100)));
  115. ViewSettingBtn.IsChecked = false;
  116. botaBarControl.InitWithPDFViewer(contentEditControl.PdfViewControl);
  117. botaBarControl.AddBOTAContent(new[] { BOTATools.Thumbnail, BOTATools.Outline, BOTATools.Bookmark, BOTATools.Annotation, BOTATools.Search });
  118. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  119. contentEditControl.SetBOTAContainer(botaBarControl);
  120. displaySettingsControl.InitWithPDFViewer(contentEditControl.PdfViewControl);
  121. panelState.PropertyChanged -= PanelState_PropertyChanged;
  122. panelState.PropertyChanged += PanelState_PropertyChanged;
  123. }
  124. private void OpenFile()
  125. {
  126. string filePath = CommonHelper.GetExistedPathOrEmpty();
  127. if (!string.IsNullOrEmpty(filePath) && contentEditControl.PdfViewControl != null)
  128. {
  129. if (viewControl.PDFToolManager != null && viewControl.PDFToolManager.GetDocument() != null)
  130. {
  131. string oldFilePath = viewControl.PDFToolManager.GetDocument().FilePath;
  132. if (oldFilePath.ToLower() == filePath.ToLower())
  133. {
  134. return;
  135. }
  136. }
  137. passwordViewer = new PDFViewControl();
  138. passwordViewer.InitDocument(filePath);
  139. if (passwordViewer.PDFToolManager.GetDocument() == null)
  140. {
  141. MessageBox.Show("Open File Failed");
  142. return;
  143. }
  144. if (passwordViewer.PDFToolManager.GetDocument().IsLocked)
  145. {
  146. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  147. PasswordUI.ClearPassword();
  148. PopupBorder.Visibility = Visibility.Visible;
  149. PasswordUI.Visibility = Visibility.Visible;
  150. }
  151. else
  152. {
  153. viewControl.PDFToolManager.GetDocument().Release();
  154. viewControl = passwordViewer;
  155. LoadDocument();
  156. }
  157. }
  158. }
  159. #endregion
  160. #region Password
  161. private void PasswordUI_Confirmed(object sender, string e)
  162. {
  163. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  164. {
  165. CPDFViewer pdfViewer=passwordViewer.PDFViewTool.GetCPDFViewer();
  166. passwordViewer.PDFToolManager.GetDocument().UnlockWithPassword(e);
  167. if (passwordViewer.PDFToolManager.GetDocument().IsLocked == false)
  168. {
  169. PasswordUI.SetShowError("", Visibility.Collapsed);
  170. PasswordUI.ClearPassword();
  171. PasswordUI.Visibility = Visibility.Collapsed;
  172. PopupBorder.Visibility = Visibility.Collapsed;
  173. viewControl = passwordViewer;
  174. LoadDocument();
  175. }
  176. else
  177. {
  178. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  179. }
  180. }
  181. }
  182. private void PasswordUI_Canceled(object sender, EventArgs e)
  183. {
  184. PopupBorder.Visibility = Visibility.Collapsed;
  185. PasswordUI.Visibility = Visibility.Collapsed;
  186. }
  187. private void PasswordUI_Closed(object sender, EventArgs e)
  188. {
  189. PopupBorder.Visibility = Visibility.Collapsed;
  190. PasswordUI.Visibility = Visibility.Collapsed;
  191. }
  192. #endregion
  193. #region Load Unload custom control
  194. private void Window_Loaded(object sender, RoutedEventArgs e)
  195. {
  196. viewControl = new PDFViewControl();
  197. LoadDefaultDocument();
  198. }
  199. #endregion
  200. #region Annotation
  201. private void InitialPDFViewControl()
  202. {
  203. contentEditControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  204. }
  205. #endregion
  206. #region Event handle
  207. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  208. {
  209. if (e.Key == "Zoom")
  210. {
  211. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  212. }
  213. }
  214. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  215. {
  216. SaveFile();
  217. }
  218. private void OpenFile_Click(object sender, RoutedEventArgs e)
  219. {
  220. OpenFile();
  221. }
  222. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  223. {
  224. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  225. contentEditControl.PdfViewControl.GetCPDFViewer().GoToPage(PageIndex: 1, new Point(100, 100));
  226. }
  227. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  228. {
  229. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  230. if (item.Content as string == currentMode)
  231. {
  232. return;
  233. }
  234. ClearPanelState();
  235. if (currentMode == "Viewer")
  236. {
  237. regularViewerControl.ClearViewerControl();
  238. }
  239. else if (currentMode == "Content Edit")
  240. {
  241. contentEditControl.ClearViewerControl();
  242. }
  243. if ((string)item.Content == "Viewer")
  244. {
  245. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFViewTool != null)
  246. {
  247. PDFGrid.Child = regularViewerControl;
  248. regularViewerControl.PdfViewControl.PDFViewTool.SetViewerModel(MouseModes.Viewer);
  249. regularViewerControl.PdfViewControl = viewControl;
  250. regularViewerControl.InitWithPDFViewer(viewControl);
  251. regularViewerControl.SetBOTAContainer(botaBarControl);
  252. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  253. }
  254. }
  255. else if ((string)item.Content == "Content Edit")
  256. {
  257. contentEditControl.PdfViewControl = viewControl;
  258. contentEditControl.InitWithPDFViewer(viewControl);
  259. displaySettingsControl.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  260. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl!= null)
  261. {
  262. PDFGrid.Child = contentEditControl;
  263. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.ContentEdit);
  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. }