MainWindow.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using Compdfkit_Tools.Helper;
  2. using Compdfkit_Tools.PDFControl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  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. namespace ContentEditorViewControl
  19. {
  20. /// <summary>
  21. /// MainWindow.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. private PDFViewControl passwordViewer;
  26. private PDFViewControl pdfViewer;
  27. private ContentEditControl contentEditControl = new ContentEditControl();
  28. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  29. public bool CanUndo
  30. {
  31. get
  32. {
  33. if (pdfViewer != null && pdfViewer.PDFView != null)
  34. {
  35. return pdfViewer.PDFView.UndoManager.CanUndo;
  36. }
  37. return false;
  38. }
  39. }
  40. public bool CanRedo
  41. {
  42. get
  43. {
  44. if (pdfViewer != null && pdfViewer.PDFView != null)
  45. {
  46. return pdfViewer.PDFView.UndoManager.CanRedo;
  47. }
  48. return false;
  49. }
  50. }
  51. public bool CanSave
  52. {
  53. get
  54. {
  55. if (pdfViewer != null && pdfViewer.PDFView != null)
  56. {
  57. return pdfViewer.PDFView.UndoManager.CanSave;
  58. }
  59. return false;
  60. }
  61. }
  62. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  63. {
  64. if (e.Key == "Zoom")
  65. {
  66. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  67. }
  68. }
  69. public MainWindow()
  70. {
  71. InitializeComponent();
  72. }
  73. #region Load Document
  74. private void LoadDocument()
  75. {
  76. if (pdfViewer.PDFView.Document == null)
  77. {
  78. return;
  79. }
  80. pdfViewer.PDFView.Load();
  81. pdfViewer.PDFView.SetShowLink(true);
  82. pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  83. pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  84. PasswordUI.Closed -= PasswordUI_Closed;
  85. PasswordUI.Canceled -= PasswordUI_Canceled;
  86. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  87. PasswordUI.Closed += PasswordUI_Closed;
  88. PasswordUI.Canceled += PasswordUI_Canceled;
  89. PasswordUI.Confirmed += PasswordUI_Confirmed;
  90. contentEditControl.PdfViewControl = pdfViewer;
  91. contentEditControl.InitWithPDFViewer(pdfViewer.PDFView);
  92. CPDFSaclingControl.InitWithPDFViewer(contentEditControl.PdfViewControl.PDFView);
  93. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(contentEditControl.PdfViewControl.PDFView.ZoomFactor * 100)));
  94. PDFGrid.Child = contentEditControl;
  95. botaBarControl.InitWithPDFViewer(contentEditControl.PdfViewControl.PDFView);
  96. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  97. contentEditControl.SetBOTAContainer(botaBarControl);
  98. contentEditControl.InitWithPDFViewer(pdfViewer.PDFView);
  99. }
  100. private void PasswordUI_Confirmed(object sender, string e)
  101. {
  102. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  103. {
  104. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  105. if (passwordViewer.PDFView.Document.IsLocked == false)
  106. {
  107. PasswordUI.SetShowError("", Visibility.Collapsed);
  108. PasswordUI.ClearPassword();
  109. PasswordUI.Visibility = Visibility.Collapsed;
  110. PopupBorder.Visibility = Visibility.Collapsed;
  111. pdfViewer = passwordViewer;
  112. LoadDocument();
  113. }
  114. else
  115. {
  116. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  117. }
  118. }
  119. }
  120. private void PasswordUI_Canceled(object sender, EventArgs e)
  121. {
  122. PopupBorder.Visibility = Visibility.Collapsed;
  123. PasswordUI.Visibility = Visibility.Collapsed;
  124. }
  125. private void PasswordUI_Closed(object sender, EventArgs e)
  126. {
  127. PopupBorder.Visibility = Visibility.Collapsed;
  128. PasswordUI.Visibility = Visibility.Collapsed;
  129. }
  130. private void LoadDefaultDocument()
  131. {
  132. string defaultFilePath = "PDF32000_2008.pdf";
  133. pdfViewer.PDFView.InitDocument(defaultFilePath);
  134. LoadDocument();
  135. }
  136. #endregion
  137. private void Window_Loaded(object sender, RoutedEventArgs e)
  138. {
  139. pdfViewer = new PDFViewControl();
  140. LoadDefaultDocument();
  141. }
  142. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  143. {
  144. PasswordUI.Visibility = Visibility.Collapsed;
  145. FileInfoUI.Visibility = Visibility.Visible;
  146. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  147. PopupBorder.Visibility = Visibility.Visible;
  148. }
  149. private void OpenFile()
  150. {
  151. string filePath = CommonHelper.GetExistedPathOrEmpty();
  152. if (!string.IsNullOrEmpty(filePath) && contentEditControl.PdfViewControl != null)
  153. {
  154. if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  155. {
  156. string oldFilePath = pdfViewer.PDFView.Document.FilePath;
  157. if (oldFilePath.ToLower() == filePath.ToLower())
  158. {
  159. return;
  160. }
  161. }
  162. passwordViewer = new PDFViewControl();
  163. passwordViewer.PDFView.InitDocument(filePath);
  164. if (passwordViewer.PDFView.Document == null)
  165. {
  166. MessageBox.Show("Open File Failed");
  167. return;
  168. }
  169. if (passwordViewer.PDFView.Document.IsLocked)
  170. {
  171. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  172. PasswordUI.ClearPassword();
  173. PopupBorder.Visibility = Visibility.Visible;
  174. PasswordUI.Visibility = Visibility.Visible;
  175. }
  176. else
  177. {
  178. pdfViewer = passwordViewer;
  179. LoadDocument();
  180. }
  181. }
  182. }
  183. private void OpenFile_Click(object sender, RoutedEventArgs e)
  184. {
  185. OpenFile();
  186. }
  187. private void ControlRightPanel()
  188. {
  189. if (RightPanelButton != null)
  190. {
  191. if (RightPanelButton.IsChecked == true)
  192. {
  193. if (contentEditControl.pdfContentEditControl != null)
  194. {
  195. contentEditControl.ExpandRightPropertyPanel(contentEditControl.pdfContentEditControl, Visibility.Visible);
  196. if ((bool)ViewSettingBtn.IsChecked)
  197. {
  198. ViewSettingBtn.IsChecked = false;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. contentEditControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  205. }
  206. }
  207. }
  208. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  209. {
  210. ControlRightPanel();
  211. }
  212. private void ShowViewSettings()
  213. {
  214. if (ViewSettingBtn != null)
  215. {
  216. if (ViewSettingBtn.IsChecked == true)
  217. {
  218. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  219. displayPanel.InitWithPDFViewer(contentEditControl.PdfViewControl.PDFView);
  220. contentEditControl.SetViewSettings(Visibility.Visible, displayPanel);
  221. if ((bool)RightPanelButton.IsChecked)
  222. {
  223. RightPanelButton.IsChecked = false;
  224. }
  225. }
  226. else
  227. {
  228. contentEditControl.SetViewSettings(Visibility.Collapsed);
  229. }
  230. }
  231. }
  232. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  233. {
  234. ShowViewSettings();
  235. }
  236. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  237. {
  238. ToggleButton expandBtn = sender as ToggleButton;
  239. if (expandBtn != null)
  240. {
  241. bool isExpand = expandBtn.IsChecked == true;
  242. contentEditControl.ExpandLeftPanel(isExpand);
  243. }
  244. }
  245. }
  246. }