MainWindow.xaml.cs 16 KB

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