MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. }
  233. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  234. {
  235. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  236. if (item.Content as string == currentMode)
  237. {
  238. return;
  239. }
  240. ClearPanelState();
  241. if (currentMode == "Viewer")
  242. {
  243. regularViewerControl.ClearViewerControl();
  244. }
  245. else if (currentMode == "Content Edit")
  246. {
  247. contentEditControl.ClearViewerControl();
  248. }
  249. if ((string)item.Content == "Viewer")
  250. {
  251. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFViewTool != null)
  252. {
  253. PDFGrid.Child = regularViewerControl;
  254. regularViewerControl.PdfViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
  255. regularViewerControl.PdfViewControl = viewControl;
  256. regularViewerControl.InitWithPDFViewer(viewControl);
  257. regularViewerControl.SetBOTAContainer(botaBarControl);
  258. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  259. }
  260. }
  261. else if ((string)item.Content == "Content Edit")
  262. {
  263. contentEditControl.PdfViewControl = viewControl;
  264. contentEditControl.InitWithPDFViewer(viewControl);
  265. displaySettingsControl.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  266. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl!= null)
  267. {
  268. PDFGrid.Child = contentEditControl;
  269. viewControl.SetToolType(ToolType.ContentEdit);
  270. contentEditControl.SetBOTAContainer(botaBarControl);
  271. contentEditControl.SetDisplaySettingsControl(displaySettingsControl);
  272. }
  273. }
  274. currentMode = item.Content as string;
  275. }
  276. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  277. {
  278. PasswordUI.Visibility = Visibility.Collapsed;
  279. FileInfoUI.Visibility = Visibility.Visible;
  280. FileInfoControl.InitWithPDFViewer(viewControl);
  281. PopupBorder.Visibility = Visibility.Visible;
  282. }
  283. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  284. {
  285. panelState.RightPanel =
  286. ((sender as ToggleButton).IsChecked == true) ?
  287. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  288. }
  289. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  290. {
  291. panelState.RightPanel =
  292. ((sender as ToggleButton).IsChecked == true) ?
  293. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  294. }
  295. private void ClearPanelState()
  296. {
  297. LeftToolPanelButtonIsChecked = false;
  298. ViewSettingBtnIsChecked = false;
  299. RightToolPanelButtonIsChecked = false;
  300. }
  301. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  302. {
  303. LeftToolPanelButton.IsChecked = true;
  304. LeftToolPanelButtonIsChecked = true;
  305. botaBarControl.SelectBotaTool(BOTATools.Search);
  306. }
  307. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  308. {
  309. if (e.PropertyName == "RightPanel")
  310. {
  311. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  312. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  313. }
  314. }
  315. private void PdfContentEditControlRefreshAnnotList(object sender, EventArgs e)
  316. {
  317. botaBarControl.LoadAnnotationList();
  318. }
  319. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  320. {
  321. PopupBorder.Visibility = Visibility.Collapsed;
  322. }
  323. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  324. {
  325. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  326. }
  327. #endregion
  328. #region Save file
  329. /// <summary>
  330. /// Save the file to another PDF file.
  331. /// </summary>
  332. public void SaveAsFile()
  333. {
  334. if (viewControl != null && viewControl.PDFViewTool != null )
  335. {
  336. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  337. CPDFDocument pdfDoc= pdfViewer?.GetDocument();
  338. if (pdfDoc == null)
  339. {
  340. return;
  341. }
  342. SaveFileDialog saveDialog = new SaveFileDialog();
  343. saveDialog.Filter = "(*.pdf)|*.pdf";
  344. saveDialog.DefaultExt = ".pdf";
  345. saveDialog.OverwritePrompt = true;
  346. if (saveDialog.ShowDialog() == true)
  347. {
  348. pdfDoc.WriteToFilePath(saveDialog.FileName);
  349. }
  350. }
  351. }
  352. /// <summary>
  353. /// Save the file in the current path.
  354. /// </summary>
  355. private void SaveFile()
  356. {
  357. if (viewControl != null && viewControl.PDFViewTool != null)
  358. {
  359. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  360. if (pdfViewer == null)
  361. {
  362. return;
  363. }
  364. try
  365. {
  366. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  367. if (pdfDoc.WriteToLoadedPath())
  368. {
  369. return;
  370. }
  371. SaveFileDialog saveDialog = new SaveFileDialog();
  372. saveDialog.Filter = "(*.pdf)|*.pdf";
  373. saveDialog.DefaultExt = ".pdf";
  374. saveDialog.OverwritePrompt = true;
  375. if (saveDialog.ShowDialog() == true)
  376. {
  377. pdfDoc.WriteToFilePath(saveDialog.FileName);
  378. }
  379. }
  380. catch (Exception ex)
  381. {
  382. }
  383. }
  384. }
  385. #endregion
  386. }
  387. }