MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using ComPDFKit.Controls.Helper;
  2. using ComPDFKit.Controls.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.Controls.PDFView;
  14. using ComPDFKitViewer;
  15. using ComPDFKit.PDFDocument;
  16. using ComPDFKit.Tool;
  17. namespace FormViewControl
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow: Window, INotifyPropertyChanged
  23. {
  24. #region Properties
  25. private PDFViewControl pdfViewer;
  26. private PDFViewControl passwordViewer;
  27. private FormControl formControl = new FormControl();
  28. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  29. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  30. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
  31. private PanelState panelState = PanelState.GetInstance();
  32. private string currentMode = "Viewer";
  33. public event PropertyChangedEventHandler PropertyChanged;
  34. private bool _canSave = false;
  35. public bool CanSave
  36. {
  37. get => _canSave;
  38. set
  39. {
  40. _canSave = value;
  41. OnPropertyChanged();
  42. }
  43. }
  44. public bool LeftToolPanelButtonIsChecked
  45. {
  46. get => panelState.IsLeftPanelExpand;
  47. set
  48. {
  49. panelState.IsLeftPanelExpand = value;
  50. OnPropertyChanged();
  51. }
  52. }
  53. public bool RightToolPanelButtonIsChecked
  54. {
  55. get
  56. {
  57. return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
  58. }
  59. set
  60. {
  61. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  62. OnPropertyChanged();
  63. }
  64. }
  65. public bool ViewSettingBtnIsChecked
  66. {
  67. get
  68. {
  69. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  70. }
  71. set
  72. {
  73. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  74. OnPropertyChanged();
  75. }
  76. }
  77. public string AppInfo
  78. {
  79. get { return Assembly.GetExecutingAssembly().GetName().Name + " " + string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3)); }
  80. }
  81. #endregion
  82. public MainWindow()
  83. {
  84. InitializeComponent();
  85. DataContext = this;
  86. }
  87. #region Load document
  88. private void LoadDefaultDocument()
  89. {
  90. string defaultFilePath = "ComPDFKit_Forms_Sample_File.pdf";
  91. pdfViewer.InitDocument(defaultFilePath);
  92. LoadDocument();
  93. }
  94. private void LoadDocument()
  95. {
  96. formControl.PdfViewControl = pdfViewer;
  97. regularViewerControl.PdfViewControl = pdfViewer;
  98. formControl.InitWithPDFViewer(pdfViewer);
  99. formControl.ClearAllToolState();
  100. formControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  101. formControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  102. formControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  103. PasswordUI.Closed -= PasswordUI_Closed;
  104. PasswordUI.Canceled -= PasswordUI_Canceled;
  105. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  106. PasswordUI.Closed += PasswordUI_Closed;
  107. PasswordUI.Canceled += PasswordUI_Canceled;
  108. PasswordUI.Confirmed += PasswordUI_Confirmed;
  109. ModeComboBox.SelectedIndex = 1;
  110. formControl.SetToolBarContainerVisibility(Visibility.Visible);
  111. formControl.PdfViewControl.PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitWidth);
  112. CPDFSaclingControl.InitWithPDFViewer(formControl.PdfViewControl);
  113. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(formControl.PdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() * 100)));
  114. ViewSettingBtn.IsChecked = false;
  115. botaBarControl.InitWithPDFViewer(formControl.PdfViewControl);
  116. botaBarControl.AddBOTAContent(new []{BOTATools.Thumbnail , BOTATools.Outline , BOTATools.Bookmark , BOTATools.Annotation , BOTATools.Search});
  117. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  118. formControl.SetBOTAContainer(botaBarControl);
  119. formControl.InitialPDFViewControl(formControl.PdfViewControl);
  120. panelState.PropertyChanged -= PanelState_PropertyChanged;
  121. panelState.PropertyChanged += PanelState_PropertyChanged;
  122. regularViewerControl.PdfViewControl.PDFViewTool.DocumentModifiedChanged -= PDFViewTool_DocumentModifiedChanged;
  123. regularViewerControl.PdfViewControl.PDFViewTool.DocumentModifiedChanged += PDFViewTool_DocumentModifiedChanged;
  124. }
  125. private void PDFViewTool_DocumentModifiedChanged(object sender, EventArgs e)
  126. {
  127. CanSave = regularViewerControl.PdfViewControl.PDFViewTool.IsDocumentModified;
  128. }
  129. private void OpenFile()
  130. {
  131. string filePath = CommonHelper.GetExistedPathOrEmpty();
  132. if (!string.IsNullOrEmpty(filePath) && formControl.PdfViewControl != null)
  133. {
  134. if (pdfViewer.PDFViewTool != null && pdfViewer.PDFToolManager.GetDocument() != null)
  135. {
  136. string oldFilePath = pdfViewer.PDFToolManager.GetDocument().FilePath;
  137. if (oldFilePath.ToLower() == filePath.ToLower())
  138. {
  139. return;
  140. }
  141. }
  142. passwordViewer = new PDFViewControl();
  143. passwordViewer.InitDocument(filePath);
  144. if (passwordViewer.PDFToolManager.GetDocument() == null)
  145. {
  146. MessageBox.Show("Open File Failed");
  147. return;
  148. }
  149. if (passwordViewer.PDFToolManager.GetDocument().IsLocked)
  150. {
  151. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  152. PasswordUI.ClearPassword();
  153. PopupBorder.Visibility = Visibility.Visible;
  154. PasswordUI.Visibility = Visibility.Visible;
  155. }
  156. else
  157. {
  158. passwordViewer.PDFToolManager.GetDocument().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.PDFViewTool != null && passwordViewer.PDFToolManager.GetDocument() != null)
  173. {
  174. passwordViewer.PDFToolManager.GetDocument().UnlockWithPassword(e);
  175. if (passwordViewer.PDFToolManager.GetDocument().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. 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. bool T = false;
  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.PDFToolManager != null)
  249. {
  250. PDFGrid.Child = regularViewerControl;
  251. regularViewerControl.PdfViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
  252. regularViewerControl.PdfViewControl = pdfViewer;
  253. regularViewerControl.InitWithPDFViewer(pdfViewer);
  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.PDFToolManager != null)
  261. {
  262. PDFGrid.Child = formControl;
  263. if (T)
  264. {
  265. formControl.PdfViewControl.PDFToolManager.SetToolType(ToolType.WidgetEdit);
  266. }
  267. if (IsLoaded)
  268. {
  269. T = IsLoaded;
  270. }
  271. formControl.PdfViewControl = pdfViewer;
  272. formControl.InitWithPDFViewer(pdfViewer);
  273. formControl.SetBOTAContainer(botaBarControl);
  274. formControl.SetDisplaySettingsControl(displaySettingsControl);
  275. }
  276. }
  277. currentMode = item.Content as string;
  278. }
  279. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  280. {
  281. PasswordUI.Visibility = Visibility.Collapsed;
  282. FileInfoUI.Visibility = Visibility.Visible;
  283. FileInfoControl.InitWithPDFViewer(pdfViewer);
  284. PopupBorder.Visibility = Visibility.Visible;
  285. }
  286. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  287. {
  288. panelState.RightPanel =
  289. ((sender as ToggleButton).IsChecked == true) ?
  290. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  291. }
  292. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  293. {
  294. panelState.RightPanel =
  295. ((sender as ToggleButton).IsChecked == true) ?
  296. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  297. }
  298. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  299. {
  300. LeftToolPanelButton.IsChecked = true;
  301. botaBarControl.SelectBotaTool(BOTATools.Search);
  302. }
  303. private void ClearPanelState()
  304. {
  305. LeftToolPanelButtonIsChecked = false;
  306. ViewSettingBtnIsChecked = false;
  307. RightToolPanelButtonIsChecked = false;
  308. }
  309. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  310. {
  311. PopupBorder.Visibility = Visibility.Collapsed;
  312. }
  313. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  314. {
  315. if (e.PropertyName == "RightPanel")
  316. {
  317. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  318. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  319. }
  320. }
  321. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  322. {
  323. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  324. }
  325. #endregion
  326. #region Save file
  327. /// <summary>
  328. /// Save the file to another PDF file.
  329. /// </summary>
  330. public void SaveAsFile()
  331. {
  332. {
  333. if (pdfViewer != null && pdfViewer.PDFToolManager != null && pdfViewer.PDFToolManager.GetDocument() != null)
  334. {
  335. CPDFDocument pdfDoc = pdfViewer.PDFToolManager.GetDocument();
  336. SaveFileDialog saveDialog = new SaveFileDialog();
  337. saveDialog.Filter = "(*.pdf)|*.pdf";
  338. saveDialog.DefaultExt = ".pdf";
  339. saveDialog.OverwritePrompt = true;
  340. if (saveDialog.ShowDialog() == true)
  341. {
  342. pdfDoc.WriteToFilePath(saveDialog.FileName);
  343. }
  344. }
  345. }
  346. }
  347. /// <summary>
  348. /// Save the file in the current path.
  349. /// </summary>
  350. private void SaveFile()
  351. {
  352. if (pdfViewer != null && pdfViewer.PDFToolManager != null && pdfViewer.PDFToolManager.GetDocument() != null)
  353. {
  354. try
  355. {
  356. CPDFDocument pdfDoc = pdfViewer.PDFToolManager.GetDocument();
  357. if (pdfDoc.WriteToLoadedPath())
  358. {
  359. return;
  360. }
  361. SaveFileDialog saveDialog = new SaveFileDialog();
  362. saveDialog.Filter = "(*.pdf)|*.pdf";
  363. saveDialog.DefaultExt = ".pdf";
  364. saveDialog.OverwritePrompt = true;
  365. if (saveDialog.ShowDialog() == true)
  366. {
  367. pdfDoc.WriteToFilePath(saveDialog.FileName);
  368. }
  369. }
  370. catch (Exception ex)
  371. {
  372. }
  373. }
  374. }
  375. #endregion
  376. }
  377. }