MainWindow.xaml.cs 15 KB

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