MainWindow.xaml.cs 16 KB

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