MainWindow.xaml.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using Compdfkit_Tools.Edit;
  5. using Compdfkit_Tools.Helper;
  6. using Compdfkit_Tools.PDFControl;
  7. using ComPDFKitViewer;
  8. using ComPDFKitViewer.AnnotEvent;
  9. using ComPDFKitViewer.PdfViewer;
  10. using Microsoft.Win32;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Diagnostics;
  15. using System.Drawing;
  16. using System.IO;
  17. using System.Runtime.CompilerServices;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Controls.Primitives;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using Point = System.Windows.Point;
  25. namespace ContentEditor
  26. {
  27. public partial class MainWindow : Window, INotifyPropertyChanged
  28. {
  29. #region Property
  30. public bool CanUndo
  31. {
  32. get
  33. {
  34. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  35. {
  36. return pdfViewControl.PDFView.UndoManager.CanUndo;
  37. }
  38. return false;
  39. }
  40. }
  41. public bool CanRedo
  42. {
  43. get
  44. {
  45. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  46. {
  47. return pdfViewControl.PDFView.UndoManager.CanRedo;
  48. }
  49. return false;
  50. }
  51. }
  52. public bool CanSave
  53. {
  54. get
  55. {
  56. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  57. {
  58. return pdfViewControl.PDFView.UndoManager.CanSave;
  59. }
  60. return false;
  61. }
  62. }
  63. /// <summary>
  64. /// The last PDF edit object
  65. /// </summary>
  66. private PDFEditEvent lastPDFEditEvent = null;
  67. private PDFViewControl passwordViewer;
  68. private PDFViewControl pdfViewControl;
  69. private PDFImageEditControl imageEditControl = new PDFImageEditControl();
  70. private PDFTextEditControl textEditControl = new PDFTextEditControl();
  71. private Border empytPanel = new Border();
  72. private UIElement prevPanel = null;
  73. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  74. public event PropertyChangedEventHandler PropertyChanged;
  75. private PDFEditEvent pdfTextCreateParam;
  76. private KeyEventHandler KeyDownHandler;
  77. #endregion
  78. public MainWindow()
  79. {
  80. InitializeComponent();
  81. Loaded += MainWindow_Loaded;
  82. DataContext = this;
  83. }
  84. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  85. {
  86. TextBlock emptyBlock = new TextBlock();
  87. emptyBlock.HorizontalAlignment = HorizontalAlignment.Center;
  88. emptyBlock.VerticalAlignment = VerticalAlignment.Center;
  89. empytPanel.Child = emptyBlock;
  90. empytPanel.Width = 260;
  91. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search);
  92. LoadDefaultDocument();
  93. }
  94. #region Load document
  95. private void LoadDocument()
  96. {
  97. if (pdfViewControl.PDFView.Document == null)
  98. {
  99. return;
  100. }
  101. pdfViewControl.PDFView?.Load();
  102. PDFGrid.Child = pdfViewControl;
  103. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  104. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  105. PasswordUI.Closed -= PasswordUI_Closed;
  106. PasswordUI.Canceled -= PasswordUI_Canceled;
  107. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  108. PasswordUI.Closed += PasswordUI_Closed;
  109. PasswordUI.Canceled += PasswordUI_Canceled;
  110. PasswordUI.Confirmed += PasswordUI_Confirmed;
  111. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  112. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  113. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  114. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  115. pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  116. pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  117. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  118. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  119. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  120. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  121. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  122. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  123. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  124. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  125. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  126. ViewSettingBtn.IsChecked = false;
  127. PropertyContainer.Child = null;
  128. PropertyContainer.Visibility = Visibility.Collapsed;
  129. SetEditMode();
  130. if (KeyDownHandler != null)
  131. {
  132. pdfViewControl.PDFView.RemoveHandler(KeyDownEvent, KeyDownHandler);
  133. }
  134. KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
  135. pdfViewControl.PDFView.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
  136. }
  137. private void LoadDefaultDocument()
  138. {
  139. string defaultFilePath = "PDF32000_2008.pdf";
  140. pdfViewControl = new PDFViewControl();
  141. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  142. LoadDocument();
  143. }
  144. #endregion
  145. #region Password
  146. private void PasswordUI_Confirmed(object sender, string e)
  147. {
  148. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  149. {
  150. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  151. if (passwordViewer.PDFView.Document.IsLocked == false)
  152. {
  153. PasswordUI.SetShowError("", Visibility.Collapsed);
  154. PasswordUI.ClearPassword();
  155. PasswordUI.Visibility = Visibility.Collapsed;
  156. PopupBorder.Visibility = Visibility.Collapsed;
  157. pdfViewControl.PDFView.Document.Release();
  158. pdfViewControl = passwordViewer;
  159. LoadDocument();
  160. }
  161. else
  162. {
  163. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  164. }
  165. }
  166. }
  167. private void PasswordUI_Canceled(object sender, EventArgs e)
  168. {
  169. PopupBorder.Visibility = Visibility.Collapsed;
  170. PasswordUI.Visibility = Visibility.Collapsed;
  171. }
  172. private void PasswordUI_Closed(object sender, EventArgs e)
  173. {
  174. PopupBorder.Visibility = Visibility.Collapsed;
  175. PasswordUI.Visibility = Visibility.Collapsed;
  176. }
  177. #endregion
  178. #region Expand and collapse Panel
  179. private void ExpandLeftPanel(bool isExpand)
  180. {
  181. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  182. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  183. if (isExpand)
  184. {
  185. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  186. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  187. }
  188. else
  189. {
  190. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  191. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  192. }
  193. }
  194. #endregion
  195. #region Context menu
  196. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  197. {
  198. if (e != null && e.CommandType == CommandType.Context)
  199. {
  200. if (e.PressOnSelectedText)
  201. {
  202. e.Handle = true;
  203. e.PopupMenu = new ContextMenu();
  204. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  205. }
  206. else if (e.CommandTarget == TargetType.ImageSelection)
  207. {
  208. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  209. {
  210. e.Handle = true;
  211. e.PopupMenu = new ContextMenu();
  212. MenuItem imageCopyMenu = new MenuItem();
  213. imageCopyMenu = new MenuItem();
  214. imageCopyMenu.Header = "Copy Images";
  215. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  216. imageCopyMenu.CommandParameter = e;
  217. e.PopupMenu.Items.Add(imageCopyMenu);
  218. MenuItem imageExtraMenu = new MenuItem();
  219. imageExtraMenu = new MenuItem();
  220. imageExtraMenu.Header = "Extract Images";
  221. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  222. imageExtraMenu.CommandParameter = e;
  223. e.PopupMenu.Items.Add(imageExtraMenu);
  224. }
  225. }
  226. else
  227. {
  228. e.Handle = true;
  229. e.PopupMenu = new ContextMenu();
  230. MenuItem fitWidthMenu = new MenuItem();
  231. fitWidthMenu.Header = "Automatically Resize";
  232. fitWidthMenu.Click += (o, p) =>
  233. {
  234. if (pdfViewControl != null)
  235. {
  236. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  237. }
  238. };
  239. e.PopupMenu.Items.Add(fitWidthMenu);
  240. MenuItem fitSizeMenu = new MenuItem();
  241. fitSizeMenu.Header = "Actual Size";
  242. fitSizeMenu.Click += (o, p) =>
  243. {
  244. if (pdfViewControl != null)
  245. {
  246. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  247. }
  248. };
  249. e.PopupMenu.Items.Add(fitSizeMenu);
  250. MenuItem zoomInMenu = new MenuItem();
  251. zoomInMenu.Header = "Zoom In";
  252. zoomInMenu.Click += (o, p) =>
  253. {
  254. if (pdfViewControl != null)
  255. {
  256. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  257. pdfViewControl.PDFView?.Zoom(newZoom);
  258. }
  259. };
  260. e.PopupMenu.Items.Add(zoomInMenu);
  261. MenuItem zoomOutMenu = new MenuItem();
  262. zoomOutMenu.Header = "Zoom Out";
  263. zoomOutMenu.Click += (o, p) =>
  264. {
  265. if (pdfViewControl != null)
  266. {
  267. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  268. pdfViewControl.PDFView?.Zoom(newZoom);
  269. }
  270. };
  271. e.PopupMenu.Items.Add(zoomOutMenu);
  272. e.PopupMenu.Items.Add(new Separator());
  273. MenuItem singleView = new MenuItem();
  274. singleView.Header = "Single Page";
  275. singleView.Click += (o, p) =>
  276. {
  277. if (pdfViewControl != null)
  278. {
  279. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  280. }
  281. };
  282. e.PopupMenu.Items.Add(singleView);
  283. MenuItem singleContinuousView = new MenuItem();
  284. singleContinuousView.Header = "Single Page Continuous";
  285. singleContinuousView.Click += (o, p) =>
  286. {
  287. if (pdfViewControl != null)
  288. {
  289. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  290. }
  291. };
  292. e.PopupMenu.Items.Add(singleContinuousView);
  293. MenuItem doubleView = new MenuItem();
  294. doubleView.Header = "Two Pages";
  295. doubleView.Click += (o, p) =>
  296. {
  297. if (pdfViewControl != null)
  298. {
  299. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  300. }
  301. };
  302. e.PopupMenu.Items.Add(doubleView);
  303. MenuItem doubleContinuousView = new MenuItem();
  304. doubleContinuousView.Header = "Two Pages Continuous";
  305. doubleContinuousView.Click += (o, p) =>
  306. {
  307. if (pdfViewControl != null)
  308. {
  309. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  310. }
  311. };
  312. e.PopupMenu.Items.Add(doubleContinuousView);
  313. }
  314. }
  315. if (e != null && e.CommandType == CommandType.Copy)
  316. {
  317. e.DoCommand();
  318. }
  319. }
  320. private void CopyImage_Click(object sender, RoutedEventArgs e)
  321. {
  322. try
  323. {
  324. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  325. if (imageDict != null && imageDict.Count > 0)
  326. {
  327. foreach (int pageIndex in imageDict.Keys)
  328. {
  329. List<Bitmap> imageList = imageDict[pageIndex];
  330. foreach (Bitmap image in imageList)
  331. {
  332. MemoryStream ms = new MemoryStream();
  333. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  334. BitmapImage imageData = new BitmapImage();
  335. imageData.BeginInit();
  336. imageData.StreamSource = ms;
  337. imageData.CacheOption = BitmapCacheOption.OnLoad;
  338. imageData.EndInit();
  339. imageData.Freeze();
  340. Clipboard.SetImage(imageData);
  341. break;
  342. }
  343. }
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. }
  349. }
  350. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  351. {
  352. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  353. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  354. {
  355. string choosePath = folderDialog.SelectedPath;
  356. string openPath = choosePath;
  357. try
  358. {
  359. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  360. if (imageDict != null && imageDict.Count > 0)
  361. {
  362. foreach (int pageIndex in imageDict.Keys)
  363. {
  364. List<Bitmap> imageList = imageDict[pageIndex];
  365. foreach (Bitmap image in imageList)
  366. {
  367. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  368. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  369. openPath = savePath;
  370. }
  371. }
  372. }
  373. Process.Start("explorer", "/select,\"" + openPath + "\"");
  374. }
  375. catch (Exception ex)
  376. {
  377. }
  378. }
  379. }
  380. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  381. {
  382. editCommand.PopupMenu = new ContextMenu();
  383. if (ApplicationCommands.Copy.CanExecute(null, (UIElement)sender) && lastPDFEditEvent != null)
  384. {
  385. MenuItem rotateLeftMenu = new MenuItem();
  386. rotateLeftMenu.Header = "Rotate Left";
  387. rotateLeftMenu.Click += (o, p) =>
  388. {
  389. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  390. {
  391. lastPDFEditEvent.Rotate = -90;
  392. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  393. imageEditControl?.SetImageThumb();
  394. }
  395. };
  396. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  397. MenuItem rotateRightMenu = new MenuItem();
  398. rotateRightMenu.Header = "Rotate Right";
  399. rotateRightMenu.Click += (o, p) =>
  400. {
  401. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  402. {
  403. lastPDFEditEvent.Rotate = 90;
  404. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  405. imageEditControl?.SetImageThumb();
  406. }
  407. };
  408. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  409. MenuItem replaceMenu = new MenuItem();
  410. replaceMenu.Header = "Replace";
  411. replaceMenu.Click += (o, p) =>
  412. {
  413. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  414. {
  415. OpenFileDialog openFileDialog = new OpenFileDialog();
  416. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  417. if (openFileDialog.ShowDialog() == true)
  418. {
  419. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  420. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  421. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  422. }
  423. }
  424. };
  425. editCommand.PopupMenu.Items.Add(replaceMenu);
  426. MenuItem exportMenu = new MenuItem();
  427. exportMenu.Header = "Export";
  428. exportMenu.Click += (o, p) =>
  429. {
  430. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  431. {
  432. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView.GetSelectedImages();
  433. if (imageDict != null && imageDict.Count > 0)
  434. {
  435. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  436. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  437. {
  438. string choosePath = folderBrowser.SelectedPath;
  439. string openPath = choosePath;
  440. try
  441. {
  442. foreach (int pageIndex in imageDict.Keys)
  443. {
  444. List<Bitmap> imageList = imageDict[pageIndex];
  445. foreach (Bitmap image in imageList)
  446. {
  447. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  448. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  449. openPath = savePath;
  450. }
  451. }
  452. Process.Start("explorer", "/select,\"" + openPath + "\"");
  453. }
  454. catch (Exception ex)
  455. {
  456. }
  457. }
  458. }
  459. }
  460. };
  461. editCommand.PopupMenu.Items.Add(exportMenu);
  462. MenuItem opacityMenu = new MenuItem();
  463. opacityMenu.Header = "Opacity";
  464. editCommand.PopupMenu.Items.Add(opacityMenu);
  465. AppendOpacityMenu(opacityMenu);
  466. MenuItem horizonMirror = new MenuItem();
  467. horizonMirror.Header = "Flip horizontal";
  468. horizonMirror.Click += (o, p) =>
  469. {
  470. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  471. {
  472. lastPDFEditEvent.HorizontalMirror = true;
  473. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  474. imageEditControl?.SetImageThumb();
  475. }
  476. };
  477. editCommand.PopupMenu.Items.Add(horizonMirror);
  478. MenuItem verticalMirror = new MenuItem();
  479. verticalMirror.Header = "Flip vertical";
  480. verticalMirror.Click += (o, p) =>
  481. {
  482. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  483. {
  484. lastPDFEditEvent.VerticalMirror = true;
  485. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  486. imageEditControl?.SetImageThumb();
  487. }
  488. };
  489. editCommand.PopupMenu.Items.Add(verticalMirror);
  490. MenuItem cropMenu = new MenuItem();
  491. cropMenu.Header = "Crop";
  492. cropMenu.Click += (o, p) =>
  493. {
  494. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  495. {
  496. lastPDFEditEvent.ClipImage = true;
  497. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  498. }
  499. };
  500. editCommand.PopupMenu.Items.Add(cropMenu);
  501. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  502. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  503. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  504. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  505. if (editCommand.TextAreaCopied)
  506. {
  507. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  508. }
  509. }
  510. else
  511. {
  512. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  513. }
  514. }
  515. private void AppendOpacityMenu(MenuItem parentMenu)
  516. {
  517. List<int> opacityList = new List<int>()
  518. {
  519. 25,50,75,100
  520. };
  521. foreach (int opacity in opacityList)
  522. {
  523. MenuItem opacityMenu = new MenuItem();
  524. opacityMenu.Header = string.Format("{0}%", opacity);
  525. opacityMenu.Click += (o, p) =>
  526. {
  527. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  528. {
  529. lastPDFEditEvent.Transparency = (int)(opacity * 255 / 100D);
  530. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  531. imageEditControl?.SetImageTransparency(lastPDFEditEvent.Transparency);
  532. imageEditControl?.SetImageThumb();
  533. }
  534. };
  535. parentMenu.Items.Add(opacityMenu);
  536. }
  537. }
  538. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  539. {
  540. editCommand.PopupMenu = new ContextMenu();
  541. if (lastPDFEditEvent != null)
  542. {
  543. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  544. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  545. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  546. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  547. }
  548. else
  549. {
  550. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  551. if (editCommand.TextAreaCopied)
  552. {
  553. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  554. }
  555. }
  556. }
  557. #endregion
  558. #region UI
  559. /// <summary>
  560. /// Check the zoom factor
  561. /// </summary>
  562. private double CheckZoomLevel(double zoom, bool IsGrowth)
  563. {
  564. double standardZoom = 100;
  565. if (zoom <= 0.01)
  566. {
  567. return 0.01;
  568. }
  569. if (zoom >= 10)
  570. {
  571. return 10;
  572. }
  573. zoom *= 100;
  574. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  575. {
  576. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  577. {
  578. standardZoom = zoomLevelList[i + 1];
  579. break;
  580. }
  581. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  582. {
  583. standardZoom = zoomLevelList[i];
  584. break;
  585. }
  586. }
  587. return standardZoom / 100;
  588. }
  589. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  590. {
  591. UIElement pageView = sender as UIElement;
  592. if (pageView != null)
  593. {
  594. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  595. }
  596. SetImageEditProperty();
  597. }
  598. private void ControlRightPanel()
  599. {
  600. if ((bool)ViewSettingBtn.IsChecked)
  601. {
  602. ViewSettingBtn.IsChecked = false;
  603. }
  604. if (RightPanelButton != null)
  605. {
  606. if (RightPanelButton.IsChecked == true)
  607. {
  608. if (PropertyContainer.Child is CPDFDisplaySettingsControl)
  609. {
  610. PropertyContainer.Child = prevPanel;
  611. }
  612. if (PropertyContainer.Child == null)
  613. {
  614. PropertyContainer.Child = empytPanel;
  615. }
  616. PropertyContainer.Visibility = Visibility.Visible;
  617. }
  618. else
  619. {
  620. PropertyContainer.Visibility = Visibility.Collapsed;
  621. }
  622. }
  623. }
  624. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  625. {
  626. ControlRightPanel();
  627. }
  628. private void ControlLeftPanel()
  629. {
  630. if (LeftToolPanelButton != null)
  631. {
  632. bool isExpand = LeftToolPanelButton.IsChecked == true;
  633. ExpandLeftPanel(isExpand);
  634. }
  635. }
  636. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  637. {
  638. ControlLeftPanel();
  639. }
  640. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  641. {
  642. LeftToolPanelButton.IsChecked = true;
  643. ExpandLeftPanel(true);
  644. BotaSideTool.SelectBotaTool(BOTATools.Search);
  645. }
  646. private void ShowViewSettings()
  647. {
  648. if (RightPanelButton != null)
  649. {
  650. RightPanelButton.IsChecked = false;
  651. }
  652. if (ViewSettingBtn != null)
  653. {
  654. if (ViewSettingBtn.IsChecked == true)
  655. {
  656. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  657. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  658. if (ViewComboBox.SelectedIndex == 1)
  659. {
  660. displayPanel.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  661. }
  662. else
  663. {
  664. displayPanel.SetVisibilityWhenContentEdit(Visibility.Visible);
  665. }
  666. prevPanel = PropertyContainer.Child;
  667. PropertyContainer.Child = displayPanel;
  668. PropertyContainer.Visibility = Visibility.Visible;
  669. }
  670. else
  671. {
  672. PropertyContainer.Child = null;
  673. PropertyContainer.Visibility = Visibility.Collapsed;
  674. }
  675. }
  676. }
  677. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  678. {
  679. ShowViewSettings();
  680. }
  681. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  682. {
  683. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  684. }
  685. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  686. {
  687. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  688. }
  689. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  690. {
  691. PasswordUI.Visibility = Visibility.Collapsed;
  692. FileInfoUI.Visibility = Visibility.Visible;
  693. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  694. PopupBorder.Visibility = Visibility.Visible;
  695. }
  696. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  697. {
  698. PopupBorder.Visibility = Visibility.Collapsed;
  699. }
  700. private void OpenFile()
  701. {
  702. string filePath = CommonHelper.GetFilePathOrEmpty();
  703. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  704. {
  705. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  706. {
  707. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  708. if (oldFilePath.ToLower() == filePath.ToLower())
  709. {
  710. return;
  711. }
  712. }
  713. passwordViewer = new PDFViewControl();
  714. passwordViewer.PDFView.InitDocument(filePath);
  715. if (passwordViewer.PDFView.Document == null)
  716. {
  717. MessageBox.Show("Open File Failed");
  718. return;
  719. }
  720. if (passwordViewer.PDFView.Document.IsLocked)
  721. {
  722. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  723. PasswordUI.ClearPassword();
  724. PopupBorder.Visibility = Visibility.Visible;
  725. PasswordUI.Visibility = Visibility.Visible;
  726. }
  727. else
  728. {
  729. pdfViewControl.PDFView.Document.Release();
  730. pdfViewControl = passwordViewer;
  731. LoadDocument();
  732. }
  733. }
  734. }
  735. private void OpenFile_Click(object sender, RoutedEventArgs e)
  736. {
  737. OpenFile();
  738. }
  739. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  740. {
  741. SaveFile();
  742. }
  743. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  744. {
  745. ToggleButton senderBtn = sender as ToggleButton;
  746. if (senderBtn != null && pdfViewControl != null)
  747. {
  748. ClearPDFEditState(senderBtn);
  749. if (senderBtn.IsChecked == true)
  750. {
  751. PDFEditEvent createParam = new PDFEditEvent();
  752. createParam.EditType = CPDFEditType.EditText;
  753. createParam.IsBold = false;
  754. createParam.IsItalic = false;
  755. createParam.FontSize = 14;
  756. createParam.FontName = "Helvetica";
  757. createParam.FontColor = Colors.Black;
  758. createParam.TextAlign = TextAlignType.AlignLeft;
  759. createParam.Transparency = 255;
  760. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  761. {
  762. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  763. if (pdfDoc.PageCount > 0)
  764. {
  765. CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
  766. CPDFEditPage editPage = pdfPage.GetEditPage();
  767. editPage.BeginEdit(CPDFEditType.EditText);
  768. createParam.SystemFontNameList.AddRange(editPage.GetFontList());
  769. editPage.EndEdit();
  770. }
  771. }
  772. pdfViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  773. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  774. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  775. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  776. pdfViewControl.PDFView?.ReloadDocument();
  777. pdfViewControl.PDFView?.SetPDFEditParam(createParam);
  778. if (textEditControl == null)
  779. {
  780. textEditControl = new PDFTextEditControl();
  781. }
  782. textEditControl.SetPDFTextEditData(createParam);
  783. PropertyContainer.Child = textEditControl;
  784. PropertyContainer.Visibility = Visibility.Visible;
  785. if ((bool)ViewSettingBtn.IsChecked)
  786. {
  787. ViewSettingBtn.IsChecked = false;
  788. }
  789. RightPanelButton.IsChecked = true;
  790. pdfTextCreateParam = createParam;
  791. }
  792. else
  793. {
  794. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  795. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  796. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  797. pdfViewControl.PDFView?.ReloadDocument();
  798. RightPanelButton.IsChecked = false;
  799. if ((bool)ViewSettingBtn.IsChecked)
  800. {
  801. return;
  802. }
  803. else
  804. {
  805. if (PropertyContainer.Child != null)
  806. {
  807. PropertyContainer.Child = empytPanel;
  808. }
  809. }
  810. PropertyContainer.Visibility = Visibility.Collapsed;
  811. }
  812. }
  813. }
  814. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  815. {
  816. ToggleButton senderBtn = sender as ToggleButton;
  817. if (senderBtn != null && pdfViewControl != null)
  818. {
  819. senderBtn.IsChecked = false;
  820. OpenFileDialog openFileDialog = new OpenFileDialog();
  821. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  822. if (openFileDialog.ShowDialog() == true)
  823. {
  824. ClearPDFEditState(senderBtn);
  825. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  826. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  827. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  828. pdfViewControl.PDFView?.ReloadDocument();
  829. RightPanelButton.IsChecked = false;
  830. if (PropertyContainer.Child != null && !(bool)ViewSettingBtn.IsChecked)
  831. {
  832. PropertyContainer.Child = empytPanel;
  833. PropertyContainer.Visibility = Visibility.Collapsed;
  834. }
  835. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  836. pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  837. }
  838. }
  839. }
  840. private void ClearPDFEditState(ToggleButton ignoreBtn = null)
  841. {
  842. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  843. {
  844. PDFTextEditButton,
  845. PDFImageEditButton
  846. };
  847. foreach (ToggleButton item in clearBtnList)
  848. {
  849. if (ignoreBtn == item)
  850. {
  851. continue;
  852. }
  853. item.IsChecked = false;
  854. }
  855. }
  856. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  857. {
  858. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  859. {
  860. pdfViewControl.PDFView.UndoManager?.Undo();
  861. }
  862. }
  863. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  864. {
  865. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  866. {
  867. pdfViewControl.PDFView.UndoManager?.Redo();
  868. }
  869. }
  870. #endregion
  871. #region Property changed
  872. protected void OnPropertyChanged([CallerMemberName] string name = null)
  873. {
  874. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  875. }
  876. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  877. {
  878. OnPropertyChanged(e.PropertyName);
  879. }
  880. #endregion
  881. #region Event handle
  882. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  883. {
  884. if (e == null)
  885. {
  886. return;
  887. }
  888. if (e.EditType == CPDFEditType.EditText)
  889. {
  890. e.Handle = true;
  891. PDFEditTextContextMenu(sender, e);
  892. }
  893. if (e.EditType == CPDFEditType.EditImage)
  894. {
  895. e.Handle = true;
  896. PDFEditImageContextMenu(sender, e);
  897. }
  898. }
  899. private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
  900. {
  901. PDFEditEvent tempEvent = lastPDFEditEvent;
  902. lastPDFEditEvent = e;
  903. if ((bool)ViewSettingBtn.IsChecked)
  904. {
  905. ViewSettingBtn.IsChecked = false;
  906. RightPanelButton.IsChecked = true;
  907. }
  908. if (e == null)
  909. {
  910. PropertyContainer.Child = empytPanel;
  911. if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null)
  912. {
  913. if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  914. {
  915. if (textEditControl == null)
  916. {
  917. textEditControl = new PDFTextEditControl();
  918. }
  919. textEditControl.SetPDFTextEditData(pdfTextCreateParam);
  920. PropertyContainer.Child = textEditControl;
  921. }
  922. }
  923. return;
  924. }
  925. if (Mouse.RightButton == MouseButtonState.Pressed && PropertyContainer.Visibility != Visibility.Visible)
  926. {
  927. return;
  928. }
  929. if (e.EditType == CPDFEditType.EditText)
  930. {
  931. if (textEditControl == null)
  932. {
  933. textEditControl = new PDFTextEditControl();
  934. }
  935. textEditControl.SetPDFTextEditData(e);
  936. PropertyContainer.Child = textEditControl;
  937. return;
  938. }
  939. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  940. {
  941. UIElement pageView = sender as UIElement;
  942. if (pageView != null)
  943. {
  944. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  945. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  946. }
  947. if (Mouse.RightButton == MouseButtonState.Pressed)
  948. {
  949. SetImageEditProperty();
  950. }
  951. return;
  952. }
  953. }
  954. #endregion
  955. #region Save file
  956. /// <summary>
  957. /// Save the file to another PDF file.
  958. /// </summary>
  959. public void SaveAsFile()
  960. {
  961. {
  962. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  963. {
  964. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  965. SaveFileDialog saveDialog = new SaveFileDialog();
  966. saveDialog.Filter = "(*.pdf)|*.pdf";
  967. saveDialog.DefaultExt = ".pdf";
  968. saveDialog.OverwritePrompt = true;
  969. if (saveDialog.ShowDialog() == true)
  970. {
  971. pdfDoc.WriteToFilePath(saveDialog.FileName);
  972. }
  973. }
  974. }
  975. }
  976. private void SaveFile()
  977. {
  978. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  979. {
  980. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  981. if (pdfDoc.WriteToLoadedPath())
  982. {
  983. return;
  984. }
  985. SaveFileDialog saveDialog = new SaveFileDialog();
  986. saveDialog.Filter = "(*.pdf)|*.pdf";
  987. saveDialog.DefaultExt = ".pdf";
  988. saveDialog.OverwritePrompt = true;
  989. if (saveDialog.ShowDialog() == true)
  990. {
  991. pdfDoc.WriteToFilePath(saveDialog.FileName);
  992. }
  993. }
  994. }
  995. #endregion
  996. #region Selected changed
  997. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  998. {
  999. if (e.Key == "Zoom")
  1000. {
  1001. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  1002. }
  1003. }
  1004. private void SetImageEditProperty()
  1005. {
  1006. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  1007. {
  1008. if (imageEditControl == null)
  1009. {
  1010. imageEditControl = new PDFImageEditControl();
  1011. }
  1012. imageEditControl.InitWithPDFViewer(pdfViewControl.PDFView);
  1013. imageEditControl.SetPDFImageEditData(lastPDFEditEvent);
  1014. PropertyContainer.Child = imageEditControl;
  1015. }
  1016. }
  1017. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1018. {
  1019. SetEditMode();
  1020. }
  1021. private void SetEditMode()
  1022. {
  1023. ClearPDFEditState(null);
  1024. if (pdfViewControl != null && ViewComboBox != null)
  1025. {
  1026. if (ViewComboBox.SelectedIndex == 0)
  1027. {
  1028. PDFEditTool.Visibility = Visibility.Collapsed;
  1029. pdfViewControl.PDFView?.SetShowLink(true);
  1030. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1031. pdfViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  1032. pdfViewControl.PDFView?.ReloadDocument();
  1033. }
  1034. if (ViewComboBox.SelectedIndex == 1)
  1035. {
  1036. PDFEditTool.Visibility = Visibility.Visible;
  1037. pdfViewControl.PDFView?.SetShowLink(false);
  1038. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1039. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  1040. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1041. pdfViewControl.PDFView?.ReloadDocument();
  1042. }
  1043. RightPanelButton.IsChecked = false;
  1044. ViewSettingBtn.IsChecked = false;
  1045. PropertyContainer.Visibility = Visibility.Collapsed;
  1046. if (PropertyContainer != null)
  1047. {
  1048. PropertyContainer.Child = null;
  1049. }
  1050. }
  1051. }
  1052. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  1053. {
  1054. if (pdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
  1055. {
  1056. return;
  1057. }
  1058. if (Keyboard.Modifiers == ModifierKeys.Control)
  1059. {
  1060. if (e.Key == Key.Left)
  1061. {
  1062. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
  1063. e.Handled = true;
  1064. }
  1065. if (e.Key == Key.Right)
  1066. {
  1067. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
  1068. e.Handled = true;
  1069. }
  1070. if (e.Key == Key.Up)
  1071. {
  1072. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
  1073. e.Handled = true;
  1074. }
  1075. if (e.Key == Key.Down)
  1076. {
  1077. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
  1078. e.Handled = true;
  1079. }
  1080. }
  1081. if (Keyboard.Modifiers == ModifierKeys.Shift)
  1082. {
  1083. if (e.Key == Key.Left)
  1084. {
  1085. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
  1086. e.Handled = true;
  1087. }
  1088. if (e.Key == Key.Right)
  1089. {
  1090. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
  1091. e.Handled = true;
  1092. }
  1093. if (e.Key == Key.Up)
  1094. {
  1095. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
  1096. e.Handled = true;
  1097. }
  1098. if (e.Key == Key.Down)
  1099. {
  1100. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
  1101. e.Handled = true;
  1102. }
  1103. }
  1104. if (Keyboard.Modifiers == ModifierKeys.Alt)
  1105. {
  1106. if (e.SystemKey == Key.Up)
  1107. {
  1108. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false);
  1109. e.Handled = true;
  1110. }
  1111. if (e.SystemKey == Key.Down)
  1112. {
  1113. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false);
  1114. e.Handled = true;
  1115. }
  1116. }
  1117. if (Keyboard.Modifiers == ModifierKeys.None)
  1118. {
  1119. if (e.Key == Key.Left)
  1120. {
  1121. pdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
  1122. e.Handled = true;
  1123. }
  1124. if (e.Key == Key.Right)
  1125. {
  1126. pdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
  1127. e.Handled = true;
  1128. }
  1129. if (e.Key == Key.Up)
  1130. {
  1131. pdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
  1132. e.Handled = true;
  1133. }
  1134. if (e.Key == Key.Down)
  1135. {
  1136. pdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
  1137. e.Handled = true;
  1138. }
  1139. }
  1140. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  1141. {
  1142. if (e.Key == Key.Left)
  1143. {
  1144. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
  1145. e.Handled = true;
  1146. }
  1147. if (e.Key == Key.Right)
  1148. {
  1149. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
  1150. e.Handled = true;
  1151. }
  1152. if (e.Key == Key.Up)
  1153. {
  1154. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
  1155. e.Handled = true;
  1156. }
  1157. if (e.Key == Key.Down)
  1158. {
  1159. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
  1160. e.Handled = true;
  1161. }
  1162. }
  1163. }
  1164. #endregion
  1165. #region Close window
  1166. protected override void OnClosing(CancelEventArgs e)
  1167. {
  1168. if (pdfViewControl.PDFView.UndoManager.CanSave)
  1169. {
  1170. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  1171. if (result == MessageBoxResult.Yes)
  1172. {
  1173. SaveFile();
  1174. }
  1175. else if (result == MessageBoxResult.No)
  1176. {
  1177. }
  1178. else
  1179. {
  1180. e.Cancel = true;
  1181. }
  1182. }
  1183. }
  1184. #endregion
  1185. #region Shortcut
  1186. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  1187. {
  1188. OpenFile();
  1189. }
  1190. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  1191. {
  1192. if (CanSave)
  1193. {
  1194. SaveFile();
  1195. }
  1196. }
  1197. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  1198. {
  1199. SaveAsFile();
  1200. }
  1201. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  1202. {
  1203. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
  1204. {
  1205. pdfViewControl.PDFView.UndoManager?.Undo();
  1206. }
  1207. }
  1208. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  1209. {
  1210. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
  1211. {
  1212. pdfViewControl.PDFView.UndoManager?.Redo();
  1213. }
  1214. }
  1215. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  1216. {
  1217. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  1218. ControlLeftPanel();
  1219. }
  1220. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  1221. {
  1222. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  1223. ControlRightPanel();
  1224. }
  1225. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  1226. {
  1227. ExpandLeftPanel(true);
  1228. LeftToolPanelButton.IsChecked = true;
  1229. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  1230. }
  1231. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  1232. {
  1233. ExpandLeftPanel(true);
  1234. LeftToolPanelButton.IsChecked = true;
  1235. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  1236. }
  1237. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  1238. {
  1239. ExpandLeftPanel(true);
  1240. LeftToolPanelButton.IsChecked = true;
  1241. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  1242. }
  1243. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  1244. {
  1245. ExpandLeftPanel(true);
  1246. LeftToolPanelButton.IsChecked = true;
  1247. BotaSideTool.SelectBotaTool(BOTATools.Search);
  1248. }
  1249. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  1250. {
  1251. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  1252. pdfViewControl.PDFView?.Zoom(newZoom);
  1253. }
  1254. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  1255. {
  1256. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  1257. pdfViewControl.PDFView?.Zoom(newZoom);
  1258. }
  1259. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  1260. {
  1261. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  1262. ShowViewSettings();
  1263. }
  1264. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  1265. {
  1266. if (PopupBorder.Visibility != Visibility.Visible)
  1267. {
  1268. PasswordUI.Visibility = Visibility.Collapsed;
  1269. FileInfoUI.Visibility = Visibility.Visible;
  1270. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  1271. PopupBorder.Visibility = Visibility.Visible;
  1272. }
  1273. else
  1274. {
  1275. FileInfoUI.Visibility = Visibility.Collapsed;
  1276. PopupBorder.Visibility = Visibility.Collapsed;
  1277. }
  1278. }
  1279. #endregion
  1280. }
  1281. }