MainWindow.xaml.cs 16 KB

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