MainPage.xaml.cs 79 KB

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