MainPage.xaml.cs 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  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.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Controls.Primitives;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using Path = System.IO.Path;
  27. using Point = System.Windows.Point;
  28. namespace PDFViewer
  29. {
  30. public partial class MainPage : UserControl, INotifyPropertyChanged
  31. {
  32. #region Property
  33. private bool isFirstLoad = true;
  34. private string currentMode = "Viewer";
  35. private PDFViewControl passwordViewer;
  36. private PDFViewControl pdfViewControl = new PDFViewControl();
  37. private CPDFAnnotationControl pdfAnnotationControl = null;
  38. private CPDFPageEditControl pageEditControl = null;
  39. private FromPropertyControl pdfFormControl = new FromPropertyControl();
  40. private PDFContentEditControl pdfContentEditControl = new PDFContentEditControl();
  41. private CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  42. private PDFEditEvent lastPDFEditEvent = null;
  43. public event Func<string[], bool> CheckExistBeforeOpenFileEvent;
  44. public event PropertyChangedEventHandler PropertyChanged;
  45. public event EventHandler FileChangeEvent;
  46. private PDFEditEvent pdfTextCreateParam;
  47. private KeyEventHandler KeyDownHandler;
  48. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  49. public bool CanSave
  50. {
  51. get
  52. {
  53. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  54. {
  55. return pdfViewControl.PDFView.UndoManager.CanSave;
  56. }
  57. return false;
  58. }
  59. }
  60. public bool CanUndo
  61. {
  62. get
  63. {
  64. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  65. {
  66. return pdfViewControl.PDFView.UndoManager.CanUndo;
  67. }
  68. return false;
  69. }
  70. }
  71. public bool CanRedo
  72. {
  73. get
  74. {
  75. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  76. {
  77. return pdfViewControl.PDFView.UndoManager.CanRedo;
  78. }
  79. return false;
  80. }
  81. }
  82. #endregion
  83. public MainPage()
  84. {
  85. InitializeComponent();
  86. DataContext = this;
  87. if (pdfFormControl != null)
  88. {
  89. pdfFormControl = new FromPropertyControl();
  90. }
  91. }
  92. #region Initial Load Unload
  93. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  94. {
  95. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  96. FormBarControl.InitWithPDFViewer(newPDFViewer.PDFView, pdfFormControl);
  97. pdfFormControl.SetPDFViewer(newPDFViewer.PDFView);
  98. pdfContentEditControl.InitWithPDFViewer(newPDFViewer.PDFView);
  99. pdfAnnotationControl.AnnotationCancel();
  100. AnnotationBarControl.ClearAllToolState();
  101. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  102. newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  103. newPDFViewer.CustomSignHandle = true;
  104. }
  105. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  106. {
  107. pdfAnnotationControl = new CPDFAnnotationControl();
  108. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  109. LoadDocument();
  110. }
  111. public void SetPDFViewer(PDFViewControl newPdfViewer)
  112. {
  113. if (newPdfViewer != null)
  114. {
  115. pdfViewControl = newPdfViewer;
  116. }
  117. }
  118. private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
  119. {
  120. CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
  121. CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent;
  122. CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
  123. CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent;
  124. CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
  125. CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent; ;
  126. }
  127. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  128. {
  129. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  130. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  131. }
  132. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  133. {
  134. 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 };
  135. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  136. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  137. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  138. }
  139. #endregion
  140. #region UI
  141. private string GetTime()
  142. {
  143. DateTime dateTime = DateTime.Now;
  144. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  145. }
  146. /// <summary>
  147. /// InfoChanged
  148. /// When the PDF zoom ratio changes, the value of the zoom control also changes.
  149. /// </summary>
  150. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  151. {
  152. if (e.Key == "Zoom")
  153. {
  154. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  155. }
  156. }
  157. private void EditLink_Click(object sender, RoutedEventArgs e)
  158. {
  159. RightPanelButton.IsChecked = true;
  160. PropertyContainer.Visibility = Visibility.Visible;
  161. }
  162. private double CheckZoomLevel(double zoom, bool IsGrowth)
  163. {
  164. double standardZoom = 100;
  165. if (zoom <= 0.01)
  166. {
  167. return 0.01;
  168. }
  169. if (zoom >= 10)
  170. {
  171. return 10;
  172. }
  173. zoom *= 100;
  174. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  175. {
  176. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  177. {
  178. standardZoom = zoomLevelList[i + 1];
  179. break;
  180. }
  181. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  182. {
  183. standardZoom = zoomLevelList[i];
  184. break;
  185. }
  186. }
  187. return standardZoom / 100;
  188. }
  189. private void PDFView_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
  190. {
  191. BotaSideTool.LoadAnnotationList();
  192. }
  193. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  194. {
  195. OnPropertyChanged(e.PropertyName);
  196. }
  197. private void PasswordUI_Closed(object sender, EventArgs e)
  198. {
  199. PopupBorder.Visibility = Visibility.Collapsed;
  200. PasswordUI.Visibility = Visibility.Collapsed;
  201. }
  202. private void PasswordUI_Canceled(object sender, EventArgs e)
  203. {
  204. PopupBorder.Visibility = Visibility.Collapsed;
  205. PasswordUI.Visibility = Visibility.Collapsed;
  206. }
  207. private void PasswordUI_Confirmed(object sender, string e)
  208. {
  209. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  210. {
  211. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  212. if (passwordViewer.PDFView.Document.IsLocked == false)
  213. {
  214. PasswordUI.SetShowError("", Visibility.Collapsed);
  215. PasswordUI.ClearPassword();
  216. PasswordUI.Visibility = Visibility.Collapsed;
  217. PopupBorder.Visibility = Visibility.Collapsed;
  218. pdfViewControl.PDFView.Document.Release();
  219. pdfViewControl = passwordViewer;
  220. LoadDocument();
  221. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  222. }
  223. else
  224. {
  225. PasswordUI.SetShowError("error", Visibility.Visible);
  226. }
  227. }
  228. }
  229. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  230. {
  231. SaveFile();
  232. }
  233. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  234. {
  235. UIElement pageView = sender as UIElement;
  236. if (pageView != null)
  237. {
  238. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  239. }
  240. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  241. {
  242. pdfContentEditControl.SetPDFImageEditData(lastPDFEditEvent);
  243. }
  244. }
  245. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  246. {
  247. PasswordUI.Visibility = Visibility.Collapsed;
  248. CPDFInfoControl.Visibility = Visibility.Visible;
  249. CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  250. CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  251. CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  252. PopupBorder.Visibility = Visibility.Visible;
  253. }
  254. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  255. {
  256. PopupBorder.Visibility = Visibility.Collapsed;
  257. }
  258. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  259. {
  260. ToggleButton senderBtn = sender as ToggleButton;
  261. if (senderBtn != null && pdfViewControl != null)
  262. {
  263. ClearPDFEditState(senderBtn);
  264. if (senderBtn.IsChecked == true)
  265. {
  266. PDFEditEvent createParam = new PDFEditEvent();
  267. createParam.EditType = CPDFEditType.EditText;
  268. createParam.IsBold = false;
  269. createParam.IsItalic = false;
  270. createParam.FontSize = 14;
  271. createParam.FontName = "Helvetica";
  272. createParam.FontColor = Colors.Black;
  273. createParam.TextAlign = TextAlignType.AlignLeft;
  274. createParam.Transparency = 255;
  275. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  276. {
  277. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  278. if (pdfDoc.PageCount > 0)
  279. {
  280. CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
  281. CPDFEditPage editPage = pdfPage.GetEditPage();
  282. editPage.BeginEdit(CPDFEditType.EditText);
  283. createParam.SystemFontNameList.AddRange(editPage.GetFontList());
  284. editPage.EndEdit();
  285. }
  286. }
  287. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  288. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  289. pdfViewControl.PDFView?.SetPDFEditParam(createParam);
  290. pdfContentEditControl.SetPDFTextEditData(createParam);
  291. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  292. if ((bool)ViewSettingBtn.IsChecked)
  293. {
  294. ViewSettingBtn.IsChecked = false;
  295. }
  296. RightPanelButton.IsChecked = true;
  297. pdfTextCreateParam = createParam;
  298. }
  299. else
  300. {
  301. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  302. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  303. RightPanelButton.IsChecked = false;
  304. if ((bool)ViewSettingBtn.IsChecked)
  305. {
  306. return;
  307. }
  308. pdfContentEditControl.ClearContentControl();
  309. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  310. }
  311. }
  312. }
  313. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  314. {
  315. ToggleButton senderBtn = sender as ToggleButton;
  316. if (senderBtn != null && pdfViewControl != null)
  317. {
  318. senderBtn.IsChecked = false;
  319. OpenFileDialog openFileDialog = new OpenFileDialog();
  320. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  321. if (openFileDialog.ShowDialog() == true)
  322. {
  323. ClearPDFEditState(senderBtn);
  324. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  325. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  326. RightPanelButton.IsChecked = false;
  327. if (PropertyContainer.Child != null && !(bool)ViewSettingBtn.IsChecked)
  328. {
  329. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  330. }
  331. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  332. pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  333. RightPanelButton.IsChecked = false;
  334. }
  335. }
  336. }
  337. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  338. {
  339. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  340. {
  341. pdfViewControl.PDFView.UndoManager?.Undo();
  342. }
  343. }
  344. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  345. {
  346. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  347. {
  348. pdfViewControl.PDFView.UndoManager?.Redo();
  349. }
  350. }
  351. private void ClearPDFEditState(ToggleButton ignoreBtn = null)
  352. {
  353. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  354. {
  355. PDFTextEditButton,
  356. PDFImageEditButton
  357. };
  358. foreach (ToggleButton item in clearBtnList)
  359. {
  360. if (ignoreBtn == item)
  361. {
  362. continue;
  363. }
  364. item.IsChecked = false;
  365. }
  366. }
  367. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  368. {
  369. AnnotationBarControl.ClearAllToolState();
  370. }
  371. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  372. {
  373. pdfAnnotationControl.AnnotationCancel();
  374. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  375. RightPanelButton.IsChecked = false;
  376. ViewSettingBtn.IsChecked = false;
  377. }
  378. #endregion
  379. #region ContextMenu
  380. /// <summary>
  381. /// Annotation Context menu
  382. /// </summary>
  383. private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e)
  384. {
  385. switch (e.CommandType)
  386. {
  387. case CommandType.Context:
  388. e.Handle = true;
  389. if (e.CommandTarget == TargetType.Annot)
  390. {
  391. e.Handle = true;
  392. e.PopupMenu = new ContextMenu();
  393. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  394. {
  395. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  396. MenuItem propertyMenu = new MenuItem();
  397. propertyMenu = new MenuItem();
  398. propertyMenu.Header = "Edit";
  399. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  400. propertyMenu.CommandParameter = e;
  401. e.PopupMenu.Items.Add(propertyMenu);
  402. }
  403. else if (e.PressOnAnnot)
  404. {
  405. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  406. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  407. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  408. }
  409. else if (e.PressOnMedia || e.PressOnSound)
  410. {
  411. e.Handle = true;
  412. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  413. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  414. }
  415. else if (e.PressOnSelectedText)
  416. {
  417. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  418. }
  419. else
  420. {
  421. e.Handle = true;
  422. e.PopupMenu = new ContextMenu();
  423. if (currentMode == "Annotations" || currentMode == "Forms")
  424. {
  425. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  426. e.PopupMenu.Items.Add(new Separator());
  427. }
  428. MenuItem fitWidthMenu = new MenuItem();
  429. fitWidthMenu.Header = "Automatically Resize";
  430. fitWidthMenu.Click += (o, p) =>
  431. {
  432. if (pdfViewControl != null)
  433. {
  434. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  435. }
  436. };
  437. e.PopupMenu.Items.Add(fitWidthMenu);
  438. MenuItem fitSizeMenu = new MenuItem();
  439. fitSizeMenu.Header = "Actual Size";
  440. fitSizeMenu.Click += (o, p) =>
  441. {
  442. if (pdfViewControl != null)
  443. {
  444. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  445. }
  446. };
  447. e.PopupMenu.Items.Add(fitSizeMenu);
  448. MenuItem zoomInMenu = new MenuItem();
  449. zoomInMenu.Header = "Zoom In";
  450. zoomInMenu.Click += (o, p) =>
  451. {
  452. if (pdfViewControl != null)
  453. {
  454. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  455. pdfViewControl.PDFView?.Zoom(newZoom);
  456. }
  457. };
  458. e.PopupMenu.Items.Add(zoomInMenu);
  459. MenuItem zoomOutMenu = new MenuItem();
  460. zoomOutMenu.Header = "Zoom Out";
  461. zoomOutMenu.Click += (o, p) =>
  462. {
  463. if (pdfViewControl != null)
  464. {
  465. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  466. pdfViewControl.PDFView?.Zoom(newZoom);
  467. }
  468. };
  469. e.PopupMenu.Items.Add(zoomOutMenu);
  470. e.PopupMenu.Items.Add(new Separator());
  471. MenuItem singleView = new MenuItem();
  472. singleView.Header = "Single Page";
  473. singleView.Click += (o, p) =>
  474. {
  475. if (pdfViewControl != null)
  476. {
  477. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  478. }
  479. };
  480. e.PopupMenu.Items.Add(singleView);
  481. MenuItem singleContinuousView = new MenuItem();
  482. singleContinuousView.Header = "Single Page Continuous";
  483. singleContinuousView.Click += (o, p) =>
  484. {
  485. if (pdfViewControl != null)
  486. {
  487. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  488. }
  489. };
  490. e.PopupMenu.Items.Add(singleContinuousView);
  491. MenuItem doubleView = new MenuItem();
  492. doubleView.Header = "Two Pages";
  493. doubleView.Click += (o, p) =>
  494. {
  495. if (pdfViewControl != null)
  496. {
  497. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  498. }
  499. };
  500. e.PopupMenu.Items.Add(doubleView);
  501. MenuItem doubleContinuousView = new MenuItem();
  502. doubleContinuousView.Header = "Two Pages Continuous";
  503. doubleContinuousView.Click += (o, p) =>
  504. {
  505. if (pdfViewControl != null)
  506. {
  507. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  508. }
  509. };
  510. e.PopupMenu.Items.Add(doubleContinuousView);
  511. }
  512. }
  513. else if (e.CommandTarget == TargetType.WidgetView)
  514. {
  515. e.Handle = true;
  516. e.PopupMenu = new ContextMenu();
  517. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  518. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  519. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  520. }
  521. else if (e.CommandTarget == TargetType.ImageSelection)
  522. {
  523. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  524. {
  525. e.Handle = true;
  526. e.PopupMenu = new ContextMenu();
  527. MenuItem imageCopyMenu = new MenuItem();
  528. imageCopyMenu = new MenuItem();
  529. imageCopyMenu.Header = "Copy Images";
  530. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  531. imageCopyMenu.CommandParameter = e;
  532. e.PopupMenu.Items.Add(imageCopyMenu);
  533. MenuItem imageExtraMenu = new MenuItem();
  534. imageExtraMenu = new MenuItem();
  535. imageExtraMenu.Header = "Extract Images";
  536. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  537. imageExtraMenu.CommandParameter = e;
  538. e.PopupMenu.Items.Add(imageExtraMenu);
  539. }
  540. }
  541. break;
  542. case CommandType.Copy:
  543. e.DoCommand();
  544. break;
  545. case CommandType.Cut:
  546. case CommandType.Paste:
  547. case CommandType.Delete:
  548. e.DoCommand();
  549. break;
  550. default:
  551. break;
  552. }
  553. }
  554. /// <summary>
  555. /// TextEdit context menu.
  556. /// </summary>
  557. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  558. {
  559. editCommand.PopupMenu = new ContextMenu();
  560. if (lastPDFEditEvent != null)
  561. {
  562. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  563. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  564. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  565. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  566. }
  567. else
  568. {
  569. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  570. if (editCommand.TextAreaCopied)
  571. {
  572. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  573. }
  574. }
  575. }
  576. /// <summary>
  577. /// ImageEdit right-click menu.
  578. /// </summary>
  579. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  580. {
  581. editCommand.PopupMenu = new ContextMenu();
  582. if (lastPDFEditEvent != null)
  583. {
  584. MenuItem rotateLeftMenu = new MenuItem();
  585. rotateLeftMenu.Header = "Left Rotate";
  586. rotateLeftMenu.Click += (o, p) =>
  587. {
  588. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  589. {
  590. lastPDFEditEvent.Rotate = -90;
  591. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  592. pdfContentEditControl.RefreshThumb();
  593. }
  594. };
  595. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  596. MenuItem rotateRightMenu = new MenuItem();
  597. rotateRightMenu.Header = "Right Rotate";
  598. rotateRightMenu.Click += (o, p) =>
  599. {
  600. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  601. {
  602. lastPDFEditEvent.Rotate = 90;
  603. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  604. pdfContentEditControl.RefreshThumb();
  605. }
  606. };
  607. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  608. MenuItem replaceMenu = new MenuItem();
  609. replaceMenu.Header = "Replace";
  610. replaceMenu.Click += (o, p) =>
  611. {
  612. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  613. {
  614. OpenFileDialog openFileDialog = new OpenFileDialog();
  615. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  616. if (openFileDialog.ShowDialog() == true)
  617. {
  618. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  619. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  620. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  621. pdfContentEditControl.RefreshThumb();
  622. }
  623. }
  624. };
  625. editCommand.PopupMenu.Items.Add(replaceMenu);
  626. MenuItem exportMenu = new MenuItem();
  627. exportMenu.Header = "Export";
  628. exportMenu.Click += (o, p) =>
  629. {
  630. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  631. {
  632. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView.GetSelectedImages();
  633. if (imageDict != null && imageDict.Count > 0)
  634. {
  635. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  636. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  637. {
  638. string choosePath = folderBrowser.SelectedPath;
  639. string openPath = choosePath;
  640. foreach (int pageIndex in imageDict.Keys)
  641. {
  642. List<Bitmap> imageList = imageDict[pageIndex];
  643. foreach (Bitmap image in imageList)
  644. {
  645. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  646. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  647. openPath = savePath;
  648. }
  649. }
  650. Process.Start("explorer", "/select,\"" + openPath + "\"");
  651. }
  652. }
  653. }
  654. };
  655. editCommand.PopupMenu.Items.Add(exportMenu);
  656. MenuItem opacityMenu = new MenuItem();
  657. opacityMenu.Header = "Opacity";
  658. editCommand.PopupMenu.Items.Add(opacityMenu);
  659. AppendOpacityMenu(opacityMenu);
  660. MenuItem horizonMirror = new MenuItem();
  661. horizonMirror.Header = "HMirror";
  662. horizonMirror.Click += (o, p) =>
  663. {
  664. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  665. {
  666. lastPDFEditEvent.HorizontalMirror = true;
  667. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  668. }
  669. };
  670. editCommand.PopupMenu.Items.Add(horizonMirror);
  671. MenuItem verticalMirror = new MenuItem();
  672. verticalMirror.Header = "VMirror";
  673. verticalMirror.Click += (o, p) =>
  674. {
  675. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  676. {
  677. lastPDFEditEvent.VerticalMirror = true;
  678. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  679. }
  680. };
  681. editCommand.PopupMenu.Items.Add(verticalMirror);
  682. MenuItem cropMenu = new MenuItem();
  683. cropMenu.Header = "Crop";
  684. cropMenu.Click += (o, p) =>
  685. {
  686. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  687. {
  688. lastPDFEditEvent.ClipImage = true;
  689. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  690. }
  691. };
  692. editCommand.PopupMenu.Items.Add(cropMenu);
  693. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  694. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  695. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  696. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  697. if (editCommand.TextAreaCopied)
  698. {
  699. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  700. }
  701. }
  702. else
  703. {
  704. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  705. }
  706. }
  707. /// <summary>
  708. /// ImageEdit transparency submenu.
  709. /// </summary>
  710. private void AppendOpacityMenu(MenuItem parentMenu)
  711. {
  712. List<int> opacityList = new List<int>()
  713. {
  714. 25,50,75,100
  715. };
  716. foreach (int opacity in opacityList)
  717. {
  718. MenuItem opacityMenu = new MenuItem();
  719. opacityMenu.Header = string.Format("{0}%", opacity);
  720. opacityMenu.Click += (o, p) =>
  721. {
  722. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  723. {
  724. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  725. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  726. }
  727. };
  728. parentMenu.Items.Add(opacityMenu);
  729. }
  730. }
  731. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  732. {
  733. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  734. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  735. {
  736. string choosePath = folderDialog.SelectedPath;
  737. string openPath = choosePath;
  738. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  739. if (imageDict != null && imageDict.Count > 0)
  740. {
  741. foreach (int pageIndex in imageDict.Keys)
  742. {
  743. List<Bitmap> imageList = imageDict[pageIndex];
  744. foreach (Bitmap image in imageList)
  745. {
  746. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  747. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  748. openPath = savePath;
  749. }
  750. }
  751. }
  752. Process.Start("explorer", "/select,\"" + openPath + "\"");
  753. }
  754. }
  755. private void CopyImage_Click(object sender, RoutedEventArgs e)
  756. {
  757. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  758. if (imageDict != null && imageDict.Count > 0)
  759. {
  760. foreach (int pageIndex in imageDict.Keys)
  761. {
  762. List<Bitmap> imageList = imageDict[pageIndex];
  763. foreach (Bitmap image in imageList)
  764. {
  765. MemoryStream ms = new MemoryStream();
  766. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  767. BitmapImage imageData = new BitmapImage();
  768. imageData.BeginInit();
  769. imageData.StreamSource = ms;
  770. imageData.CacheOption = BitmapCacheOption.OnLoad;
  771. imageData.EndInit();
  772. imageData.Freeze();
  773. Clipboard.SetImage(imageData);
  774. break;
  775. }
  776. }
  777. }
  778. }
  779. #endregion
  780. #region Property changed
  781. protected void OnPropertyChanged([CallerMemberName] string name = null)
  782. {
  783. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  784. }
  785. #endregion
  786. #region Open and save file
  787. public void InitWithFilePath(string filePath)
  788. {
  789. pdfViewControl = new PDFViewControl();
  790. pdfViewControl.PDFView.InitDocument(filePath);
  791. }
  792. private void LoadDocument()
  793. {
  794. if (pdfViewControl.PDFView.Document == null)
  795. {
  796. return;
  797. }
  798. //Load the Document
  799. pdfViewControl.PDFView?.Load();
  800. //Highlight the link annotation
  801. pdfViewControl.PDFView?.SetShowLink(true);
  802. //Add PDFViewControl to PDFGrid
  803. PDFGrid.Child = pdfViewControl;
  804. // Register the InfoChanged event handler,
  805. // which is used to notify the CPDFScalingControl of real-time changes in the displayed number when the page size changes.
  806. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  807. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  808. //
  809. pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  810. pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  811. //
  812. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  813. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  814. //Can undo, can redo
  815. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  816. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  817. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  818. PasswordUI.Closed -= PasswordUI_Closed;
  819. PasswordUI.Canceled -= PasswordUI_Canceled;
  820. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  821. PasswordUI.Confirmed += PasswordUI_Confirmed;
  822. PasswordUI.Canceled += PasswordUI_Canceled;
  823. PasswordUI.Closed += PasswordUI_Closed;
  824. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  825. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  826. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  827. InitialPDFViewControl(pdfViewControl);
  828. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  829. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  830. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  831. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  832. ViewSettingBtn.IsChecked = false;
  833. PropertyContainer.Child = null;
  834. PropertyContainer.Visibility = Visibility.Collapsed;
  835. ModeComboBox.SelectedIndex = 0;
  836. if (KeyDownHandler != null)
  837. {
  838. pdfViewControl.PDFView.RemoveHandler(KeyDownEvent, KeyDownHandler);
  839. }
  840. KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
  841. pdfViewControl.PDFView.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
  842. }
  843. /// <summary>
  844. /// Save the file to another PDF file.
  845. /// </summary>
  846. public void SaveAsFile()
  847. {
  848. {
  849. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  850. {
  851. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  852. SaveFileDialog saveDialog = new SaveFileDialog();
  853. saveDialog.Filter = "(*.pdf)|*.pdf";
  854. saveDialog.DefaultExt = ".pdf";
  855. saveDialog.OverwritePrompt = true;
  856. if (saveDialog.ShowDialog() == true)
  857. {
  858. pdfDoc.WriteToFilePath(saveDialog.FileName);
  859. }
  860. }
  861. }
  862. }
  863. /// <summary>
  864. /// Save the file in the current path.
  865. /// </summary>
  866. public void SaveFile()
  867. {
  868. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  869. {
  870. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  871. if (pdfDoc.WriteToLoadedPath())
  872. {
  873. pdfViewControl.PDFView.UndoManager.CanSave = false;
  874. return;
  875. }
  876. SaveFileDialog saveDialog = new SaveFileDialog();
  877. saveDialog.Filter = "(*.pdf)|*.pdf";
  878. saveDialog.DefaultExt = ".pdf";
  879. saveDialog.OverwritePrompt = true;
  880. if (saveDialog.ShowDialog() == true)
  881. {
  882. pdfDoc.WriteToFilePath(saveDialog.FileName);
  883. }
  884. }
  885. }
  886. public void OpenFile()
  887. {
  888. string filePath = CommonHelper.GetFilePathOrEmpty();
  889. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  890. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  891. {
  892. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  893. {
  894. if (oldFilePath.ToLower() == filePath.ToLower())
  895. {
  896. return;
  897. }
  898. }
  899. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  900. {
  901. return;
  902. }
  903. passwordViewer = new PDFViewControl();
  904. passwordViewer.PDFView.InitDocument(filePath);
  905. if (passwordViewer.PDFView.Document == null)
  906. {
  907. MessageBox.Show("Open File Failed");
  908. return;
  909. }
  910. if (passwordViewer.PDFView.Document.IsLocked)
  911. {
  912. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  913. PasswordUI.ClearPassword();
  914. PopupBorder.Visibility = Visibility.Visible;
  915. PasswordUI.Visibility = Visibility.Visible;
  916. }
  917. else
  918. {
  919. pdfViewControl.PDFView.Document.Release();
  920. pdfViewControl = passwordViewer;
  921. LoadDocument();
  922. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  923. }
  924. }
  925. }
  926. private void OpenFile_Click(object sender, RoutedEventArgs e)
  927. {
  928. OpenFile();
  929. }
  930. #endregion
  931. #region ToolPanel expand and collapse
  932. private void ExpandLeftPanel(bool isExpand)
  933. {
  934. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  935. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  936. if (isExpand)
  937. {
  938. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  939. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  940. }
  941. else
  942. {
  943. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  944. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  945. }
  946. }
  947. private void ControlLeftPanel()
  948. {
  949. if (LeftToolPanelButton != null)
  950. {
  951. bool isExpand = LeftToolPanelButton.IsChecked == true;
  952. ExpandLeftPanel(isExpand);
  953. }
  954. }
  955. private void ControlRightPanel()
  956. {
  957. if ((bool)ViewSettingBtn.IsChecked)
  958. {
  959. ViewSettingBtn.IsChecked = false;
  960. }
  961. if (RightPanelButton != null)
  962. {
  963. if (RightPanelButton.IsChecked == true)
  964. {
  965. if (currentMode == "Annotation")
  966. {
  967. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  968. }
  969. else if (currentMode == "Edit")
  970. {
  971. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  972. }
  973. else if (currentMode == "Forms")
  974. {
  975. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  976. }
  977. else if (currentMode == "Content Editor")
  978. {
  979. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  980. }
  981. else
  982. {
  983. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  984. }
  985. }
  986. else
  987. {
  988. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  989. }
  990. }
  991. }
  992. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  993. {
  994. ControlLeftPanel();
  995. }
  996. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  997. {
  998. ExpandLeftPanel(true);
  999. LeftToolPanelButton.IsChecked = true;
  1000. BotaSideTool.SelectBotaTool(BOTATools.Search);
  1001. }
  1002. private void ShowViewSettings()
  1003. {
  1004. if (ViewSettingBtn != null)
  1005. {
  1006. if (ViewSettingBtn.IsChecked == true)
  1007. {
  1008. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  1009. PropertyContainer.Child = displayPanel;
  1010. PropertyContainer.Visibility = Visibility.Visible;
  1011. if ((bool)RightPanelButton.IsChecked)
  1012. {
  1013. RightPanelButton.IsChecked = false;
  1014. }
  1015. }
  1016. else
  1017. {
  1018. PropertyContainer.Child = null;
  1019. PropertyContainer.Visibility = Visibility.Collapsed;
  1020. }
  1021. }
  1022. }
  1023. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  1024. {
  1025. ShowViewSettings();
  1026. }
  1027. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  1028. {
  1029. PropertyContainer.Width = 260;
  1030. PropertyContainer.Child = properytPanel;
  1031. PropertyContainer.Visibility = visible;
  1032. if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
  1033. {
  1034. RightPanelButton.IsChecked = false;
  1035. }
  1036. }
  1037. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  1038. {
  1039. ControlRightPanel();
  1040. }
  1041. #endregion
  1042. #region Event handle
  1043. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  1044. {
  1045. pdfAnnotationControl.LoadAnnotationPanel(e);
  1046. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  1047. {
  1048. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  1049. RightPanelButton.IsChecked = true;
  1050. }
  1051. }
  1052. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  1053. {
  1054. if ((bool)ViewSettingBtn.IsChecked)
  1055. {
  1056. ViewSettingBtn.IsChecked = false;
  1057. RightPanelButton.IsChecked = true;
  1058. }
  1059. PropertyContainer.Child = pdfAnnotationControl;
  1060. pdfAnnotationControl.SetAnnotEventData(e);
  1061. }
  1062. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  1063. {
  1064. if (e is WidgetSignArgs)
  1065. {
  1066. RightPanelButton.IsChecked = true;
  1067. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1068. pdfFormControl.SetPropertyForType(e, null);
  1069. }
  1070. else
  1071. {
  1072. RightPanelButton.IsChecked = false;
  1073. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1074. }
  1075. }
  1076. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  1077. {
  1078. if (e == null)
  1079. {
  1080. return;
  1081. }
  1082. if (e.EditType == CPDFEditType.EditText)
  1083. {
  1084. e.Handle = true;
  1085. PDFEditTextContextMenu(sender, e);
  1086. }
  1087. if (e.EditType == CPDFEditType.EditImage)
  1088. {
  1089. e.Handle = true;
  1090. PDFEditImageContextMenu(sender, e);
  1091. }
  1092. }
  1093. /// <summary>
  1094. /// Text and Image Selected Event
  1095. /// </summary>
  1096. private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e)
  1097. {
  1098. PropertyContainer.Child = pdfContentEditControl;
  1099. lastPDFEditEvent = e;
  1100. if ((bool)ViewSettingBtn.IsChecked)
  1101. {
  1102. ViewSettingBtn.IsChecked = false;
  1103. RightPanelButton.IsChecked = true;
  1104. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  1105. }
  1106. if (e == null)
  1107. {
  1108. PropertyContainer.Child = pdfContentEditControl;
  1109. if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null)
  1110. {
  1111. if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  1112. {
  1113. pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  1114. }
  1115. else if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  1116. {
  1117. pdfContentEditControl.ClearContentControl();
  1118. }
  1119. }
  1120. else
  1121. {
  1122. pdfContentEditControl.ClearContentControl();
  1123. }
  1124. return;
  1125. }
  1126. if (e.EditType == CPDFEditType.EditText)
  1127. {
  1128. pdfContentEditControl.SetPDFTextEditData(e, true);
  1129. return;
  1130. }
  1131. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  1132. {
  1133. UIElement pageView = sender as UIElement;
  1134. if (pageView != null)
  1135. {
  1136. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  1137. }
  1138. pdfContentEditControl.SetPDFImageEditData(e);
  1139. return;
  1140. }
  1141. }
  1142. private void PDFView_FormEditHandler(object sender, List<AnnotEditEvent> e)
  1143. {
  1144. if (e != null && e.Count > 0)
  1145. {
  1146. AnnotEditEvent editEvent = e[e.Count - 1];
  1147. if (editEvent.EditAction == ActionType.Add)
  1148. {
  1149. if (pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs != null)
  1150. {
  1151. WidgetArgs widgetArgs = pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs as WidgetArgs;
  1152. if (widgetArgs != null)
  1153. {
  1154. switch (widgetArgs.WidgeType)
  1155. {
  1156. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1157. widgetArgs.FieldName = "Button" + GetTime();
  1158. break;
  1159. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1160. widgetArgs.FieldName = "Checkbox" + GetTime();
  1161. break;
  1162. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1163. widgetArgs.FieldName = "Radio button" + GetTime();
  1164. break;
  1165. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1166. widgetArgs.FieldName = "Text" + GetTime();
  1167. break;
  1168. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1169. widgetArgs.FieldName = "Combobox" + GetTime();
  1170. break;
  1171. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1172. widgetArgs.FieldName = "List" + GetTime();
  1173. break;
  1174. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1175. widgetArgs.FieldName = "Signature" + GetTime();
  1176. break;
  1177. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  1185. }
  1186. else if (editEvent.EditAction == ActionType.Del)
  1187. {
  1188. pdfFormControl.CleanProperty();
  1189. }
  1190. }
  1191. }
  1192. private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e)
  1193. {
  1194. if (e == null || e.IsAnnotCreateReset)
  1195. {
  1196. pdfFormControl.SetPropertyForType(null, null);
  1197. }
  1198. else
  1199. {
  1200. switch (e.GetAnnotTypes())
  1201. {
  1202. case AnnotArgsType.WidgetViewForm:
  1203. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  1204. switch (formArgs.WidgeType)
  1205. {
  1206. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1207. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1208. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1209. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1210. if ((bool)ViewSettingBtn.IsChecked)
  1211. {
  1212. ViewSettingBtn.IsChecked = false;
  1213. }
  1214. RightPanelButton.IsChecked = true;
  1215. break;
  1216. default:
  1217. break;
  1218. }
  1219. pdfFormControl.SetPropertyForType(formArgs, e);
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  1225. {
  1226. ModeComboBox.SelectedIndex = 0;
  1227. }
  1228. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  1229. {
  1230. if (pdfViewControl == null || pdfViewControl.PDFView == null)
  1231. {
  1232. return;
  1233. }
  1234. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  1235. }
  1236. private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
  1237. {
  1238. pageEditControl.PageEdit(e);
  1239. }
  1240. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  1241. {
  1242. SaveFile();
  1243. }
  1244. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  1245. {
  1246. SaveAsFile();
  1247. }
  1248. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  1249. {
  1250. OpenFile();
  1251. }
  1252. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  1253. {
  1254. if (pdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
  1255. {
  1256. return;
  1257. }
  1258. if (Keyboard.Modifiers == ModifierKeys.Control)
  1259. {
  1260. if (e.Key == Key.Left)
  1261. {
  1262. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
  1263. e.Handled = true;
  1264. }
  1265. if (e.Key == Key.Right)
  1266. {
  1267. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
  1268. e.Handled = true;
  1269. }
  1270. if (e.Key == Key.Up)
  1271. {
  1272. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
  1273. e.Handled = true;
  1274. }
  1275. if (e.Key == Key.Down)
  1276. {
  1277. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
  1278. e.Handled = true;
  1279. }
  1280. }
  1281. if (Keyboard.Modifiers == ModifierKeys.Shift)
  1282. {
  1283. if (e.Key == Key.Left)
  1284. {
  1285. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
  1286. e.Handled = true;
  1287. }
  1288. if (e.Key == Key.Right)
  1289. {
  1290. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
  1291. e.Handled = true;
  1292. }
  1293. if (e.Key == Key.Up)
  1294. {
  1295. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
  1296. e.Handled = true;
  1297. }
  1298. if (e.Key == Key.Down)
  1299. {
  1300. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
  1301. e.Handled = true;
  1302. }
  1303. }
  1304. if (Keyboard.Modifiers == ModifierKeys.Alt)
  1305. {
  1306. if (e.SystemKey == Key.Up)
  1307. {
  1308. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false);
  1309. e.Handled = true;
  1310. }
  1311. if (e.SystemKey == Key.Down)
  1312. {
  1313. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false);
  1314. e.Handled = true;
  1315. }
  1316. }
  1317. if (Keyboard.Modifiers == ModifierKeys.None)
  1318. {
  1319. if (e.Key == Key.Left)
  1320. {
  1321. pdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
  1322. e.Handled = true;
  1323. }
  1324. if (e.Key == Key.Right)
  1325. {
  1326. pdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
  1327. e.Handled = true;
  1328. }
  1329. if (e.Key == Key.Up)
  1330. {
  1331. pdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
  1332. e.Handled = true;
  1333. }
  1334. if (e.Key == Key.Down)
  1335. {
  1336. pdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
  1337. e.Handled = true;
  1338. }
  1339. }
  1340. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  1341. {
  1342. if (e.Key == Key.Left)
  1343. {
  1344. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
  1345. e.Handled = true;
  1346. }
  1347. if (e.Key == Key.Right)
  1348. {
  1349. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
  1350. e.Handled = true;
  1351. }
  1352. if (e.Key == Key.Up)
  1353. {
  1354. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
  1355. e.Handled = true;
  1356. }
  1357. if (e.Key == Key.Down)
  1358. {
  1359. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
  1360. e.Handled = true;
  1361. }
  1362. }
  1363. }
  1364. #endregion
  1365. #region Change mode
  1366. /// <summary>
  1367. /// Actions performed when switching modes:
  1368. /// Separated into actions performed upon entering and exiting the mode.
  1369. /// </summary>
  1370. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1371. {
  1372. pdfTextCreateParam = null;
  1373. if (isFirstLoad)
  1374. {
  1375. isFirstLoad = false;
  1376. return;
  1377. }
  1378. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  1379. //Behavior when exit mode
  1380. switch (currentMode)
  1381. {
  1382. case "Viewer":
  1383. break;
  1384. /*
  1385. * Behavior when exit Edit mode
  1386. * Clear and collapse the ToolBar
  1387. * Clear And collapse the property panel.
  1388. * Change the mousemode to PanTool
  1389. * Unsubscribe the event handles
  1390. * Clear the undo history
  1391. */
  1392. case "Annotations":
  1393. AnnotationBarControl.Visibility = Visibility.Collapsed;
  1394. AnnotationBarControl.ClearAllToolState();
  1395. pdfAnnotationControl.ClearPanel();
  1396. if (!(bool)ViewSettingBtn.IsChecked)
  1397. {
  1398. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1399. }
  1400. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1401. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  1402. pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  1403. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  1404. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1405. break;
  1406. /*
  1407. * Behavior when exit edit mode:
  1408. * Collapsed the ToolBar
  1409. * Highlight the link annotation
  1410. * Set the mode to not allow edit
  1411. * Set the mouse mode to PanTool
  1412. * Reload Document
  1413. * Unsubscribe click event.
  1414. * Clear And collapse the property panel.
  1415. * Clear and collapse the ToolBar
  1416. * Clear undo history
  1417. * CPDFDisplaySettingsControl
  1418. * Enable the configuration of the split view.
  1419. */
  1420. case "Content Editor":
  1421. PDFEditTool.Visibility = Visibility.Collapsed;
  1422. pdfViewControl.PDFView?.SetShowLink(true);
  1423. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1424. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1425. pdfViewControl.PDFView?.ReloadDocument();
  1426. pdfContentEditControl.ClearPanel();
  1427. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  1428. if (!(bool)ViewSettingBtn.IsChecked)
  1429. {
  1430. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1431. }
  1432. ClearPDFEditState();
  1433. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1434. if (displayPanel != null)
  1435. {
  1436. displayPanel.SetVisibilityWhenContentEdit(Visibility.Visible);
  1437. }
  1438. break;
  1439. /*
  1440. * Behavior when exit form mode:
  1441. * Clear and collapsed the ToolBar
  1442. * Clear And Collapse the property panel.
  1443. * Unsubscribe event.
  1444. * Clear undo history
  1445. */
  1446. case "Forms":
  1447. if (!(bool)ViewSettingBtn.IsChecked)
  1448. {
  1449. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1450. }
  1451. FormBarControl.ClearAllToolState();
  1452. FormBarControl.Visibility = Visibility.Collapsed;
  1453. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1454. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler;
  1455. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler;
  1456. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1457. break;
  1458. /*
  1459. * Unsubscribe event.
  1460. * Show Undo Redo Button
  1461. * Collapse PageEditBarControl
  1462. * Replace PDFGrid.Child to pdfViewControl.
  1463. * Reload Document
  1464. * Enable Button
  1465. */
  1466. case "Docs Editor":
  1467. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  1468. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  1469. PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
  1470. UndoRedoStackPanel.Visibility = Visibility.Visible;
  1471. PageEditBarControl.Visibility = Visibility.Collapsed;
  1472. PDFGrid.Child = pdfViewControl;
  1473. FloatPageTool.Visibility = Visibility.Visible;
  1474. pdfViewControl.PDFView.ReloadDocument();
  1475. LeftToolPanelButton.IsEnabled = true;
  1476. SearchButton.IsEnabled = true;
  1477. RightPanelButton.IsEnabled = true;
  1478. ViewSettingBtn.IsEnabled = true;
  1479. break;
  1480. default:
  1481. break;
  1482. }
  1483. /*
  1484. * Behavior when switching to Viewer mode:
  1485. * Hide the ToolBar
  1486. * Set mousemode to Viewer
  1487. */
  1488. if ((string)item.Content == "Viewer")
  1489. {
  1490. ToolBarContainer.Visibility = Visibility.Collapsed;
  1491. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  1492. }
  1493. /*
  1494. * Behavior when switching to Annotation mode:
  1495. * Show the ToolBar
  1496. * Show the AnnotationBar
  1497. */
  1498. else if ((string)item.Content == "Annotations")
  1499. {
  1500. ToolBarContainer.Visibility = Visibility.Visible;
  1501. AnnotationBarControl.Visibility = Visibility.Visible;
  1502. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  1503. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  1504. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  1505. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  1506. }
  1507. /*
  1508. * Behavior when switching to Edit mode:
  1509. * Show the ToolBar
  1510. * Show the EditBar
  1511. * Set the mode to allow editing of both text and images
  1512. * Set the mouse mode to PDFEdit
  1513. * Reload Document
  1514. * Set the splite mode to None and unable the split setting
  1515. */
  1516. else if ((string)item.Content == "Content Editor")
  1517. {
  1518. ToolBarContainer.Visibility = Visibility.Visible;
  1519. PDFEditTool.Visibility = Visibility.Visible;
  1520. PageEditBarControl.Visibility = Visibility.Collapsed;
  1521. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1522. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  1523. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1524. pdfViewControl.PDFView?.ReloadDocument();
  1525. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  1526. pdfViewControl.PDFView.SetSplitMode(SplitMode.None);
  1527. if (displayPanel != null)
  1528. {
  1529. displayPanel.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  1530. }
  1531. }
  1532. /*
  1533. * Behavior when switching to Form mode:
  1534. *
  1535. */
  1536. else if ((string)item.Content == "Forms")
  1537. {
  1538. ToolBarContainer.Visibility = Visibility.Visible;
  1539. FormBarControl.Visibility = Visibility.Visible;
  1540. pdfViewControl.PDFView?.SetShowLink(true);
  1541. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  1542. pdfViewControl.PDFView?.ReloadDocument();
  1543. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler;
  1544. pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler;
  1545. }
  1546. /*
  1547. *
  1548. */
  1549. else if ((string)item.Content == "Docs Editor")
  1550. {
  1551. ToolBarContainer.Visibility = Visibility.Visible;
  1552. if (pageEditControl == null)
  1553. {
  1554. pageEditControl = new CPDFPageEditControl();
  1555. }
  1556. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ;
  1557. pageEditControl.PageMoved += PageEditControl_PageMoved; ;
  1558. PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
  1559. pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
  1560. PDFGrid.Child = pageEditControl;
  1561. UndoRedoStackPanel.Visibility = Visibility.Collapsed;
  1562. PageEditBarControl.Visibility = Visibility.Visible;
  1563. LeftToolPanelButton.IsChecked = false;
  1564. LeftToolPanelButton.IsEnabled = false;
  1565. SearchButton.IsEnabled = false;
  1566. RightPanelButton.IsChecked = false;
  1567. RightPanelButton.IsEnabled = false;
  1568. ViewSettingBtn.IsChecked = false;
  1569. ViewSettingBtn.IsEnabled = false;
  1570. ExpandLeftPanel(false);
  1571. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1572. FloatPageTool.Visibility = Visibility.Collapsed;
  1573. }
  1574. currentMode = (string)item.Content;
  1575. }
  1576. #endregion
  1577. #region Shortcut
  1578. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  1579. {
  1580. OpenFile();
  1581. }
  1582. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  1583. {
  1584. if (CanSave)
  1585. {
  1586. SaveFile();
  1587. }
  1588. }
  1589. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  1590. {
  1591. SaveAsFile();
  1592. }
  1593. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  1594. {
  1595. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
  1596. {
  1597. pdfViewControl.PDFView.UndoManager?.Undo();
  1598. }
  1599. }
  1600. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  1601. {
  1602. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
  1603. {
  1604. pdfViewControl.PDFView.UndoManager?.Redo();
  1605. }
  1606. }
  1607. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  1608. {
  1609. if (currentMode != "Docs Editor")
  1610. {
  1611. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  1612. ControlLeftPanel();
  1613. }
  1614. }
  1615. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  1616. {
  1617. if (currentMode != "Docs Editor")
  1618. {
  1619. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  1620. ControlRightPanel();
  1621. }
  1622. }
  1623. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  1624. {
  1625. if (currentMode != "Docs Editor")
  1626. {
  1627. ExpandLeftPanel(true);
  1628. LeftToolPanelButton.IsChecked = true;
  1629. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  1630. }
  1631. }
  1632. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  1633. {
  1634. if (currentMode != "Docs Editor")
  1635. {
  1636. ExpandLeftPanel(true);
  1637. LeftToolPanelButton.IsChecked = true;
  1638. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  1639. }
  1640. }
  1641. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  1642. {
  1643. if (currentMode != "Docs Editor")
  1644. {
  1645. ExpandLeftPanel(true);
  1646. LeftToolPanelButton.IsChecked = true;
  1647. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  1648. }
  1649. }
  1650. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  1651. {
  1652. if (currentMode != "Docs Editor")
  1653. {
  1654. ExpandLeftPanel(true);
  1655. LeftToolPanelButton.IsChecked = true;
  1656. BotaSideTool.SelectBotaTool(BOTATools.Annotation);
  1657. }
  1658. }
  1659. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  1660. {
  1661. if (currentMode != "Docs Editor")
  1662. {
  1663. ExpandLeftPanel(true);
  1664. LeftToolPanelButton.IsChecked = true;
  1665. BotaSideTool.SelectBotaTool(BOTATools.Search);
  1666. }
  1667. }
  1668. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  1669. {
  1670. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  1671. pdfViewControl.PDFView?.Zoom(newZoom);
  1672. }
  1673. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  1674. {
  1675. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  1676. pdfViewControl.PDFView?.Zoom(newZoom);
  1677. }
  1678. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  1679. {
  1680. if (currentMode != "Docs Editor")
  1681. {
  1682. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  1683. ShowViewSettings();
  1684. }
  1685. }
  1686. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  1687. {
  1688. if (PopupBorder.Visibility != Visibility.Visible)
  1689. {
  1690. PasswordUI.Visibility = Visibility.Collapsed;
  1691. CPDFInfoControl.Visibility = Visibility.Visible;
  1692. CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  1693. CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  1694. CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  1695. PopupBorder.Visibility = Visibility.Visible;
  1696. }
  1697. else
  1698. {
  1699. CPDFInfoControl.Visibility = Visibility.Collapsed;
  1700. PopupBorder.Visibility = Visibility.Collapsed;
  1701. this.Focus();
  1702. }
  1703. }
  1704. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  1705. {
  1706. if (currentMode == "Annotations")
  1707. {
  1708. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  1709. }
  1710. }
  1711. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  1712. {
  1713. if (currentMode == "Annotations")
  1714. {
  1715. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  1716. }
  1717. }
  1718. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  1719. {
  1720. if (currentMode == "Annotations")
  1721. {
  1722. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  1723. }
  1724. }
  1725. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  1726. {
  1727. if (currentMode == "Annotations")
  1728. {
  1729. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  1730. }
  1731. }
  1732. #endregion
  1733. }
  1734. }