MainWindow.xaml.cs 16 KB

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