MainPage.xaml.cs 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  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 = new CPDFDisplaySettingsControl();
  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.InitWithPDFViewer(pdfViewControl.PDFView);
  1038. PropertyContainer.Child = displayPanel;
  1039. PropertyContainer.Visibility = Visibility.Visible;
  1040. if ((bool)RightPanelButton.IsChecked)
  1041. {
  1042. RightPanelButton.IsChecked = false;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. PropertyContainer.Child = null;
  1048. PropertyContainer.Visibility = Visibility.Collapsed;
  1049. }
  1050. }
  1051. }
  1052. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  1053. {
  1054. ShowViewSettings();
  1055. }
  1056. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  1057. {
  1058. PropertyContainer.Width = 260;
  1059. PropertyContainer.Child = properytPanel;
  1060. PropertyContainer.Visibility = visible;
  1061. if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
  1062. {
  1063. RightPanelButton.IsChecked = false;
  1064. }
  1065. }
  1066. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  1067. {
  1068. ControlRightPanel();
  1069. }
  1070. #endregion
  1071. #region Event handle
  1072. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  1073. {
  1074. pdfAnnotationControl.LoadAnnotationPanel(e);
  1075. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  1076. {
  1077. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  1078. RightPanelButton.IsChecked = true;
  1079. }
  1080. }
  1081. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  1082. {
  1083. if ((bool)ViewSettingBtn.IsChecked)
  1084. {
  1085. ViewSettingBtn.IsChecked = false;
  1086. RightPanelButton.IsChecked = true;
  1087. }
  1088. PropertyContainer.Child = pdfAnnotationControl;
  1089. pdfAnnotationControl.SetAnnotEventData(e);
  1090. }
  1091. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  1092. {
  1093. if (e is WidgetSignArgs)
  1094. {
  1095. RightPanelButton.IsChecked = true;
  1096. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1097. pdfFormControl.SetPropertyForType(e, null);
  1098. }
  1099. else
  1100. {
  1101. RightPanelButton.IsChecked = false;
  1102. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1103. }
  1104. }
  1105. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  1106. {
  1107. if (e == null)
  1108. {
  1109. return;
  1110. }
  1111. if (e.EditType == CPDFEditType.EditText)
  1112. {
  1113. e.Handle = true;
  1114. PDFEditTextContextMenu(sender, e);
  1115. }
  1116. if (e.EditType == CPDFEditType.EditImage)
  1117. {
  1118. e.Handle = true;
  1119. PDFEditImageContextMenu(sender, e);
  1120. }
  1121. }
  1122. /// <summary>
  1123. /// Text and Image Selected Event
  1124. /// </summary>
  1125. private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e)
  1126. {
  1127. lastPDFEditEvent = e;
  1128. if ((bool)ViewSettingBtn.IsChecked)
  1129. {
  1130. ViewSettingBtn.IsChecked = false;
  1131. RightPanelButton.IsChecked = true;
  1132. }
  1133. if (e == null)
  1134. {
  1135. pdfEditControl = null;
  1136. PropertyContainer.Child = pdfEditControl;
  1137. if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null)
  1138. {
  1139. if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  1140. {
  1141. if (textEditControl == null)
  1142. {
  1143. textEditControl = new PDFTextEditControl();
  1144. }
  1145. textEditControl.SetPDFTextEditData(pdfTextCreateParam);
  1146. PropertyContainer.Child = textEditControl;
  1147. }
  1148. }
  1149. return;
  1150. }
  1151. if (Mouse.RightButton == MouseButtonState.Pressed)
  1152. {
  1153. return;
  1154. }
  1155. if (e.EditType == CPDFEditType.EditText)
  1156. {
  1157. if (textEditControl == null)
  1158. {
  1159. textEditControl = new PDFTextEditControl();
  1160. }
  1161. textEditControl.SetPDFTextEditData(e);
  1162. pdfEditControl = textEditControl as UIElement;
  1163. PropertyContainer.Child = pdfEditControl;
  1164. return;
  1165. }
  1166. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  1167. {
  1168. RightPanelButton.IsChecked = true;
  1169. UIElement pageView = sender as UIElement;
  1170. if (pageView != null)
  1171. {
  1172. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; ;
  1173. }
  1174. return;
  1175. }
  1176. }
  1177. private void PDFView_FormEditHandler(object sender, List<AnnotEditEvent> e)
  1178. {
  1179. if (e != null && e.Count > 0)
  1180. {
  1181. AnnotEditEvent editEvent = e[e.Count - 1];
  1182. if (editEvent.EditAction == ActionType.Add)
  1183. {
  1184. if (pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs != null)
  1185. {
  1186. WidgetArgs widgetArgs = pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs as WidgetArgs;
  1187. if (widgetArgs != null)
  1188. {
  1189. switch (widgetArgs.WidgeType)
  1190. {
  1191. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1192. widgetArgs.FieldName = "Button" + GetTime();
  1193. break;
  1194. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1195. widgetArgs.FieldName = "Checkbox" + GetTime();
  1196. break;
  1197. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1198. widgetArgs.FieldName = "Radio button" + GetTime();
  1199. break;
  1200. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1201. widgetArgs.FieldName = "Text" + GetTime();
  1202. break;
  1203. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1204. widgetArgs.FieldName = "Combobox" + GetTime();
  1205. break;
  1206. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1207. widgetArgs.FieldName = "List" + GetTime();
  1208. break;
  1209. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1210. widgetArgs.FieldName = "Signature" + GetTime();
  1211. break;
  1212. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1213. break;
  1214. default:
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  1220. }
  1221. else if (editEvent.EditAction == ActionType.Del)
  1222. {
  1223. pdfFormControl.CleanProperty();
  1224. }
  1225. }
  1226. }
  1227. private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e)
  1228. {
  1229. if (e == null || e.IsAnnotCreateReset)
  1230. {
  1231. pdfFormControl.SetPropertyForType(null, null);
  1232. }
  1233. else
  1234. {
  1235. switch (e.GetAnnotTypes())
  1236. {
  1237. case AnnotArgsType.WidgetViewForm:
  1238. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  1239. switch (formArgs.WidgeType)
  1240. {
  1241. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1242. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1243. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1244. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1245. if ((bool)ViewSettingBtn.IsChecked)
  1246. {
  1247. ViewSettingBtn.IsChecked = false;
  1248. }
  1249. RightPanelButton.IsChecked = true;
  1250. break;
  1251. default:
  1252. break;
  1253. }
  1254. pdfFormControl.SetPropertyForType(formArgs, e);
  1255. break;
  1256. }
  1257. }
  1258. }
  1259. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  1260. {
  1261. ModeComboBox.SelectedIndex = 0;
  1262. }
  1263. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  1264. {
  1265. if (pdfViewControl == null || pdfViewControl.PDFView == null)
  1266. {
  1267. return;
  1268. }
  1269. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  1270. }
  1271. private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
  1272. {
  1273. pageEditControl.PageEdit(e);
  1274. }
  1275. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  1276. {
  1277. SaveFile();
  1278. }
  1279. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  1280. {
  1281. SaveAsFile();
  1282. }
  1283. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  1284. {
  1285. OpenFile();
  1286. }
  1287. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  1288. {
  1289. if (pdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
  1290. {
  1291. return;
  1292. }
  1293. if (Keyboard.Modifiers == ModifierKeys.Control)
  1294. {
  1295. if (e.Key == Key.Left)
  1296. {
  1297. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
  1298. e.Handled = true;
  1299. }
  1300. if (e.Key == Key.Right)
  1301. {
  1302. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
  1303. e.Handled = true;
  1304. }
  1305. if (e.Key == Key.Up)
  1306. {
  1307. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
  1308. e.Handled = true;
  1309. }
  1310. if (e.Key == Key.Down)
  1311. {
  1312. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
  1313. e.Handled = true;
  1314. }
  1315. }
  1316. if (Keyboard.Modifiers == ModifierKeys.Shift)
  1317. {
  1318. if (e.Key == Key.Left)
  1319. {
  1320. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
  1321. e.Handled = true;
  1322. }
  1323. if (e.Key == Key.Right)
  1324. {
  1325. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
  1326. e.Handled = true;
  1327. }
  1328. if (e.Key == Key.Up)
  1329. {
  1330. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
  1331. e.Handled = true;
  1332. }
  1333. if (e.Key == Key.Down)
  1334. {
  1335. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
  1336. e.Handled = true;
  1337. }
  1338. }
  1339. if (Keyboard.Modifiers == ModifierKeys.Alt)
  1340. {
  1341. if (e.SystemKey == Key.Up)
  1342. {
  1343. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, true);
  1344. e.Handled = true;
  1345. }
  1346. if (e.SystemKey == Key.Down)
  1347. {
  1348. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, true);
  1349. e.Handled = true;
  1350. }
  1351. }
  1352. if (Keyboard.Modifiers == ModifierKeys.None)
  1353. {
  1354. if (e.Key == Key.Left)
  1355. {
  1356. pdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
  1357. e.Handled = true;
  1358. }
  1359. if (e.Key == Key.Right)
  1360. {
  1361. pdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
  1362. e.Handled = true;
  1363. }
  1364. if (e.Key == Key.Up)
  1365. {
  1366. pdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
  1367. e.Handled = true;
  1368. }
  1369. if (e.Key == Key.Down)
  1370. {
  1371. pdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
  1372. e.Handled = true;
  1373. }
  1374. }
  1375. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  1376. {
  1377. if (e.Key == Key.Left)
  1378. {
  1379. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
  1380. e.Handled = true;
  1381. }
  1382. if (e.Key == Key.Right)
  1383. {
  1384. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
  1385. e.Handled = true;
  1386. }
  1387. if (e.Key == Key.Up)
  1388. {
  1389. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
  1390. e.Handled = true;
  1391. }
  1392. if (e.Key == Key.Down)
  1393. {
  1394. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
  1395. e.Handled = true;
  1396. }
  1397. }
  1398. }
  1399. #endregion
  1400. #region Change mode
  1401. /// <summary>
  1402. /// Actions performed when switching modes:
  1403. /// Separated into actions performed upon entering and exiting the mode.
  1404. /// </summary>
  1405. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1406. {
  1407. pdfTextCreateParam = null;
  1408. if (isFirstLoad)
  1409. {
  1410. isFirstLoad = false;
  1411. return;
  1412. }
  1413. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  1414. //Behavior when exit mode
  1415. switch (currentMode)
  1416. {
  1417. case "Viewer":
  1418. break;
  1419. /*
  1420. * Behavior when exit Edit mode
  1421. * Clear and collapse the ToolBar
  1422. * Clear And collapse the property panel.
  1423. * Change the mousemode to PanTool
  1424. * Unsubscribe the event handles
  1425. * Clear the undo history
  1426. */
  1427. case "Annotations":
  1428. AnnotationBarControl.Visibility = Visibility.Collapsed;
  1429. AnnotationBarControl.ClearAllToolState();
  1430. pdfAnnotationControl.ClearPanel();
  1431. if (!(bool)ViewSettingBtn.IsChecked)
  1432. {
  1433. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1434. }
  1435. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1436. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  1437. pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  1438. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  1439. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1440. break;
  1441. /*
  1442. * Behavior when exit edit mode:
  1443. * Collapsed the ToolBar
  1444. * Highlight the link annotation
  1445. * Set the mode to not allow edit
  1446. * Set the mouse mode to PanTool
  1447. * Reload Document
  1448. * Unsubscribe click event.
  1449. * Clear And collapse the property panel.
  1450. * Clear and collapse the ToolBar
  1451. * Clear undo history
  1452. * CPDFDisplaySettingsControl
  1453. * Enable the configuration of the split view.
  1454. */
  1455. case "Content Editor":
  1456. PDFEditTool.Visibility = Visibility.Collapsed;
  1457. pdfViewControl.PDFView?.SetShowLink(true);
  1458. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1459. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1460. pdfViewControl.PDFView?.ReloadDocument();
  1461. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  1462. if (!(bool)ViewSettingBtn.IsChecked)
  1463. {
  1464. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1465. }
  1466. ClearPDFEditState();
  1467. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1468. if (displayPanel != null)
  1469. {
  1470. displayPanel.SetVisibilityWhenContentEdit(Visibility.Visible);
  1471. }
  1472. break;
  1473. /*
  1474. * Behavior when exit form mode:
  1475. * Clear and collapsed the ToolBar
  1476. * Clear And Collapse the property panel.
  1477. * Unsubscribe event.
  1478. * Clear undo history
  1479. */
  1480. case "Forms":
  1481. if (!(bool)ViewSettingBtn.IsChecked)
  1482. {
  1483. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1484. }
  1485. FormBarControl.ClearAllToolState();
  1486. FormBarControl.Visibility = Visibility.Collapsed;
  1487. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1488. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler;
  1489. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler;
  1490. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1491. break;
  1492. /*
  1493. * Unsubscribe event.
  1494. * Show Undo Redo Button
  1495. * Collapse PageEditBarControl
  1496. * Replace PDFGrid.Child to pdfViewControl.
  1497. * Reload Document
  1498. * Enable Button
  1499. */
  1500. case "Docs Editor":
  1501. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  1502. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  1503. PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
  1504. UndoRedoStackPanel.Visibility = Visibility.Visible;
  1505. PageEditBarControl.Visibility = Visibility.Collapsed;
  1506. PDFGrid.Child = pdfViewControl;
  1507. FloatPageTool.Visibility = Visibility.Visible;
  1508. pdfViewControl.PDFView.ReloadDocument();
  1509. LeftToolPanelButton.IsEnabled = true;
  1510. SearchButton.IsEnabled = true;
  1511. RightPanelButton.IsEnabled = true;
  1512. ViewSettingBtn.IsEnabled = true;
  1513. break;
  1514. default:
  1515. break;
  1516. }
  1517. /*
  1518. * Behavior when switching to Viewer mode:
  1519. * Hide the ToolBar
  1520. * Set mousemode to Viewer
  1521. */
  1522. if ((string)item.Content == "Viewer")
  1523. {
  1524. ToolBarContainer.Visibility = Visibility.Collapsed;
  1525. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  1526. }
  1527. /*
  1528. * Behavior when switching to Annotation mode:
  1529. * Show the ToolBar
  1530. * Show the AnnotationBar
  1531. */
  1532. else if ((string)item.Content == "Annotations")
  1533. {
  1534. ToolBarContainer.Visibility = Visibility.Visible;
  1535. AnnotationBarControl.Visibility = Visibility.Visible;
  1536. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  1537. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  1538. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  1539. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  1540. }
  1541. /*
  1542. * Behavior when switching to Edit mode:
  1543. * Show the ToolBar
  1544. * Show the EditBar
  1545. * Set the mode to allow editing of both text and images
  1546. * Set the mouse mode to PDFEdit
  1547. * Reload Document
  1548. * Set the splite mode to None and unable the split setting
  1549. */
  1550. else if ((string)item.Content == "Content Editor")
  1551. {
  1552. ToolBarContainer.Visibility = Visibility.Visible;
  1553. PDFEditTool.Visibility = Visibility.Visible;
  1554. PageEditBarControl.Visibility = Visibility.Collapsed;
  1555. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1556. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  1557. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1558. pdfViewControl.PDFView?.ReloadDocument();
  1559. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  1560. pdfViewControl.PDFView.SetSplitMode(SplitMode.None);
  1561. if (displayPanel != null)
  1562. {
  1563. displayPanel.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  1564. }
  1565. }
  1566. /*
  1567. * Behavior when switching to Form mode:
  1568. *
  1569. */
  1570. else if ((string)item.Content == "Forms")
  1571. {
  1572. ToolBarContainer.Visibility = Visibility.Visible;
  1573. FormBarControl.Visibility = Visibility.Visible;
  1574. pdfViewControl.PDFView?.SetShowLink(true);
  1575. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  1576. pdfViewControl.PDFView?.ReloadDocument();
  1577. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler;
  1578. pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler;
  1579. }
  1580. /*
  1581. *
  1582. */
  1583. else if ((string)item.Content == "Docs Editor")
  1584. {
  1585. ToolBarContainer.Visibility = Visibility.Visible;
  1586. if (pageEditControl == null)
  1587. {
  1588. pageEditControl = new CPDFPageEditControl();
  1589. }
  1590. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ;
  1591. pageEditControl.PageMoved += PageEditControl_PageMoved; ;
  1592. PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
  1593. pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
  1594. PDFGrid.Child = pageEditControl;
  1595. UndoRedoStackPanel.Visibility = Visibility.Collapsed;
  1596. PageEditBarControl.Visibility = Visibility.Visible;
  1597. LeftToolPanelButton.IsChecked = false;
  1598. LeftToolPanelButton.IsEnabled = false;
  1599. SearchButton.IsEnabled = false;
  1600. RightPanelButton.IsChecked = false;
  1601. RightPanelButton.IsEnabled = false;
  1602. ViewSettingBtn.IsChecked = false;
  1603. ViewSettingBtn.IsEnabled = false;
  1604. ExpandLeftPanel(false);
  1605. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1606. FloatPageTool.Visibility = Visibility.Collapsed;
  1607. }
  1608. currentMode = (string)item.Content;
  1609. }
  1610. #endregion
  1611. #region Shortcut
  1612. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  1613. {
  1614. OpenFile();
  1615. }
  1616. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  1617. {
  1618. if (CanSave)
  1619. {
  1620. SaveFile();
  1621. }
  1622. }
  1623. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  1624. {
  1625. SaveAsFile();
  1626. }
  1627. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  1628. {
  1629. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
  1630. {
  1631. pdfViewControl.PDFView.UndoManager?.Undo();
  1632. }
  1633. }
  1634. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  1635. {
  1636. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
  1637. {
  1638. pdfViewControl.PDFView.UndoManager?.Redo();
  1639. }
  1640. }
  1641. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  1642. {
  1643. if (currentMode != "Docs Editor")
  1644. {
  1645. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  1646. ControlLeftPanel();
  1647. }
  1648. }
  1649. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  1650. {
  1651. if (currentMode != "Docs Editor")
  1652. {
  1653. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  1654. ControlRightPanel();
  1655. }
  1656. }
  1657. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  1658. {
  1659. if (currentMode != "Docs Editor")
  1660. {
  1661. ExpandLeftPanel(true);
  1662. LeftToolPanelButton.IsChecked = true;
  1663. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  1664. }
  1665. }
  1666. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  1667. {
  1668. if (currentMode != "Docs Editor")
  1669. {
  1670. ExpandLeftPanel(true);
  1671. LeftToolPanelButton.IsChecked = true;
  1672. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  1673. }
  1674. }
  1675. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  1676. {
  1677. if (currentMode != "Docs Editor")
  1678. {
  1679. ExpandLeftPanel(true);
  1680. LeftToolPanelButton.IsChecked = true;
  1681. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  1682. }
  1683. }
  1684. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  1685. {
  1686. if (currentMode != "Docs Editor")
  1687. {
  1688. ExpandLeftPanel(true);
  1689. LeftToolPanelButton.IsChecked = true;
  1690. BotaSideTool.SelectBotaTool(BOTATools.Annotation);
  1691. }
  1692. }
  1693. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  1694. {
  1695. if (currentMode != "Docs Editor")
  1696. {
  1697. ExpandLeftPanel(true);
  1698. LeftToolPanelButton.IsChecked = true;
  1699. BotaSideTool.SelectBotaTool(BOTATools.Search);
  1700. }
  1701. }
  1702. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  1703. {
  1704. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  1705. pdfViewControl.PDFView?.Zoom(newZoom);
  1706. }
  1707. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  1708. {
  1709. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  1710. pdfViewControl.PDFView?.Zoom(newZoom);
  1711. }
  1712. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  1713. {
  1714. if (currentMode != "Docs Editor")
  1715. {
  1716. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  1717. ShowViewSettings();
  1718. }
  1719. }
  1720. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  1721. {
  1722. if (PopupBorder.Visibility != Visibility.Visible)
  1723. {
  1724. PasswordUI.Visibility = Visibility.Collapsed;
  1725. FileInfoUI.Visibility = Visibility.Visible;
  1726. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  1727. PopupBorder.Visibility = Visibility.Visible;
  1728. }
  1729. else
  1730. {
  1731. FileInfoUI.Visibility = Visibility.Collapsed;
  1732. PopupBorder.Visibility = Visibility.Collapsed;
  1733. }
  1734. }
  1735. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  1736. {
  1737. if (currentMode == "Annotations")
  1738. {
  1739. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  1740. }
  1741. }
  1742. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  1743. {
  1744. if (currentMode == "Annotations")
  1745. {
  1746. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  1747. }
  1748. }
  1749. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  1750. {
  1751. if (currentMode == "Annotations")
  1752. {
  1753. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  1754. }
  1755. }
  1756. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  1757. {
  1758. if (currentMode == "Annotations")
  1759. {
  1760. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  1761. }
  1762. }
  1763. #endregion
  1764. }
  1765. }