MainPage.xaml.cs 18 KB

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