MainPage.xaml.cs 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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. }