MainWindow.xaml.cs 15 KB

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