MainWindow.xaml.cs 15 KB

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