MainWindow.xaml.cs 15 KB

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