MainWindow.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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();
  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. botaBarControl.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  98. contentEditControl.SetBOTAContainer(botaBarControl);
  99. contentEditControl.InitWithPDFViewer(pdfViewer.PDFView);
  100. }
  101. private void PasswordUI_Confirmed(object sender, string e)
  102. {
  103. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  104. {
  105. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  106. if (passwordViewer.PDFView.Document.IsLocked == false)
  107. {
  108. PasswordUI.SetShowError("", Visibility.Collapsed);
  109. PasswordUI.ClearPassword();
  110. PasswordUI.Visibility = Visibility.Collapsed;
  111. PopupBorder.Visibility = Visibility.Collapsed;
  112. pdfViewer = passwordViewer;
  113. LoadDocument();
  114. }
  115. else
  116. {
  117. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  118. }
  119. }
  120. }
  121. private void PasswordUI_Canceled(object sender, EventArgs e)
  122. {
  123. PopupBorder.Visibility = Visibility.Collapsed;
  124. PasswordUI.Visibility = Visibility.Collapsed;
  125. }
  126. private void PasswordUI_Closed(object sender, EventArgs e)
  127. {
  128. PopupBorder.Visibility = Visibility.Collapsed;
  129. PasswordUI.Visibility = Visibility.Collapsed;
  130. }
  131. private void LoadDefaultDocument()
  132. {
  133. string defaultFilePath = "PDF32000_2008.pdf";
  134. pdfViewer.PDFView.InitDocument(defaultFilePath);
  135. LoadDocument();
  136. }
  137. #endregion
  138. private void Window_Loaded(object sender, RoutedEventArgs e)
  139. {
  140. pdfViewer = new PDFViewControl();
  141. LoadDefaultDocument();
  142. }
  143. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  144. {
  145. PasswordUI.Visibility = Visibility.Collapsed;
  146. FileInfoUI.Visibility = Visibility.Visible;
  147. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  148. PopupBorder.Visibility = Visibility.Visible;
  149. }
  150. private void OpenFile()
  151. {
  152. string filePath = CommonHelper.GetFilePathOrEmpty();
  153. if (!string.IsNullOrEmpty(filePath) && contentEditControl.PdfViewControl != null)
  154. {
  155. if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  156. {
  157. string oldFilePath = pdfViewer.PDFView.Document.FilePath;
  158. if (oldFilePath.ToLower() == filePath.ToLower())
  159. {
  160. return;
  161. }
  162. }
  163. passwordViewer = new PDFViewControl();
  164. passwordViewer.PDFView.InitDocument(filePath);
  165. if (passwordViewer.PDFView.Document == null)
  166. {
  167. MessageBox.Show("Open File Failed");
  168. return;
  169. }
  170. if (passwordViewer.PDFView.Document.IsLocked)
  171. {
  172. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  173. PasswordUI.ClearPassword();
  174. PopupBorder.Visibility = Visibility.Visible;
  175. PasswordUI.Visibility = Visibility.Visible;
  176. }
  177. else
  178. {
  179. pdfViewer = passwordViewer;
  180. LoadDocument();
  181. }
  182. }
  183. }
  184. private void OpenFile_Click(object sender, RoutedEventArgs e)
  185. {
  186. OpenFile();
  187. }
  188. private void ControlRightPanel()
  189. {
  190. if (RightPanelButton != null)
  191. {
  192. if (RightPanelButton.IsChecked == true)
  193. {
  194. if (contentEditControl.pdfContentEditControl != null)
  195. {
  196. contentEditControl.ExpandRightPropertyPanel(contentEditControl.pdfContentEditControl, Visibility.Visible);
  197. if ((bool)ViewSettingBtn.IsChecked)
  198. {
  199. ViewSettingBtn.IsChecked = false;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. contentEditControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
  206. }
  207. }
  208. }
  209. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  210. {
  211. ControlRightPanel();
  212. }
  213. private void ShowViewSettings()
  214. {
  215. if (ViewSettingBtn != null)
  216. {
  217. if (ViewSettingBtn.IsChecked == true)
  218. {
  219. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  220. displayPanel.InitWithPDFViewer(contentEditControl.PdfViewControl.PDFView);
  221. contentEditControl.SetViewSettings(Visibility.Visible, displayPanel);
  222. if ((bool)RightPanelButton.IsChecked)
  223. {
  224. RightPanelButton.IsChecked = false;
  225. }
  226. }
  227. else
  228. {
  229. contentEditControl.SetViewSettings(Visibility.Collapsed);
  230. }
  231. }
  232. }
  233. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  234. {
  235. ShowViewSettings();
  236. }
  237. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  238. {
  239. ToggleButton expandBtn = sender as ToggleButton;
  240. if (expandBtn != null)
  241. {
  242. bool isExpand = expandBtn.IsChecked == true;
  243. contentEditControl.ExpandLeftPanel(isExpand);
  244. }
  245. }
  246. }
  247. }