MainWindow.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using ComPDFKit.PDFPage;
  2. using compdfkit_tools.Edit;
  3. using compdfkit_tools.PDFControl;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.PdfViewer;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Runtime.CompilerServices;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Controls.Primitives;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using System.Windows.Navigation;
  25. using System.Windows.Shapes;
  26. using System.Xml.Linq;
  27. namespace edit_ctrl_demo
  28. {
  29. /// <summary>
  30. /// MainWindow.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class MainWindow : Window,INotifyPropertyChanged
  33. {
  34. public bool CanUndo
  35. {
  36. get
  37. {
  38. if (PDFView != null)
  39. {
  40. return PDFView.UndoManager.CanUndo;
  41. }
  42. return false;
  43. }
  44. }
  45. public bool CanRedo
  46. {
  47. get
  48. {
  49. if (PDFView != null)
  50. {
  51. return PDFView.UndoManager.CanRedo;
  52. }
  53. return false;
  54. }
  55. }
  56. private PDFEditEvent lastPDFEditEvent = null;
  57. public MainWindow()
  58. {
  59. InitializeComponent();
  60. Loaded += MainWindow_Loaded;
  61. DataContext = this;
  62. }
  63. public event PropertyChangedEventHandler PropertyChanged;
  64. protected void OnPropertyChanged([CallerMemberName] string name = null)
  65. {
  66. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  67. }
  68. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  69. {
  70. LoadDefaultDocument();
  71. TitleBarTool.OpenFileEvent += TitleBarTool_OpenFileEvent;
  72. PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  73. PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  74. PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  75. }
  76. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  77. {
  78. if (e == null)
  79. {
  80. return;
  81. }
  82. if(e.EditType==CPDFEditType.EditText)
  83. {
  84. e.Handle = true;
  85. PDFEditTextContextMenu(sender,e);
  86. }
  87. if(e.EditType==CPDFEditType.EditImage)
  88. {
  89. e.Handle = true;
  90. PDFEditImageContextMenu(sender, e);
  91. }
  92. }
  93. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  94. {
  95. OnPropertyChanged(e.PropertyName);
  96. }
  97. /// <summary>
  98. /// 文字或图片块选中事件
  99. /// </summary>
  100. private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
  101. {
  102. PDFEditEvent tempEvent = lastPDFEditEvent;
  103. lastPDFEditEvent = null;
  104. if (e == null)
  105. {
  106. PDFEditContainer.Child= null;
  107. return;
  108. }
  109. if(Mouse.RightButton==MouseButtonState.Pressed)
  110. {
  111. lastPDFEditEvent = e;
  112. return;
  113. }
  114. if (e.EditType == CPDFEditType.EditText)
  115. {
  116. PDFTextEditControl textEditControl = new PDFTextEditControl();
  117. textEditControl.SetPDFTextEditData(e);
  118. PDFEditContainer.Child = textEditControl;
  119. return;
  120. }
  121. if (e.EditType == CPDFEditType.EditImage)
  122. {
  123. PDFImageEditControl imageEditControl = new PDFImageEditControl();
  124. imageEditControl.InitWithPDFViewer(PDFView);
  125. imageEditControl.SetPDFImageEditData(e);
  126. PDFEditContainer.Child = imageEditControl;
  127. return;
  128. }
  129. }
  130. private void TitleBarTool_OpenFileEvent(object sender, string filePath)
  131. {
  132. ClearPDFEditState();
  133. PDFView?.CloseDocument();
  134. PDFView?.InitDocument(filePath);
  135. PDFView?.Load();
  136. PDFView?.SetMouseMode(MouseModes.PanTool);
  137. }
  138. private void LoadDefaultDocument()
  139. {
  140. string defaultFilePath = "..\\..\\..\\..\\developer_guide_windows.pdf";
  141. PDFView?.InitDocument(defaultFilePath);
  142. PDFView?.Load();
  143. }
  144. /// <summary>
  145. /// 文字编辑模式
  146. /// </summary>
  147. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  148. {
  149. ToggleButton senderBtn=sender as ToggleButton;
  150. if(senderBtn != null)
  151. {
  152. ClearPDFEditState(senderBtn);
  153. if (senderBtn.IsChecked == true)
  154. {
  155. PDFView?.SetPDFEditType(CPDFEditType.EditText);
  156. PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  157. PDFView?.SetMouseMode(MouseModes.PDFEdit);
  158. PDFView?.ReloadDocument();
  159. }
  160. else
  161. {
  162. PDFView?.SetPDFEditType(CPDFEditType.None);
  163. PDFView?.SetMouseMode(MouseModes.PanTool);
  164. PDFView?.ReloadDocument();
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// 图片编辑模式
  170. /// </summary>
  171. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  172. {
  173. ToggleButton senderBtn = sender as ToggleButton;
  174. if (senderBtn != null)
  175. {
  176. ClearPDFEditState(senderBtn);
  177. senderBtn.IsChecked = true;
  178. //if (senderBtn.IsChecked == true)
  179. {
  180. PDFView?.SetPDFEditType(CPDFEditType.EditImage);
  181. PDFView?.SetMouseMode(MouseModes.PDFEdit);
  182. PDFView?.ReloadDocument();
  183. OpenFileDialog openFileDialog = new OpenFileDialog();
  184. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  185. if (openFileDialog.ShowDialog() == true)
  186. {
  187. PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  188. PDFView?.AddPDFEditImage(openFileDialog.FileName);
  189. }
  190. }
  191. //else
  192. //{
  193. // PDFView?.SetPDFEditType(CPDFEditType.None);
  194. // PDFView?.SetMouseMode(MouseModes.PanTool);
  195. // PDFView?.ReloadDocument();
  196. //}
  197. }
  198. }
  199. private void ClearPDFEditState(ToggleButton ignoreBtn=null)
  200. {
  201. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  202. {
  203. PDFTextEditButton,
  204. PDFImageEditButton,
  205. PDFEditBtn,
  206. };
  207. foreach (ToggleButton item in clearBtnList)
  208. {
  209. if(ignoreBtn==item)
  210. {
  211. continue;
  212. }
  213. item.IsChecked = false;
  214. }
  215. PDFEditContainer.Child = null;
  216. }
  217. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  218. {
  219. PDFView?.UndoManager?.Undo();
  220. }
  221. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  222. {
  223. PDFView?.UndoManager?.Redo();
  224. }
  225. private void PDFEditTextContextMenu(object sender,PDFEditCommand editCommand)
  226. {
  227. editCommand.PopupMenu = new ContextMenu();
  228. if (lastPDFEditEvent != null)
  229. {
  230. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "复制", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  231. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "剪切", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  232. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "删除", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  233. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  234. MenuItem propertyMenu = new MenuItem();
  235. propertyMenu.Header = "属性";
  236. propertyMenu.Click += (o, p) =>
  237. {
  238. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditText)
  239. {
  240. PDFTextEditControl textEditControl = new PDFTextEditControl();
  241. textEditControl.SetPDFTextEditData(lastPDFEditEvent);
  242. PDFEditContainer.Child = textEditControl;
  243. }
  244. };
  245. editCommand.PopupMenu.Items.Add(propertyMenu);
  246. }
  247. else
  248. {
  249. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  250. if(editCommand.TextAreaCopied)
  251. {
  252. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴样式", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  253. }
  254. }
  255. }
  256. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  257. {
  258. editCommand.PopupMenu = new ContextMenu();
  259. if(lastPDFEditEvent!=null)
  260. {
  261. MenuItem rotateLeftMenu = new MenuItem();
  262. rotateLeftMenu.Header = "左旋转";
  263. rotateLeftMenu.Click += (o, p) =>
  264. {
  265. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  266. {
  267. lastPDFEditEvent.Rotate = -90;
  268. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  269. }
  270. };
  271. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  272. MenuItem rotateRightMenu = new MenuItem();
  273. rotateRightMenu.Header = "右旋转";
  274. rotateRightMenu.Click += (o, p) =>
  275. {
  276. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  277. {
  278. lastPDFEditEvent.Rotate = 90;
  279. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  280. }
  281. };
  282. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  283. MenuItem replaceMenu = new MenuItem();
  284. replaceMenu.Header = "替换";
  285. replaceMenu.Click += (o, p) =>
  286. {
  287. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  288. {
  289. OpenFileDialog openFileDialog = new OpenFileDialog();
  290. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  291. if (openFileDialog.ShowDialog() == true)
  292. {
  293. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  294. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  295. PDFView?.ClearSelectPDFEdit();
  296. }
  297. }
  298. };
  299. editCommand.PopupMenu.Items.Add(replaceMenu);
  300. MenuItem exportMenu = new MenuItem();
  301. exportMenu.Header = "导出";
  302. exportMenu.Click += (o, p) =>
  303. {
  304. if (PDFView != null)
  305. {
  306. Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  307. if (imageDict != null && imageDict.Count > 0)
  308. {
  309. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  310. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  311. {
  312. string choosePath = folderBrowser.SelectedPath;
  313. string openPath = choosePath;
  314. try
  315. {
  316. foreach (int pageIndex in imageDict.Keys)
  317. {
  318. List<Bitmap> imageList = imageDict[pageIndex];
  319. foreach (Bitmap image in imageList)
  320. {
  321. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  322. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  323. openPath = savePath;
  324. }
  325. }
  326. Process.Start("explorer", "/select,\"" + openPath + "\"");
  327. }
  328. catch (Exception ex)
  329. {
  330. }
  331. }
  332. }
  333. }
  334. };
  335. editCommand.PopupMenu.Items.Add(exportMenu);
  336. MenuItem opacityMenu = new MenuItem();
  337. opacityMenu.Header = "透明度";
  338. editCommand.PopupMenu.Items.Add(opacityMenu);
  339. AppendOpacityMenu(opacityMenu);
  340. MenuItem horizonMirror = new MenuItem();
  341. horizonMirror.Header = "水平镜像";
  342. horizonMirror.Click += (o, p) =>
  343. {
  344. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  345. {
  346. lastPDFEditEvent.HorizontalMirror = true;
  347. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  348. }
  349. };
  350. editCommand.PopupMenu.Items.Add(horizonMirror);
  351. MenuItem verticalMirror = new MenuItem();
  352. verticalMirror.Header = "垂直镜像";
  353. verticalMirror.Click += (o, p) =>
  354. {
  355. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  356. {
  357. lastPDFEditEvent.VerticalMirror = true;
  358. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  359. }
  360. };
  361. editCommand.PopupMenu.Items.Add(verticalMirror);
  362. MenuItem cropMenu = new MenuItem();
  363. cropMenu.Header = "裁剪";
  364. cropMenu.Click += (o, p) =>
  365. {
  366. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  367. {
  368. lastPDFEditEvent.ClipImage = true;
  369. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  370. }
  371. };
  372. editCommand.PopupMenu.Items.Add(cropMenu);
  373. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "复制", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  374. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "剪切", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  375. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "删除", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  376. }
  377. else
  378. {
  379. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  380. }
  381. }
  382. private void AppendOpacityMenu(MenuItem parentMenu)
  383. {
  384. List<int> opacityList = new List<int>()
  385. {
  386. 25,50,75,100
  387. };
  388. foreach(int opacity in opacityList)
  389. {
  390. MenuItem opacityMenu = new MenuItem();
  391. opacityMenu.Header = string.Format("{0}%", opacity);
  392. opacityMenu.Click += (o, p) =>
  393. {
  394. if(lastPDFEditEvent != null && lastPDFEditEvent.EditType==CPDFEditType.EditImage)
  395. {
  396. lastPDFEditEvent.Transparency = (int)(opacity * 255 / 100);
  397. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  398. }
  399. };
  400. parentMenu.Items.Add(opacityMenu);
  401. }
  402. }
  403. private void PDFEditBtn_Click(object sender, RoutedEventArgs e)
  404. {
  405. ToggleButton senderBtn = sender as ToggleButton;
  406. if (senderBtn != null)
  407. {
  408. ClearPDFEditState(senderBtn);
  409. if (senderBtn.IsChecked == true)
  410. {
  411. PDFView?.SetPDFEditType(CPDFEditType.EditText|CPDFEditType.EditImage);
  412. PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  413. PDFView?.SetMouseMode(MouseModes.PDFEdit);
  414. PDFView?.ReloadDocument();
  415. }
  416. else
  417. {
  418. PDFView?.SetPDFEditType(CPDFEditType.None);
  419. PDFView?.SetMouseMode(MouseModes.PanTool);
  420. PDFView?.ReloadDocument();
  421. }
  422. }
  423. }
  424. }
  425. }