MainPage.xaml.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using Compdfkit_Tools.Data;
  5. using Compdfkit_Tools.Edit;
  6. using Compdfkit_Tools.Helper;
  7. using Compdfkit_Tools.PDFControl;
  8. using ComPDFKitViewer;
  9. using ComPDFKitViewer.AnnotEvent;
  10. using ComPDFKitViewer.PdfViewer;
  11. using Microsoft.Win32;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Diagnostics;
  16. using System.Drawing;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Runtime.CompilerServices;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Windows.Controls;
  24. using System.Windows.Controls.Primitives;
  25. using System.Windows.Data;
  26. using System.Windows.Documents;
  27. using System.Windows.Input;
  28. using System.Windows.Media;
  29. using System.Windows.Media.Imaging;
  30. using System.Windows.Navigation;
  31. using Path = System.IO.Path;
  32. namespace PDFViewer
  33. {
  34. public partial class MainPage : UserControl, INotifyPropertyChanged
  35. {
  36. #region Property
  37. private bool isFirstLoad = true;
  38. private string currentMode = "Viewer";
  39. private PDFViewControl passwordViewer;
  40. private PDFViewControl pdfViewControl = new PDFViewControl();
  41. private CPDFAnnotationControl pdfAnnotationControl = null;
  42. private UIElement pdfEditControl = null;
  43. private CPDFPageEditControl pageEditControl = null;
  44. private FromPropertyControl pdfFormControl = new FromPropertyControl();
  45. private PDFImageEditControl imageEditControl = null;
  46. private CPDFDisplaySettingsControl displayPanel = null;
  47. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  48. public bool CanSave
  49. {
  50. get
  51. {
  52. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  53. {
  54. return pdfViewControl.PDFView.UndoManager.CanSave;
  55. }
  56. return false;
  57. }
  58. }
  59. public bool CanUndo
  60. {
  61. get
  62. {
  63. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  64. {
  65. return pdfViewControl.PDFView.UndoManager.CanUndo;
  66. }
  67. return false;
  68. }
  69. }
  70. public bool CanRedo
  71. {
  72. get
  73. {
  74. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  75. {
  76. return pdfViewControl.PDFView.UndoManager.CanRedo;
  77. }
  78. return false;
  79. }
  80. }
  81. private PDFEditEvent lastPDFEditEvent = null;
  82. public event Func<string[], bool> CheckExistBeforeOpenFileEvent;
  83. public event PropertyChangedEventHandler PropertyChanged;
  84. public event EventHandler FileChangeEvent;
  85. private PDFEditEvent pdfTextCreateParam;
  86. private PDFTextEditControl textEditControl = new PDFTextEditControl();
  87. #endregion
  88. public MainPage()
  89. {
  90. InitializeComponent();
  91. DataContext = this;
  92. if (pdfFormControl != null)
  93. {
  94. pdfFormControl = new FromPropertyControl();
  95. }
  96. }
  97. #region Initial Load Unload
  98. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  99. {
  100. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  101. FormBarControl.InitWithPDFViewer(newPDFViewer.PDFView, pdfFormControl);
  102. pdfFormControl.SetPDFViewer(newPDFViewer.PDFView);
  103. pdfAnnotationControl.AnnotationCancel();
  104. AnnotationBarControl.ClearAllToolState();
  105. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  106. newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  107. newPDFViewer.CustomSignHandle = true;
  108. }
  109. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  110. {
  111. pdfAnnotationControl = new CPDFAnnotationControl();
  112. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  113. LoadDocument();
  114. }
  115. public void SetPDFViewer(PDFViewControl newPdfViewer)
  116. {
  117. if (newPdfViewer != null)
  118. {
  119. pdfViewControl = newPdfViewer;
  120. }
  121. }
  122. private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
  123. {
  124. CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
  125. CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent;
  126. CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
  127. CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent;
  128. CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
  129. CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent; ;
  130. }
  131. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  132. {
  133. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  134. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  135. }
  136. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  137. {
  138. CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
  139. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  140. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  141. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  142. }
  143. #endregion
  144. #region UI
  145. /// <summary>
  146. /// InfoChanged
  147. /// When the PDF zoom ratio changes, the value of the zoom control also changes.
  148. /// </summary>
  149. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  150. {
  151. if (e.Key == "Zoom")
  152. {
  153. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  154. }
  155. }
  156. private void EditLink_Click(object sender, RoutedEventArgs e)
  157. {
  158. RightPanelButton.IsChecked = true;
  159. PropertyContainer.Visibility = Visibility.Visible;
  160. }
  161. private double CheckZoomLevel(double zoom, bool IsGrowth)
  162. {
  163. double standardZoom = 100;
  164. if (zoom <= 0.01)
  165. {
  166. return 0.01;
  167. }
  168. if (zoom >= 10)
  169. {
  170. return 10;
  171. }
  172. zoom *= 100;
  173. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  174. {
  175. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  176. {
  177. standardZoom = zoomLevelList[i + 1];
  178. break;
  179. }
  180. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  181. {
  182. standardZoom = zoomLevelList[i];
  183. break;
  184. }
  185. }
  186. return standardZoom / 100;
  187. }
  188. private void PDFView_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
  189. {
  190. BotaSideTool.LoadAnnotationList();
  191. }
  192. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  193. {
  194. OnPropertyChanged(e.PropertyName);
  195. }
  196. private void PasswordUI_Closed(object sender, EventArgs e)
  197. {
  198. PopupBorder.Visibility = Visibility.Collapsed;
  199. PasswordUI.Visibility = Visibility.Collapsed;
  200. }
  201. private void PasswordUI_Canceled(object sender, EventArgs e)
  202. {
  203. PopupBorder.Visibility = Visibility.Collapsed;
  204. PasswordUI.Visibility = Visibility.Collapsed;
  205. }
  206. private void PasswordUI_Confirmed(object sender, string e)
  207. {
  208. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  209. {
  210. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  211. if (passwordViewer.PDFView.Document.IsLocked == false)
  212. {
  213. PasswordUI.SetShowError("", Visibility.Collapsed);
  214. PasswordUI.ClearPassword();
  215. PasswordUI.Visibility = Visibility.Collapsed;
  216. PopupBorder.Visibility = Visibility.Collapsed;
  217. pdfViewControl = passwordViewer;
  218. LoadDocument();
  219. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  220. }
  221. else
  222. {
  223. PasswordUI.SetShowError("error", Visibility.Visible);
  224. }
  225. }
  226. }
  227. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  228. {
  229. SaveFile();
  230. }
  231. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  232. {
  233. UIElement pageView = sender as UIElement;
  234. if (pageView != null)
  235. {
  236. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  237. }
  238. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  239. {
  240. if (imageEditControl == null)
  241. {
  242. imageEditControl = new PDFImageEditControl();
  243. }
  244. imageEditControl.InitWithPDFViewer(pdfViewControl.PDFView);
  245. imageEditControl.SetPDFImageEditData(lastPDFEditEvent);
  246. pdfEditControl = imageEditControl as UIElement;
  247. PropertyContainer.Child = pdfEditControl;
  248. //ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  249. }
  250. }
  251. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  252. {
  253. PasswordUI.Visibility = Visibility.Collapsed;
  254. FileInfoUI.Visibility = Visibility.Visible;
  255. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  256. PopupBorder.Visibility = Visibility.Visible;
  257. }
  258. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  259. {
  260. PopupBorder.Visibility = Visibility.Collapsed;
  261. }
  262. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  263. {
  264. ToggleButton senderBtn = sender as ToggleButton;
  265. if (senderBtn != null && pdfViewControl != null)
  266. {
  267. ClearPDFEditState(senderBtn);
  268. if (senderBtn.IsChecked == true)
  269. {
  270. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  271. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  272. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  273. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  274. pdfViewControl.PDFView?.ReloadDocument();
  275. PDFEditEvent createParam = new PDFEditEvent();
  276. createParam.EditType = CPDFEditType.EditText;
  277. createParam.IsBold = true;
  278. createParam.IsItalic = true;
  279. createParam.FontSize = 14;
  280. createParam.FontName = "Courier New";
  281. createParam.FontColor = Colors.Red;
  282. createParam.TextAlign = TextAlignType.AlignLeft;
  283. createParam.Transparency = 100;
  284. pdfViewControl.PDFView?.SetPDFEditParam(createParam);
  285. if (textEditControl == null)
  286. {
  287. textEditControl = new PDFTextEditControl();
  288. }
  289. textEditControl.SetPDFTextEditData(createParam);
  290. ExpandRightPropertyPanel(textEditControl, Visibility.Visible);
  291. pdfTextCreateParam = createParam;
  292. }
  293. else
  294. {
  295. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  296. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  297. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  298. pdfViewControl.PDFView?.ReloadDocument();
  299. }
  300. }
  301. }
  302. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  303. {
  304. ToggleButton senderBtn = sender as ToggleButton;
  305. if (senderBtn != null && pdfViewControl != null)
  306. {
  307. senderBtn.IsChecked = false;
  308. OpenFileDialog openFileDialog = new OpenFileDialog();
  309. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  310. if (openFileDialog.ShowDialog() == true)
  311. {
  312. ClearPDFEditState(senderBtn);
  313. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  314. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  315. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  316. pdfViewControl.PDFView?.ReloadDocument();
  317. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  318. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  319. pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  320. }
  321. }
  322. }
  323. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  324. {
  325. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  326. {
  327. pdfViewControl.PDFView.UndoManager?.Undo();
  328. }
  329. }
  330. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  331. {
  332. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  333. {
  334. pdfViewControl.PDFView.UndoManager?.Redo();
  335. }
  336. }
  337. private void ClearPDFEditState(ToggleButton ignoreBtn = null)
  338. {
  339. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  340. {
  341. PDFTextEditButton,
  342. PDFImageEditButton
  343. };
  344. foreach (ToggleButton item in clearBtnList)
  345. {
  346. if (ignoreBtn == item)
  347. {
  348. continue;
  349. }
  350. item.IsChecked = false;
  351. }
  352. }
  353. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  354. {
  355. AnnotationBarControl.ClearAllToolState();
  356. }
  357. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  358. {
  359. pdfAnnotationControl.AnnotationCancel();
  360. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  361. RightPanelButton.IsChecked = false;
  362. ViewSettingBtn.IsChecked = false;
  363. }
  364. #endregion
  365. #region ContextMenu
  366. /// <summary>
  367. /// Annotation Context menu
  368. /// </summary>
  369. private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e)
  370. {
  371. switch (e.CommandType)
  372. {
  373. case CommandType.Context:
  374. e.Handle = true;
  375. if (e.CommandTarget == TargetType.Annot)
  376. {
  377. e.Handle = true;
  378. e.PopupMenu = new ContextMenu();
  379. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  380. {
  381. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  382. MenuItem propertyMenu = new MenuItem();
  383. propertyMenu = new MenuItem();
  384. propertyMenu.Header = "Edit";
  385. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  386. propertyMenu.CommandParameter = e;
  387. e.PopupMenu.Items.Add(propertyMenu);
  388. }
  389. else if (e.PressOnAnnot)
  390. {
  391. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  392. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  393. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  394. }
  395. else if (e.PressOnMedia || e.PressOnSound)
  396. {
  397. e.Handle = true;
  398. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  399. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  400. }
  401. else if (e.PressOnSelectedText)
  402. {
  403. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  404. }
  405. else
  406. {
  407. e.Handle = true;
  408. e.PopupMenu = new ContextMenu();
  409. if (currentMode == "Annotations")
  410. {
  411. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  412. e.PopupMenu.Items.Add(new Separator());
  413. }
  414. MenuItem fitWidthMenu = new MenuItem();
  415. fitWidthMenu.Header = "Automatically Resize";
  416. fitWidthMenu.Click += (o, p) =>
  417. {
  418. if (pdfViewControl != null)
  419. {
  420. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  421. }
  422. };
  423. e.PopupMenu.Items.Add(fitWidthMenu);
  424. MenuItem fitSizeMenu = new MenuItem();
  425. fitSizeMenu.Header = "Actual Size";
  426. fitSizeMenu.Click += (o, p) =>
  427. {
  428. if (pdfViewControl != null)
  429. {
  430. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  431. }
  432. };
  433. e.PopupMenu.Items.Add(fitSizeMenu);
  434. MenuItem zoomInMenu = new MenuItem();
  435. zoomInMenu.Header = "Zoom In";
  436. zoomInMenu.Click += (o, p) =>
  437. {
  438. if (pdfViewControl != null)
  439. {
  440. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  441. pdfViewControl.PDFView?.Zoom(newZoom);
  442. }
  443. };
  444. e.PopupMenu.Items.Add(zoomInMenu);
  445. MenuItem zoomOutMenu = new MenuItem();
  446. zoomOutMenu.Header = "Zoom Out";
  447. zoomOutMenu.Click += (o, p) =>
  448. {
  449. if (pdfViewControl != null)
  450. {
  451. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  452. pdfViewControl.PDFView?.Zoom(newZoom);
  453. }
  454. };
  455. e.PopupMenu.Items.Add(zoomOutMenu);
  456. e.PopupMenu.Items.Add(new Separator());
  457. MenuItem singleView = new MenuItem();
  458. singleView.Header = "Single Page";
  459. singleView.Click += (o, p) =>
  460. {
  461. if (pdfViewControl != null)
  462. {
  463. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  464. }
  465. };
  466. e.PopupMenu.Items.Add(singleView);
  467. MenuItem singleContinuousView = new MenuItem();
  468. singleContinuousView.Header = "Single Page Continuous";
  469. singleContinuousView.Click += (o, p) =>
  470. {
  471. if (pdfViewControl != null)
  472. {
  473. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  474. }
  475. };
  476. e.PopupMenu.Items.Add(singleContinuousView);
  477. MenuItem doubleView = new MenuItem();
  478. doubleView.Header = "Two Pages";
  479. doubleView.Click += (o, p) =>
  480. {
  481. if (pdfViewControl != null)
  482. {
  483. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  484. }
  485. };
  486. e.PopupMenu.Items.Add(doubleView);
  487. MenuItem doubleContinuousView = new MenuItem();
  488. doubleContinuousView.Header = "Two Pages Continuous";
  489. doubleContinuousView.Click += (o, p) =>
  490. {
  491. if (pdfViewControl != null)
  492. {
  493. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  494. }
  495. };
  496. e.PopupMenu.Items.Add(doubleContinuousView);
  497. }
  498. }
  499. else if (e.CommandTarget == TargetType.ImageSelection)
  500. {
  501. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  502. {
  503. e.Handle = true;
  504. e.PopupMenu = new ContextMenu();
  505. MenuItem imageCopyMenu = new MenuItem();
  506. imageCopyMenu = new MenuItem();
  507. imageCopyMenu.Header = "Copy Images";
  508. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  509. imageCopyMenu.CommandParameter = e;
  510. e.PopupMenu.Items.Add(imageCopyMenu);
  511. MenuItem imageExtraMenu = new MenuItem();
  512. imageExtraMenu = new MenuItem();
  513. imageExtraMenu.Header = "Extract Images";
  514. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  515. imageExtraMenu.CommandParameter = e;
  516. e.PopupMenu.Items.Add(imageExtraMenu);
  517. }
  518. }
  519. break;
  520. case CommandType.Copy:
  521. e.DoCommand();
  522. break;
  523. case CommandType.Cut:
  524. case CommandType.Paste:
  525. case CommandType.Delete:
  526. e.DoCommand();
  527. break;
  528. default:
  529. break;
  530. }
  531. }
  532. /// <summary>
  533. /// TextEdit context menu.
  534. /// </summary>
  535. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  536. {
  537. editCommand.PopupMenu = new ContextMenu();
  538. if (lastPDFEditEvent != null)
  539. {
  540. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  541. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  542. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  543. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  544. }
  545. else
  546. {
  547. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  548. if (editCommand.TextAreaCopied)
  549. {
  550. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  551. }
  552. }
  553. }
  554. /// <summary>
  555. /// ImageEdit right-click menu.
  556. /// </summary>
  557. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  558. {
  559. editCommand.PopupMenu = new ContextMenu();
  560. if (lastPDFEditEvent != null)
  561. {
  562. MenuItem rotateLeftMenu = new MenuItem();
  563. rotateLeftMenu.Header = "Left Rotate";
  564. rotateLeftMenu.Click += (o, p) =>
  565. {
  566. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  567. {
  568. lastPDFEditEvent.Rotate = -90;
  569. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  570. imageEditControl?.SetImageThumb();
  571. }
  572. };
  573. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  574. MenuItem rotateRightMenu = new MenuItem();
  575. rotateRightMenu.Header = "Right Rotate";
  576. rotateRightMenu.Click += (o, p) =>
  577. {
  578. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  579. {
  580. lastPDFEditEvent.Rotate = 90;
  581. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  582. imageEditControl?.SetImageThumb();
  583. }
  584. };
  585. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  586. MenuItem replaceMenu = new MenuItem();
  587. replaceMenu.Header = "Replace";
  588. replaceMenu.Click += (o, p) =>
  589. {
  590. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  591. {
  592. OpenFileDialog openFileDialog = new OpenFileDialog();
  593. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  594. if (openFileDialog.ShowDialog() == true)
  595. {
  596. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  597. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  598. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  599. imageEditControl?.SetImageThumb();
  600. }
  601. }
  602. };
  603. editCommand.PopupMenu.Items.Add(replaceMenu);
  604. MenuItem exportMenu = new MenuItem();
  605. exportMenu.Header = "Export";
  606. exportMenu.Click += (o, p) =>
  607. {
  608. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  609. {
  610. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView.GetSelectedImages();
  611. if (imageDict != null && imageDict.Count > 0)
  612. {
  613. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  614. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  615. {
  616. string choosePath = folderBrowser.SelectedPath;
  617. string openPath = choosePath;
  618. foreach (int pageIndex in imageDict.Keys)
  619. {
  620. List<Bitmap> imageList = imageDict[pageIndex];
  621. foreach (Bitmap image in imageList)
  622. {
  623. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  624. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  625. openPath = savePath;
  626. }
  627. }
  628. Process.Start("explorer", "/select,\"" + openPath + "\"");
  629. }
  630. }
  631. }
  632. };
  633. editCommand.PopupMenu.Items.Add(exportMenu);
  634. MenuItem opacityMenu = new MenuItem();
  635. opacityMenu.Header = "Opacity";
  636. editCommand.PopupMenu.Items.Add(opacityMenu);
  637. AppendOpacityMenu(opacityMenu);
  638. MenuItem horizonMirror = new MenuItem();
  639. horizonMirror.Header = "HMirror";
  640. horizonMirror.Click += (o, p) =>
  641. {
  642. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  643. {
  644. lastPDFEditEvent.HorizontalMirror = true;
  645. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  646. }
  647. };
  648. editCommand.PopupMenu.Items.Add(horizonMirror);
  649. MenuItem verticalMirror = new MenuItem();
  650. verticalMirror.Header = "VMirror";
  651. verticalMirror.Click += (o, p) =>
  652. {
  653. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  654. {
  655. lastPDFEditEvent.VerticalMirror = true;
  656. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  657. }
  658. };
  659. editCommand.PopupMenu.Items.Add(verticalMirror);
  660. MenuItem cropMenu = new MenuItem();
  661. cropMenu.Header = "Crop";
  662. cropMenu.Click += (o, p) =>
  663. {
  664. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  665. {
  666. lastPDFEditEvent.ClipImage = true;
  667. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  668. }
  669. };
  670. editCommand.PopupMenu.Items.Add(cropMenu);
  671. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  672. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  673. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  674. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  675. }
  676. else
  677. {
  678. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  679. }
  680. }
  681. /// <summary>
  682. /// ImageEdit transparency submenu.
  683. /// </summary>
  684. private void AppendOpacityMenu(MenuItem parentMenu)
  685. {
  686. List<int> opacityList = new List<int>()
  687. {
  688. 25,50,75,100
  689. };
  690. foreach (int opacity in opacityList)
  691. {
  692. MenuItem opacityMenu = new MenuItem();
  693. opacityMenu.Header = string.Format("{0}%", opacity);
  694. opacityMenu.Click += (o, p) =>
  695. {
  696. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  697. {
  698. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  699. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  700. }
  701. };
  702. parentMenu.Items.Add(opacityMenu);
  703. }
  704. }
  705. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  706. {
  707. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  708. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  709. {
  710. string choosePath = folderDialog.SelectedPath;
  711. string openPath = choosePath;
  712. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  713. if (imageDict != null && imageDict.Count > 0)
  714. {
  715. foreach (int pageIndex in imageDict.Keys)
  716. {
  717. List<Bitmap> imageList = imageDict[pageIndex];
  718. foreach (Bitmap image in imageList)
  719. {
  720. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  721. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  722. openPath = savePath;
  723. }
  724. }
  725. }
  726. Process.Start("explorer", "/select,\"" + openPath + "\"");
  727. }
  728. }
  729. private void CopyImage_Click(object sender, RoutedEventArgs e)
  730. {
  731. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  732. if (imageDict != null && imageDict.Count > 0)
  733. {
  734. foreach (int pageIndex in imageDict.Keys)
  735. {
  736. List<Bitmap> imageList = imageDict[pageIndex];
  737. foreach (Bitmap image in imageList)
  738. {
  739. MemoryStream ms = new MemoryStream();
  740. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  741. BitmapImage imageData = new BitmapImage();
  742. imageData.BeginInit();
  743. imageData.StreamSource = ms;
  744. imageData.CacheOption = BitmapCacheOption.OnLoad;
  745. imageData.EndInit();
  746. imageData.Freeze();
  747. Clipboard.SetImage(imageData);
  748. break;
  749. }
  750. }
  751. }
  752. }
  753. #endregion
  754. #region Property changed
  755. protected void OnPropertyChanged([CallerMemberName] string name = null)
  756. {
  757. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  758. }
  759. #endregion
  760. #region Open and save file
  761. public void InitWithFilePath(string filePath)
  762. {
  763. pdfViewControl = new PDFViewControl();
  764. pdfViewControl.PDFView.InitDocument(filePath);
  765. }
  766. private void LoadDocument()
  767. {
  768. //Load the Document
  769. pdfViewControl.PDFView?.Load();
  770. //Highlight the link annotation
  771. pdfViewControl.PDFView?.SetShowLink(true);
  772. //Add PDFViewControl to PDFGrid
  773. PDFGrid.Child = pdfViewControl;
  774. // Register the InfoChanged event handler,
  775. // which is used to notify the CPDFScalingControl of real-time changes in the displayed number when the page size changes.
  776. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  777. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  778. //
  779. pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  780. pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  781. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  782. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  783. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  784. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  785. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  786. PasswordUI.Closed -= PasswordUI_Closed;
  787. PasswordUI.Canceled -= PasswordUI_Canceled;
  788. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  789. PasswordUI.Confirmed += PasswordUI_Confirmed;
  790. PasswordUI.Canceled += PasswordUI_Canceled;
  791. PasswordUI.Closed += PasswordUI_Closed;
  792. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  793. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  794. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  795. InitialPDFViewControl(pdfViewControl);
  796. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  797. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  798. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  799. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  800. ViewSettingBtn.IsChecked = false;
  801. PropertyContainer.Child = null;
  802. PropertyContainer.Visibility = Visibility.Collapsed;
  803. ModeComboBox.SelectedIndex = 0;
  804. }
  805. /// <summary>
  806. /// Save the file to another PDF file.
  807. /// </summary>
  808. public void SaveAsFile()
  809. {
  810. {
  811. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  812. {
  813. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  814. SaveFileDialog saveDialog = new SaveFileDialog();
  815. saveDialog.Filter = "(*.pdf)|*.pdf";
  816. saveDialog.DefaultExt = ".pdf";
  817. saveDialog.OverwritePrompt = true;
  818. if (saveDialog.ShowDialog() == true)
  819. {
  820. pdfDoc.WriteToFilePath(saveDialog.FileName);
  821. }
  822. }
  823. }
  824. }
  825. /// <summary>
  826. /// Save the file in the current path.
  827. /// </summary>
  828. public void SaveFile()
  829. {
  830. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  831. {
  832. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  833. if (pdfDoc.WriteToLoadedPath())
  834. {
  835. pdfViewControl.PDFView.UndoManager.CanSave = false;
  836. return;
  837. }
  838. SaveFileDialog saveDialog = new SaveFileDialog();
  839. saveDialog.Filter = "(*.pdf)|*.pdf";
  840. saveDialog.DefaultExt = ".pdf";
  841. saveDialog.OverwritePrompt = true;
  842. if (saveDialog.ShowDialog() == true)
  843. {
  844. pdfDoc.WriteToFilePath(saveDialog.FileName);
  845. }
  846. }
  847. }
  848. public void OpenFile()
  849. {
  850. string filePath = CommonHelper.GetFilePathOrEmpty();
  851. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  852. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  853. {
  854. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  855. {
  856. if (oldFilePath.ToLower() == filePath.ToLower())
  857. {
  858. return;
  859. }
  860. }
  861. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  862. {
  863. return;
  864. }
  865. passwordViewer = new PDFViewControl();
  866. passwordViewer.PDFView.InitDocument(filePath);
  867. if (passwordViewer.PDFView.Document == null)
  868. {
  869. MessageBox.Show("Open File Failed");
  870. return;
  871. }
  872. if (passwordViewer.PDFView.Document.IsLocked)
  873. {
  874. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  875. PasswordUI.ClearPassword();
  876. PopupBorder.Visibility = Visibility.Visible;
  877. PasswordUI.Visibility = Visibility.Visible;
  878. }
  879. else
  880. {
  881. pdfViewControl = passwordViewer;
  882. LoadDocument();
  883. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  884. }
  885. }
  886. }
  887. private void OpenFile_Click(object sender, RoutedEventArgs e)
  888. {
  889. OpenFile();
  890. }
  891. #endregion
  892. #region ToolPanel expand and collapse
  893. private void ExpandLeftPanel(bool isExpand)
  894. {
  895. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  896. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  897. if (isExpand)
  898. {
  899. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  900. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  901. }
  902. else
  903. {
  904. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  905. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  906. }
  907. }
  908. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  909. {
  910. ToggleButton expandBtn = sender as ToggleButton;
  911. if (expandBtn != null)
  912. {
  913. bool isExpand = expandBtn.IsChecked == true;
  914. ExpandLeftPanel(isExpand);
  915. }
  916. }
  917. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  918. {
  919. ExpandLeftPanel(true);
  920. LeftToolPanelButton.IsChecked = true;
  921. BotaSideTool.SelectBotaTool(BOTATools.Search);
  922. }
  923. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  924. {
  925. ToggleButton toggleButton = sender as ToggleButton;
  926. if (toggleButton != null)
  927. {
  928. if (toggleButton.IsChecked == true)
  929. {
  930. if (displayPanel == null)
  931. {
  932. displayPanel = new CPDFDisplaySettingsControl();
  933. }
  934. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  935. PropertyContainer.Child = displayPanel;
  936. PropertyContainer.Visibility = Visibility.Visible;
  937. if ((bool)RightPanelButton.IsChecked)
  938. {
  939. RightPanelButton.IsChecked = false;
  940. }
  941. }
  942. else
  943. {
  944. PropertyContainer.Child = null;
  945. PropertyContainer.Visibility = Visibility.Collapsed;
  946. }
  947. }
  948. }
  949. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  950. {
  951. PropertyContainer.Width = 260;
  952. PropertyContainer.Child = properytPanel;
  953. PropertyContainer.Visibility = visible;
  954. if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
  955. {
  956. RightPanelButton.IsChecked = false;
  957. }
  958. }
  959. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  960. {
  961. ToggleButton toggleButton = sender as ToggleButton;
  962. if (toggleButton != null)
  963. {
  964. if (toggleButton.IsChecked == true)
  965. {
  966. if (currentMode == "Annotation")
  967. {
  968. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  969. }
  970. else if (currentMode == "Edit")
  971. {
  972. ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  973. }
  974. else if (currentMode == "Forms")
  975. {
  976. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  977. }
  978. else
  979. {
  980. bool expandEmpty = true;
  981. if (currentMode == "Content Editor")
  982. {
  983. if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null)
  984. {
  985. if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  986. {
  987. if (textEditControl == null)
  988. {
  989. textEditControl = new PDFTextEditControl();
  990. }
  991. textEditControl.SetPDFTextEditData(pdfTextCreateParam);
  992. ExpandRightPropertyPanel(textEditControl, Visibility.Visible);
  993. expandEmpty = false;
  994. }
  995. }
  996. }
  997. if (expandEmpty)
  998. {
  999. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  1000. }
  1001. }
  1002. if ((bool)ViewSettingBtn.IsChecked)
  1003. {
  1004. ViewSettingBtn.IsChecked = false;
  1005. }
  1006. }
  1007. else
  1008. {
  1009. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1010. }
  1011. }
  1012. }
  1013. #endregion
  1014. #region Event handle
  1015. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  1016. {
  1017. pdfAnnotationControl.LoadAnnotationPanel(e);
  1018. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  1019. {
  1020. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  1021. RightPanelButton.IsChecked = true;
  1022. }
  1023. }
  1024. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  1025. {
  1026. PropertyContainer.Child = pdfAnnotationControl;
  1027. pdfAnnotationControl.SetAnnotEventData(e);
  1028. }
  1029. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  1030. {
  1031. if (e is WidgetSignArgs)
  1032. {
  1033. RightPanelButton.IsChecked = true;
  1034. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1035. pdfFormControl.SetPropertyForType(e, null);
  1036. }
  1037. else
  1038. {
  1039. RightPanelButton.IsChecked = false;
  1040. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1041. }
  1042. }
  1043. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  1044. {
  1045. if (e == null)
  1046. {
  1047. return;
  1048. }
  1049. if (e.EditType == CPDFEditType.EditText)
  1050. {
  1051. e.Handle = true;
  1052. PDFEditTextContextMenu(sender, e);
  1053. }
  1054. if (e.EditType == CPDFEditType.EditImage)
  1055. {
  1056. e.Handle = true;
  1057. PDFEditImageContextMenu(sender, e);
  1058. }
  1059. }
  1060. /// <summary>
  1061. /// Text and Image Selected Event
  1062. /// </summary>
  1063. private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e)
  1064. {
  1065. lastPDFEditEvent = e;
  1066. if ((bool)ViewSettingBtn.IsChecked)
  1067. {
  1068. ViewSettingBtn.IsChecked = false;
  1069. RightPanelButton.IsChecked = true;
  1070. }
  1071. if (e == null)
  1072. {
  1073. pdfEditControl = null;
  1074. PropertyContainer.Child = pdfEditControl;
  1075. if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null)
  1076. {
  1077. if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  1078. {
  1079. if (textEditControl == null)
  1080. {
  1081. textEditControl = new PDFTextEditControl();
  1082. }
  1083. textEditControl.SetPDFTextEditData(pdfTextCreateParam);
  1084. }
  1085. }
  1086. return;
  1087. }
  1088. if (Mouse.RightButton == MouseButtonState.Pressed)
  1089. {
  1090. return;
  1091. }
  1092. if (e.EditType == CPDFEditType.EditText)
  1093. {
  1094. if (textEditControl == null)
  1095. {
  1096. textEditControl = new PDFTextEditControl();
  1097. }
  1098. textEditControl.SetPDFTextEditData(e);
  1099. pdfEditControl = textEditControl as UIElement;
  1100. PropertyContainer.Child = pdfEditControl;
  1101. //ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  1102. return;
  1103. }
  1104. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  1105. {
  1106. RightPanelButton.IsChecked = true;
  1107. UIElement pageView = sender as UIElement;
  1108. if (pageView != null)
  1109. {
  1110. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; ;
  1111. }
  1112. return;
  1113. }
  1114. }
  1115. private void PDFView_FormEditHandler(object sender, List<AnnotEditEvent> e)
  1116. {
  1117. if (e != null && e.Count > 0)
  1118. {
  1119. AnnotEditEvent editEvent = e[e.Count - 1];
  1120. if (editEvent.EditAction == ActionType.Add)
  1121. {
  1122. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  1123. }
  1124. }
  1125. }
  1126. private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e)
  1127. {
  1128. if (e == null || e.IsAnnotCreateReset)
  1129. {
  1130. pdfFormControl.SetPropertyForType(null, null);
  1131. }
  1132. else
  1133. {
  1134. switch (e.GetAnnotTypes())
  1135. {
  1136. case AnnotArgsType.WidgetViewForm:
  1137. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  1138. switch (formArgs.WidgeType)
  1139. {
  1140. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1141. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1142. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1143. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1144. if ((bool)ViewSettingBtn.IsChecked)
  1145. {
  1146. ViewSettingBtn.IsChecked = false;
  1147. }
  1148. RightPanelButton.IsChecked = true;
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. pdfFormControl.SetPropertyForType(formArgs, e);
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  1159. {
  1160. ModeComboBox.SelectedIndex = 0;
  1161. }
  1162. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  1163. {
  1164. if (pdfViewControl == null || pdfViewControl.PDFView == null)
  1165. {
  1166. return;
  1167. }
  1168. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  1169. }
  1170. private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
  1171. {
  1172. pageEditControl.PageEdit(e);
  1173. }
  1174. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  1175. {
  1176. SaveFile();
  1177. }
  1178. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  1179. {
  1180. SaveAsFile();
  1181. }
  1182. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  1183. {
  1184. OpenFile();
  1185. }
  1186. #endregion
  1187. #region Change mode
  1188. /// <summary>
  1189. /// Actions performed when switching modes:
  1190. /// Separated into actions performed upon entering and exiting the mode.
  1191. /// </summary>
  1192. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1193. {
  1194. pdfTextCreateParam = null;
  1195. if (isFirstLoad)
  1196. {
  1197. isFirstLoad = false;
  1198. return;
  1199. }
  1200. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  1201. //Behavior when exit mode
  1202. switch (currentMode)
  1203. {
  1204. case "Viewer":
  1205. break;
  1206. /*
  1207. * Behavior when exit Edit mode
  1208. * Clear and collapse the ToolBar
  1209. * Clear And collapse the property panel.
  1210. * Change the mousemode to PanTool
  1211. * Unsubscribe the event handles
  1212. * Clear the undo history
  1213. */
  1214. case "Annotations":
  1215. AnnotationBarControl.Visibility = Visibility.Collapsed;
  1216. AnnotationBarControl.ClearAllToolState();
  1217. pdfAnnotationControl.ClearPanel();
  1218. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1219. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1220. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  1221. pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  1222. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  1223. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1224. break;
  1225. /*
  1226. * Behavior when exit edit mode:
  1227. * Collapsed the ToolBar
  1228. * Highlight the link annotation
  1229. * Set the mode to not allow edit
  1230. * Set the mouse mode to PanTool
  1231. * Reload Document
  1232. * Unsubscribe click event.
  1233. * Clear And collapse the property panel.
  1234. * Clear and collapse the ToolBar
  1235. * Clear undo history
  1236. * CPDFDisplaySettingsControl
  1237. * Enable the configuration of the split view.
  1238. */
  1239. case "Content Editor":
  1240. PDFEditTool.Visibility = Visibility.Collapsed;
  1241. pdfViewControl.PDFView?.SetShowLink(true);
  1242. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1243. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1244. pdfViewControl.PDFView?.ReloadDocument();
  1245. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  1246. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1247. ClearPDFEditState();
  1248. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1249. if (displayPanel != null)
  1250. {
  1251. displayPanel.SetSplitContainerVisibility(Visibility.Visible);
  1252. }
  1253. break;
  1254. /*
  1255. * Behavior when exit form mode:
  1256. * Clear and collapsed the ToolBar
  1257. * Clear And Collapse the property panel.
  1258. * Unsubscribe event.
  1259. * Clear undo history
  1260. */
  1261. case "Forms":
  1262. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1263. FormBarControl.ClearAllToolState();
  1264. FormBarControl.Visibility = Visibility.Collapsed;
  1265. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1266. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler;
  1267. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler;
  1268. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1269. break;
  1270. /*
  1271. * Unsubscribe event.
  1272. * Show Undo Redo Button
  1273. * Collapse PageEditBarControl
  1274. * Replace PDFGrid.Child to pdfViewControl.
  1275. * Reload Document
  1276. * Enable Button
  1277. */
  1278. case "Docs Editor":
  1279. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  1280. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  1281. PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
  1282. UndoRedoStackPanel.Visibility = Visibility.Visible;
  1283. PageEditBarControl.Visibility = Visibility.Collapsed;
  1284. PDFGrid.Child = pdfViewControl;
  1285. FloatPageTool.Visibility = Visibility.Visible;
  1286. pdfViewControl.PDFView.ReloadDocument();
  1287. LeftToolPanelButton.IsEnabled = true;
  1288. SearchButton.IsEnabled = true;
  1289. RightPanelButton.IsEnabled = true;
  1290. ViewSettingBtn.IsEnabled = true;
  1291. break;
  1292. default:
  1293. break;
  1294. }
  1295. /*
  1296. * Behavior when switching to Viewer mode:
  1297. * Hide the ToolBar
  1298. * Set mousemode to Viewer
  1299. */
  1300. if ((string)item.Content == "Viewer")
  1301. {
  1302. ToolBarContainer.Visibility = Visibility.Collapsed;
  1303. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  1304. }
  1305. /*
  1306. * Behavior when switching to Annotation mode:
  1307. * Show the ToolBar
  1308. * Show the AnnotationBar
  1309. */
  1310. else if ((string)item.Content == "Annotations")
  1311. {
  1312. ToolBarContainer.Visibility = Visibility.Visible;
  1313. AnnotationBarControl.Visibility = Visibility.Visible;
  1314. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  1315. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  1316. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  1317. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  1318. }
  1319. /*
  1320. * Behavior when switching to Edit mode:
  1321. * Show the ToolBar
  1322. * Show the EditBar
  1323. * Set the mode to allow editing of both text and images
  1324. * Set the mouse mode to PDFEdit
  1325. * Reload Document
  1326. * Set the splite mode to None and unable the split setting
  1327. */
  1328. else if ((string)item.Content == "Content Editor")
  1329. {
  1330. ToolBarContainer.Visibility = Visibility.Visible;
  1331. PDFEditTool.Visibility = Visibility.Visible;
  1332. PageEditBarControl.Visibility = Visibility.Collapsed;
  1333. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1334. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  1335. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1336. pdfViewControl.PDFView?.ReloadDocument();
  1337. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  1338. pdfViewControl.PDFView.SetSplitMode(SplitMode.None);
  1339. if (displayPanel != null)
  1340. {
  1341. displayPanel.SetSplitContainerVisibility(Visibility.Collapsed);
  1342. }
  1343. }
  1344. /*
  1345. * Behavior when switching to Form mode:
  1346. *
  1347. */
  1348. else if ((string)item.Content == "Forms")
  1349. {
  1350. ToolBarContainer.Visibility = Visibility.Visible;
  1351. FormBarControl.Visibility = Visibility.Visible;
  1352. pdfViewControl.PDFView?.SetShowLink(true);
  1353. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  1354. pdfViewControl.PDFView?.ReloadDocument();
  1355. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler;
  1356. pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler;
  1357. }
  1358. /*
  1359. *
  1360. */
  1361. else if ((string)item.Content == "Docs Editor")
  1362. {
  1363. ToolBarContainer.Visibility = Visibility.Visible;
  1364. if (pageEditControl == null)
  1365. {
  1366. pageEditControl = new CPDFPageEditControl();
  1367. }
  1368. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ;
  1369. pageEditControl.PageMoved += PageEditControl_PageMoved; ;
  1370. PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
  1371. pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
  1372. PDFGrid.Child = pageEditControl;
  1373. UndoRedoStackPanel.Visibility = Visibility.Collapsed;
  1374. PageEditBarControl.Visibility = Visibility.Visible;
  1375. LeftToolPanelButton.IsChecked = false;
  1376. LeftToolPanelButton.IsEnabled = false;
  1377. SearchButton.IsEnabled = false;
  1378. RightPanelButton.IsChecked = false;
  1379. RightPanelButton.IsEnabled = false;
  1380. ViewSettingBtn.IsChecked = false;
  1381. ViewSettingBtn.IsEnabled = false;
  1382. ExpandLeftPanel(false);
  1383. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1384. FloatPageTool.Visibility = Visibility.Collapsed;
  1385. }
  1386. currentMode = (string)item.Content;
  1387. }
  1388. #endregion
  1389. }
  1390. }