MainWindow.xaml.cs 16 KB

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