MainWindow.xaml.cs 14 KB

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