MainWindow.xaml.cs 15 KB

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