MainWindow.xaml.cs 16 KB

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