MainWindow.xaml.cs 16 KB

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