CPDFViewerTool.Command.cs 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFPage;
  6. using ComPDFKit.PDFPage.Edit;
  7. using ComPDFKit.Tool.DrawTool;
  8. using ComPDFKit.Tool.Help;
  9. using ComPDFKit.Tool.UndoManger;
  10. using ComPDFKit.Viewer.Annot;
  11. using ComPDFKit.Viewer.Helper;
  12. using ComPDFKitViewer;
  13. using ComPDFKitViewer.BaseObject;
  14. using ComPDFKitViewer.Helper;
  15. using ComPDFKitViewer.Widget;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Dynamic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Windows;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. namespace ComPDFKit.Tool
  26. {
  27. partial class CPDFViewerTool
  28. {
  29. public class CommandData
  30. {
  31. public ExecutedRoutedEventArgs uIEventArgs { get; set; }
  32. public bool StartCommand { get; set; }
  33. public CPDFEditType PDFEditType { get; set; }
  34. public AnnotParam CurrentParam { get; set; } = null;
  35. }
  36. /// <summary>
  37. /// System provided related command time notification
  38. /// </summary>
  39. public event EventHandler<CommandData> CommandExecutedHandler;
  40. public AnnotParam PasteParam { get; set; }
  41. internal class PDFEditCommandData
  42. {
  43. public string TextContent { get; set; }
  44. /// <summary>
  45. /// Original rectangle in PDF (72DPI)
  46. /// </summary>
  47. public Rect PDFRect { get; set; }
  48. public bool EditAreaCopied { get; set; }
  49. public CPDFEditType EditType { get; set; }
  50. public CPDFCopyEditArea CopyArea { get; set; }
  51. public CPDFEditPage EditPage { get; set; }
  52. }
  53. internal Point rightPressPoint = new Point(-1, -1);
  54. internal static List<PDFEditCommandData> lastPDFEditArgsList { get; private set; } = new List<PDFEditCommandData>();
  55. internal static List<AnnotParam> lastAnnotList { get; private set; } = new List<AnnotParam>();
  56. internal void BindCommand()
  57. {
  58. CommandBinding copyBind = new CommandBinding(ApplicationCommands.Copy);
  59. copyBind.CanExecute += CommandCanExecute;
  60. copyBind.Executed += CommandBind_Executed;
  61. CommandBindings.Add(copyBind);
  62. CommandBinding cutBind = new CommandBinding(ApplicationCommands.Cut);
  63. cutBind.CanExecute += CommandCanExecute;
  64. cutBind.Executed += CommandBind_Executed;
  65. CommandBindings.Add(cutBind);
  66. CommandBinding pasteBind = new CommandBinding(ApplicationCommands.Paste);
  67. pasteBind.CanExecute += CommandCanExecute;
  68. pasteBind.Executed += CommandBind_Executed;
  69. CommandBindings.Add(pasteBind);
  70. CommandBinding deleteBind = new CommandBinding(ApplicationCommands.Delete);
  71. deleteBind.CanExecute += CommandCanExecute;
  72. deleteBind.Executed += CommandBind_Executed;
  73. CommandBindings.Add(deleteBind);
  74. CommandBinding playBind = new CommandBinding(MediaCommands.Play);
  75. playBind.CanExecute += PlayBind_CanExecute;
  76. playBind.Executed += PlayBind_Executed;
  77. CommandBindings.Add(playBind);
  78. CommandBinding pastWithoutStyle = new CommandBinding(CustomCommands.PasteWithoutStyle);
  79. pastWithoutStyle.CanExecute += CommandCanExecute;
  80. pastWithoutStyle.Executed += CommandBind_Executed;
  81. CommandBindings.Add(pastWithoutStyle);
  82. CommandBinding selectAllBind = new CommandBinding(ApplicationCommands.SelectAll);
  83. selectAllBind.CanExecute += CommandCanExecute;
  84. selectAllBind.Executed += CommandBind_Executed;
  85. CommandBindings.Add(selectAllBind);
  86. }
  87. private void PlayBind_Executed(object sender, ExecutedRoutedEventArgs e)
  88. {
  89. BaseAnnot cmdArgs = e.Parameter as BaseAnnot;
  90. if (cmdArgs != null && cmdArgs.GetAnnotData() != null)
  91. {
  92. if (cmdArgs is SoundAnnot)
  93. {
  94. (cmdArgs as SoundAnnot).Play();
  95. }
  96. if (cmdArgs is MovieAnnot)
  97. {
  98. (cmdArgs as MovieAnnot).Play();
  99. }
  100. }
  101. e.Handled = true;
  102. }
  103. private void PlayBind_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  104. {
  105. e.CanExecute = false;
  106. BaseAnnot cmdArgs = e.Parameter as BaseAnnot;
  107. if (cmdArgs != null && cmdArgs.GetAnnotData() != null)
  108. {
  109. e.CanExecute = true;
  110. }
  111. e.Handled = true;
  112. e.ContinueRouting = false;
  113. }
  114. public void ReBindCommand(List<CommandBinding> commandBindings)
  115. {
  116. CommandBindings.Clear();
  117. foreach (CommandBinding binding in commandBindings)
  118. {
  119. CommandBindings.Add(binding);
  120. }
  121. }
  122. private bool IsCanDoCommand = true;
  123. public void CanDoCommand()
  124. {
  125. IsCanDoCommand = true;
  126. }
  127. public void NotDoCommand()
  128. {
  129. IsCanDoCommand = false;
  130. }
  131. private void CommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
  132. {
  133. if (e.Command is RoutedUICommand uiCommand)
  134. {
  135. switch (currentModel)
  136. {
  137. case ToolType.Viewer:
  138. CheckViewerCommandStatus(uiCommand, e);
  139. break;
  140. case ToolType.CreateAnnot:
  141. case ToolType.WidgetEdit:
  142. case ToolType.Pan:
  143. CheckAnnotCommandStatus(uiCommand, e);
  144. break;
  145. case ToolType.Customize:
  146. break;
  147. case ToolType.ContentEdit:
  148. {
  149. CheckPDFEditCommandStatus(uiCommand, e);
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. }
  157. private void CommandBind_Executed(object sender, ExecutedRoutedEventArgs e)
  158. {
  159. RoutedUICommand uiCommand = e.Command as RoutedUICommand;
  160. if (uiCommand != null)
  161. {
  162. try
  163. {
  164. PasteParam = null;
  165. CommandData commandData = new CommandData();
  166. commandData.uIEventArgs = e;
  167. commandData.StartCommand = true;
  168. CPDFEditType editType = CPDFEditType.None;
  169. CommandExecutedHandler?.Invoke(this, commandData);
  170. if (e.Handled)
  171. {
  172. return;
  173. }
  174. if (!IsCanDoCommand)
  175. {
  176. CanDoCommand();
  177. return;
  178. }
  179. switch (currentModel)
  180. {
  181. case ToolType.Viewer:
  182. ExecuteViewerCommand(uiCommand);
  183. break;
  184. case ToolType.CreateAnnot:
  185. case ToolType.WidgetEdit:
  186. case ToolType.Pan:
  187. ExecuteAnnotCommand(uiCommand);
  188. break;
  189. case ToolType.ContentEdit:
  190. {
  191. ExecutePDFEditCommand(uiCommand, out editType);
  192. }
  193. break;
  194. case ToolType.Customize:
  195. break;
  196. default:
  197. break;
  198. }
  199. commandData.StartCommand = false;
  200. commandData.PDFEditType = editType;
  201. commandData.CurrentParam = PasteParam;
  202. CommandExecutedHandler?.Invoke(this, commandData);
  203. }
  204. catch (Exception ex)
  205. {
  206. }
  207. }
  208. }
  209. public void SetPastePoint(Point point)
  210. {
  211. rightPressPoint = point;
  212. }
  213. #region ContentEdit
  214. private void CheckPDFEditCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  215. {
  216. switch (uiCommand.Name)
  217. {
  218. case "Copy":
  219. case "Cut":
  220. case "Delete":
  221. case "SelectAll":
  222. {
  223. if (currentEditAreaObject != null)
  224. {
  225. e.CanExecute = true;
  226. }
  227. //Add multi status
  228. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  229. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  230. {
  231. e.CanExecute = true;
  232. }
  233. break;
  234. }
  235. case "Paste":
  236. case "PasteWithoutStyle":
  237. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  238. {
  239. if (checkItem.EditType == CPDFEditType.EditText && string.IsNullOrEmpty(checkItem.TextContent) == false)
  240. {
  241. e.CanExecute = true;
  242. break;
  243. }
  244. if (checkItem.EditType == CPDFEditType.EditImage && checkItem.EditAreaCopied)
  245. {
  246. e.CanExecute = true;
  247. break;
  248. }
  249. if (checkItem.EditType == CPDFEditType.EditPath && checkItem.EditAreaCopied)
  250. {
  251. e.CanExecute = true;
  252. break;
  253. }
  254. }
  255. try
  256. {
  257. if (Clipboard.ContainsText())
  258. {
  259. if (!string.IsNullOrEmpty(Clipboard.GetText()))
  260. {
  261. e.CanExecute = true;
  262. }
  263. }
  264. if (Clipboard.ContainsImage())
  265. {
  266. if (Clipboard.GetImage() != null)
  267. {
  268. e.CanExecute = true;
  269. }
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. }
  275. break;
  276. }
  277. }
  278. private void ExecutePDFEditCommand(RoutedUICommand uiCommand, out CPDFEditType editType)
  279. {
  280. editType = CPDFEditType.None;
  281. switch (uiCommand.Name)
  282. {
  283. case "Copy":
  284. CopyPDFEditData(out editType);
  285. break;
  286. case "Cut":
  287. CopyPDFEditData(out editType);
  288. DelPDFEditData(out editType);
  289. break;
  290. case "Delete":
  291. DelPDFEditData(out editType);
  292. break;
  293. case "Paste":
  294. SetEditCopyData();
  295. if (lastPDFEditArgsList.Count > 0)
  296. {
  297. PastePDFEditData(out editType);
  298. PDFViewer.UpdateRenderFrame();
  299. }
  300. break;
  301. case "PasteWithoutStyle":
  302. SetEditCopyData();
  303. if (lastPDFEditArgsList.Count > 0)
  304. {
  305. PastePDFEditData(out editType, false);
  306. PDFViewer.UpdateRenderFrame();
  307. }
  308. break;
  309. case "SelectAll":
  310. SelectAllPDFEditData(out editType);
  311. break;
  312. }
  313. }
  314. private void SetEditCopyData()
  315. {
  316. try
  317. {
  318. if (Clipboard.ContainsText())
  319. {
  320. string copyText = Clipboard.GetText();
  321. bool findCopy = false;
  322. if (lastPDFEditArgsList != null && lastPDFEditArgsList.Count > 0)
  323. {
  324. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  325. {
  326. if (checkItem.EditType == CPDFEditType.EditText && copyText == checkItem.TextContent)
  327. {
  328. findCopy = true;
  329. }
  330. }
  331. }
  332. if (findCopy == false)
  333. {
  334. lastPDFEditArgsList?.Clear();
  335. if (string.IsNullOrEmpty(copyText) == false)
  336. {
  337. PDFEditCommandData commandData = new PDFEditCommandData();
  338. commandData.EditType = CPDFEditType.EditText;
  339. commandData.TextContent = copyText;
  340. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  341. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  342. Rect rect = render.PaintRect;
  343. Point centerPoint = new Point(
  344. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  345. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  346. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, 0, 0));
  347. lastPDFEditArgsList.Add(commandData);
  348. }
  349. }
  350. }
  351. else if (Clipboard.ContainsImage())
  352. {
  353. if (Clipboard.GetImage() != null)
  354. {
  355. BitmapSource bitmapSource = Clipboard.GetImage();
  356. PDFEditCommandData commandData = new PDFEditCommandData();
  357. commandData.EditType = CPDFEditType.EditImage;
  358. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  359. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  360. Rect rect = render.PaintRect;
  361. Point centerPoint = new Point(
  362. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  363. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  364. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, bitmapSource.PixelWidth, bitmapSource.PixelHeight));
  365. lastPDFEditArgsList.Clear();
  366. lastPDFEditArgsList.Add(commandData);
  367. }
  368. }
  369. }
  370. catch (Exception ex)
  371. {
  372. }
  373. }
  374. private void CopyPDFEditData(out CPDFEditType editType)
  375. {
  376. editType = CPDFEditType.None;
  377. if (CPDFEditPage.CopyPage != null)
  378. {
  379. CPDFEditPage.CopyPage.ReleaseCopyEditAreaList();
  380. }
  381. try
  382. {
  383. lastPDFEditArgsList.Clear();
  384. Clipboard.Clear();
  385. if (currentEditAreaObject != null)
  386. {
  387. PDFEditCommandData commandData = new PDFEditCommandData();
  388. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  389. {
  390. editType = CPDFEditType.EditText;
  391. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  392. commandData.TextContent = editTextArea.SelectText;
  393. if (selectAllCharsForLine || string.IsNullOrEmpty(commandData.TextContent))
  394. {
  395. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  396. commandData.EditType = CPDFEditType.EditText;
  397. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  398. if (commandData.EditAreaCopied)
  399. {
  400. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  401. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  402. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  403. commandData.CopyArea = CopyArea;
  404. commandData.EditPage = editPage;
  405. }
  406. }
  407. try
  408. {
  409. Clipboard.Clear();
  410. Clipboard.SetText(commandData.TextContent);
  411. Clipboard.Flush();
  412. }
  413. catch (Exception)
  414. {
  415. }
  416. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  417. lastPDFEditArgsList.Add(commandData);
  418. }
  419. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  420. {
  421. editType = CPDFEditType.EditImage;
  422. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  423. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  424. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  425. commandData.EditType = CPDFEditType.EditImage;
  426. if (commandData.EditAreaCopied)
  427. {
  428. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  429. commandData.CopyArea = copyList[copyList.Count - 1];
  430. commandData.EditPage = editPage;
  431. lastPDFEditArgsList.Add(commandData);
  432. }
  433. }
  434. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  435. {
  436. editType = CPDFEditType.EditPath;
  437. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  438. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  439. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  440. commandData.EditType = CPDFEditType.EditPath;
  441. if (commandData.EditAreaCopied)
  442. {
  443. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  444. commandData.CopyArea = copyList[copyList.Count - 1];
  445. commandData.EditPage = editPage;
  446. lastPDFEditArgsList.Add(commandData);
  447. }
  448. }
  449. }
  450. else
  451. {
  452. //Add multi status
  453. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  454. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  455. {
  456. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  457. {
  458. EditAreaObject editAreaObject = GetEditAreaObjectListForRect(selectedRect);
  459. if (editAreaObject != null)
  460. {
  461. PDFEditCommandData commandData = new PDFEditCommandData();
  462. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  463. {
  464. editType = CPDFEditType.EditText;
  465. CPDFEditTextArea editTextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  466. commandData.TextContent = editTextArea.SelectText;
  467. if (!string.IsNullOrEmpty(commandData.TextContent))
  468. {
  469. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  470. commandData.EditType = CPDFEditType.EditText;
  471. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  472. if (commandData.EditAreaCopied)
  473. {
  474. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  475. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  476. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  477. commandData.CopyArea = CopyArea;
  478. commandData.EditPage = editPage;
  479. }
  480. }
  481. try
  482. {
  483. Clipboard.Clear();
  484. Clipboard.SetText(commandData.TextContent);
  485. Clipboard.Flush();
  486. }
  487. catch (Exception)
  488. {
  489. }
  490. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  491. lastPDFEditArgsList.Add(commandData);
  492. }
  493. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  494. {
  495. editType = CPDFEditType.EditImage;
  496. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  497. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  498. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  499. commandData.EditType = CPDFEditType.EditImage;
  500. if (commandData.EditAreaCopied)
  501. {
  502. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  503. commandData.CopyArea = copyList[copyList.Count - 1];
  504. commandData.EditPage = editPage;
  505. lastPDFEditArgsList.Add(commandData);
  506. }
  507. }
  508. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  509. {
  510. editType = CPDFEditType.EditPath;
  511. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  512. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  513. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  514. commandData.EditType = CPDFEditType.EditPath;
  515. if (commandData.EditAreaCopied)
  516. {
  517. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  518. commandData.CopyArea = copyList[copyList.Count - 1];
  519. commandData.EditPage = editPage;
  520. lastPDFEditArgsList.Add(commandData);
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. catch
  529. {
  530. }
  531. }
  532. private void DelPDFEditData(out CPDFEditType editType)
  533. {
  534. editType = CPDFEditType.None;
  535. if (currentEditAreaObject != null)
  536. {
  537. GroupHistory groupHistory = new GroupHistory();
  538. PDFEditHistory editHistory = new PDFEditHistory();
  539. editHistory.EditPage = currentEditAreaObject.cPDFEditPage;
  540. editHistory.PageIndex = currentEditAreaObject.PageIndex;
  541. groupHistory.Histories.Add(editHistory);
  542. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  543. {
  544. editType = CPDFEditType.EditText;
  545. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  546. string selectContent = editTextArea.SelectText;
  547. if (!selectAllCharsForLine)
  548. {
  549. DeleteChars();
  550. }
  551. else
  552. {
  553. RemoveTextBlock();
  554. }
  555. }
  556. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  557. {
  558. editType = CPDFEditType.EditImage;
  559. RemoveImageBlock();
  560. }
  561. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  562. {
  563. editType = CPDFEditType.EditPath;
  564. RemovePathBlock();
  565. }
  566. if (PDFViewer != null && PDFViewer.UndoManager != null)
  567. {
  568. PDFViewer.UndoManager.AddHistory(groupHistory);
  569. //After removing the data, you need to get the data again.
  570. PDFViewer.UpdateRenderFrame();
  571. }
  572. }
  573. else
  574. {
  575. //Add multi status
  576. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  577. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  578. {
  579. List<EditAreaObject> editAreaObjectList = new List<EditAreaObject>();
  580. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  581. {
  582. EditAreaObject editAreaObject = GetEditAreaObjectListForRect(selectedRect);
  583. editAreaObjectList.Add(editAreaObject);
  584. }
  585. if (editAreaObjectList != null && editAreaObjectList.Count > 0)
  586. {
  587. editAreaObjectList = editAreaObjectList.OrderByDescending(x => x.EditAreaIndex).ToList();
  588. GroupHistory groupHistory = new GroupHistory();
  589. foreach (EditAreaObject editareaobject in editAreaObjectList)
  590. {
  591. PDFEditHistory editHistory = new PDFEditHistory();
  592. editHistory.EditPage = editareaobject.cPDFEditPage;
  593. editHistory.PageIndex = editareaobject.PageIndex;
  594. groupHistory.Histories.Add(editHistory);
  595. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditText)
  596. {
  597. editType = CPDFEditType.EditText;
  598. CPDFEditTextArea editTextArea = editareaobject.cPDFEditArea as CPDFEditTextArea;
  599. string selectContent = editTextArea.SelectText;
  600. RemoveTextBlock(editareaobject);
  601. }
  602. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditImage)
  603. {
  604. editType = CPDFEditType.EditImage;
  605. RemoveImageBlock(editareaobject);
  606. }
  607. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditPath)
  608. {
  609. editType = CPDFEditType.EditPath;
  610. RemovePathBlock(editareaobject);
  611. }
  612. }
  613. if (PDFViewer != null && PDFViewer.UndoManager != null)
  614. {
  615. PDFViewer.UndoManager.AddHistory(groupHistory);
  616. //After removing the data, you need to get the data again.
  617. PDFViewer.UpdateRenderFrame();
  618. }
  619. }
  620. CleanSelectedMultiRect();
  621. OpenSelectedMulti(false);
  622. }
  623. }
  624. }
  625. private void PastePDFEditData(out CPDFEditType editType, bool pasteMatchStyle = true)
  626. {
  627. editType = CPDFEditType.None;
  628. double left = 0;
  629. double right = 0;
  630. double top = 0;
  631. double bottom = 0;
  632. bool initial = false;
  633. #region Calculate the maximum rectangle
  634. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  635. {
  636. if (initial == false)
  637. {
  638. left = commandData.PDFRect.Left;
  639. right = commandData.PDFRect.Right;
  640. top = commandData.PDFRect.Top;
  641. bottom = commandData.PDFRect.Bottom;
  642. initial = true;
  643. continue;
  644. }
  645. left = Math.Min(left, commandData.PDFRect.Left);
  646. right = Math.Max(right, commandData.PDFRect.Right);
  647. top = Math.Min(top, commandData.PDFRect.Top);
  648. bottom = Math.Max(bottom, commandData.PDFRect.Bottom);
  649. }
  650. left = DpiHelper.PDFNumToStandardNum(left);
  651. right = DpiHelper.PDFNumToStandardNum(right);
  652. top = DpiHelper.PDFNumToStandardNum(top);
  653. bottom = DpiHelper.PDFNumToStandardNum(bottom);
  654. int offsetX = 25;
  655. int offsetY = 25;
  656. Point hoverPoint = rightPressPoint;
  657. rightPressPoint = new Point(-1, -1);
  658. int pageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  659. // Judge whether the right-click coordinate is on the page.
  660. PDFViewer.GetePointToPage(hoverPoint, out RenderData renderData, out Point pagePoint);
  661. if (renderData != null)
  662. {
  663. //Calculate the offset from the upper left corner.
  664. offsetX = (int)(pagePoint.X / currentZoom - left);
  665. offsetY = (int)(pagePoint.Y / currentZoom - top);
  666. if (left + offsetX < 0)
  667. {
  668. offsetX = (int)-left;
  669. }
  670. if (right + offsetX > renderData.PageBound.Width / currentZoom)
  671. {
  672. offsetX = (int)(renderData.PageBound.Width / currentZoom - right);
  673. }
  674. if (top + offsetY < 0)
  675. {
  676. offsetY = (int)-top;
  677. }
  678. if (bottom + offsetY > renderData.PageBound.Height / currentZoom)
  679. {
  680. offsetY = (int)(renderData.PageBound.Height / currentZoom - bottom);
  681. }
  682. pageIndex = renderData.PageIndex;
  683. }
  684. else
  685. {
  686. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  687. //Max rectangle (standard DPI).
  688. Rect maxRect = new Rect((int)left + render.PaintRect.Left / currentZoom, (int)top + render.PaintRect.Top / currentZoom, (int)(right - left), (int)(bottom - top));
  689. //Center point of the visible range.
  690. Point centerPoint = new Point(
  691. render.PaintRect.Left / currentZoom + render.PaintRect.Width / currentZoom / 2,
  692. render.PaintRect.Top / currentZoom + render.PaintRect.Height / currentZoom / 2);
  693. //Visible range.
  694. Rect checkRect = new Rect(
  695. render.PaintRect.Left / currentZoom,
  696. render.PaintRect.Top / currentZoom,
  697. render.PaintRect.Width / currentZoom,
  698. render.PaintRect.Height / currentZoom);
  699. if (!checkRect.IntersectsWith(maxRect))
  700. {
  701. offsetX = (int)(left - centerPoint.X);
  702. offsetY = (int)(top - centerPoint.Y);
  703. }
  704. if (left + offsetX < 0)
  705. {
  706. offsetX = (int)-left;
  707. }
  708. if (right + offsetX > render.RenderRect.Width / currentZoom)
  709. {
  710. offsetX = (int)(render.RenderRect.Width / currentZoom / 2 - right);
  711. }
  712. if (top + offsetY < 0)
  713. {
  714. offsetY = (int)-top;
  715. }
  716. if (bottom + offsetY > render.RenderRect.Height / currentZoom)
  717. {
  718. offsetY = (int)(render.RenderRect.Height / currentZoom / 2 - bottom);
  719. }
  720. }
  721. #endregion
  722. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  723. {
  724. GroupHistory groupHistory = new GroupHistory();
  725. CPDFPage docPage = PDFViewer.GetDocument().PageAtIndex(pageIndex);
  726. CPDFEditPage editPage = docPage.GetEditPage();
  727. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  728. Rect offsetRect = AddPasteOffset(commandData.PDFRect, (int)DpiHelper.StandardNumToPDFNum(offsetX), (int)DpiHelper.StandardNumToPDFNum(offsetY), new Size(DpiHelper.StandardNumToPDFNum(render.PageBound.Width / currentZoom), DpiHelper.StandardNumToPDFNum(render.PageBound.Height / currentZoom)));
  729. commandData.PDFRect = offsetRect;
  730. if (commandData.EditType == CPDFEditType.EditText)
  731. {
  732. editType = CPDFEditType.EditText;
  733. if (pasteMatchStyle && commandData.EditAreaCopied)
  734. {
  735. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  736. if (editPage.CanUndo())
  737. {
  738. PDFEditHistory editHistory = new PDFEditHistory();
  739. editHistory.EditPage = editPage;
  740. editHistory.PageIndex = pageIndex;
  741. groupHistory.Histories.Add(editHistory);
  742. CPDFViewer pdfViewer = GetCPDFViewer();
  743. if (pdfViewer != null && pdfViewer.UndoManager != null)
  744. {
  745. pdfViewer.UndoManager.AddHistory(groupHistory);
  746. }
  747. }
  748. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  749. }
  750. else
  751. {
  752. PDFEditHistory editHistory = new PDFEditHistory();
  753. if (!string.IsNullOrEmpty(commandData.TextContent))
  754. {
  755. EditAreaObject editAreaObject = GetHitTestAreaObject(hoverPoint);
  756. if (editAreaObject != null && editAreaObject.cPDFEditArea is CPDFEditTextArea)
  757. {
  758. CPDFEditTextArea TextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  759. if (TextArea.SelectLineRects.Count > 0)
  760. {
  761. TextArea.DeleteChars();
  762. TextArea.ClearSelectChars();
  763. PDFEditHistory deleteHistory = new PDFEditHistory();
  764. deleteHistory.EditPage = editPage;
  765. deleteHistory.PageIndex = pageIndex;
  766. groupHistory.Histories.Add(deleteHistory);
  767. }
  768. TextArea.InsertText(commandData.TextContent);
  769. editHistory.EditPage = editPage;
  770. editHistory.PageIndex = pageIndex;
  771. groupHistory.Histories.Add(editHistory);
  772. CPDFViewer pdfViewer = GetCPDFViewer();
  773. if (pdfViewer != null && pdfViewer.UndoManager != null)
  774. {
  775. pdfViewer.UndoManager.AddHistory(groupHistory);
  776. }
  777. }
  778. else
  779. {
  780. PDFEditHistory createHistory = new PDFEditHistory();
  781. CPDFEditTextArea editArea = editPage.CreateNewTextArea(DataConversionForWPF.RectConversionForCRect(offsetRect), "Helvetica", 14, new byte[3] { 0, 0, 0 });
  782. createHistory.EditPage = editPage;
  783. createHistory.PageIndex = pageIndex;
  784. groupHistory.Histories.Add(createHistory);
  785. if (editArea != null)
  786. {
  787. if (editArea.InsertText(commandData.TextContent))
  788. {
  789. editHistory.EditPage = editPage;
  790. editHistory.PageIndex = pageIndex;
  791. groupHistory.Histories.Add(editHistory);
  792. }
  793. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  794. }
  795. CPDFViewer pdfViewer = GetCPDFViewer();
  796. if (pdfViewer != null && pdfViewer.UndoManager != null)
  797. {
  798. pdfViewer.UndoManager.AddHistory(groupHistory);
  799. }
  800. }
  801. }
  802. }
  803. }
  804. if (commandData.EditType == CPDFEditType.EditImage)
  805. {
  806. editType = CPDFEditType.EditImage;
  807. PDFEditHistory editHistory = new PDFEditHistory();
  808. if (commandData.EditAreaCopied)
  809. {
  810. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  811. editHistory.EditPage = editPage;
  812. editHistory.PageIndex = pageIndex;
  813. CPDFViewer pdfViewer = GetCPDFViewer();
  814. if (pdfViewer != null && pdfViewer.UndoManager != null)
  815. {
  816. pdfViewer.UndoManager.AddHistory(editHistory);
  817. }
  818. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  819. }
  820. else
  821. {
  822. BitmapSource bitmapSource = BinaryStructConverter.ImageFromClipboardDib();
  823. if (bitmapSource != null)
  824. {
  825. PDFEditHistory createHistory = new PDFEditHistory();
  826. byte[] imageData = new byte[bitmapSource.PixelWidth * bitmapSource.PixelHeight * 4];
  827. if (bitmapSource.Format != PixelFormats.Bgra32)
  828. {
  829. FormatConvertedBitmap covert = new FormatConvertedBitmap(bitmapSource, PixelFormats.Bgra32, bitmapSource.Palette, 0);
  830. covert.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  831. }
  832. else
  833. {
  834. bitmapSource.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  835. }
  836. CPDFEditImageArea editArea = editPage.CreateNewImageArea(DataConversionForWPF.RectConversionForCRect(offsetRect), imageData, bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  837. createHistory.EditPage = editPage;
  838. createHistory.PageIndex = pageIndex;
  839. groupHistory.Histories.Add(createHistory);
  840. if (editArea != null)
  841. {
  842. editHistory.EditPage = editPage;
  843. editHistory.PageIndex = pageIndex;
  844. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  845. }
  846. CPDFViewer pdfViewer = GetCPDFViewer();
  847. if (pdfViewer != null && pdfViewer.UndoManager != null)
  848. {
  849. pdfViewer.UndoManager.AddHistory(groupHistory);
  850. }
  851. }
  852. }
  853. }
  854. if (commandData.EditType == CPDFEditType.EditPath)
  855. {
  856. editType = CPDFEditType.EditPath;
  857. PDFEditHistory editHistory = new PDFEditHistory();
  858. if (commandData.EditAreaCopied)
  859. {
  860. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  861. editHistory.EditPage = editPage;
  862. editHistory.PageIndex = pageIndex;
  863. CPDFViewer pdfViewer = GetCPDFViewer();
  864. if (pdfViewer != null && pdfViewer.UndoManager != null)
  865. {
  866. pdfViewer.UndoManager.AddHistory(editHistory);
  867. }
  868. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  869. }
  870. }
  871. editPage.EndEdit();
  872. }
  873. }
  874. private void SelectAllPDFEditData(out CPDFEditType editType)
  875. {
  876. editType = CPDFEditType.None;
  877. if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
  878. {
  879. CPDFEditTextArea textArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  880. textArea.SelectAllChars();
  881. editType = CPDFEditType.EditText;
  882. PDFViewer.UpdateRenderFrame();
  883. }
  884. }
  885. private Rect AddPasteOffset(Rect clientRect, int offsetX, int offsetY, Size pageSize)
  886. {
  887. clientRect.X += offsetX;
  888. clientRect.Y += offsetY;
  889. if (clientRect.Left < 0)
  890. {
  891. clientRect.X = 0;
  892. }
  893. if (clientRect.Top < 0)
  894. {
  895. clientRect.Y = 0;
  896. }
  897. if (clientRect.Right > pageSize.Width)
  898. {
  899. clientRect.X = pageSize.Width - Math.Min(clientRect.Width, pageSize.Width);
  900. }
  901. if (clientRect.Bottom > pageSize.Height)
  902. {
  903. clientRect.Y = pageSize.Height - Math.Min(clientRect.Height, pageSize.Height);
  904. }
  905. return clientRect;
  906. }
  907. #endregion
  908. #region Annot
  909. private bool CheckCacheHitTestAnnot(string cmdName = "")
  910. {
  911. if (cacheHitTestAnnot == null)
  912. {
  913. return false;
  914. }
  915. AnnotData hitData = cacheHitTestAnnot.GetAnnotData();
  916. if (hitData == null)
  917. {
  918. return false;
  919. }
  920. if (hitData.Annot == null)
  921. {
  922. return false;
  923. }
  924. if (cmdName == "Delete" && hitData.Annot is CPDFSignatureWidget)
  925. {
  926. CPDFSignatureWidget signAnnot = hitData.Annot as CPDFSignatureWidget;
  927. if (signAnnot != null && signAnnot.IsSigned())
  928. {
  929. return false;
  930. }
  931. }
  932. return true;
  933. }
  934. private bool CheckAnnotCanDoCopyCut(bool isCut = false)
  935. {
  936. if (!CheckCacheHitTestAnnot(isCut ? "Delete" : ""))
  937. {
  938. return false;
  939. }
  940. if (cacheHitTestAnnot.GetAnnotData().Annot.GetIsLocked() && isCut)
  941. {
  942. return false;
  943. }
  944. switch (cacheHitTestAnnot.CurrentType)
  945. {
  946. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  947. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  948. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  949. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  950. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  951. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  952. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  953. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  954. return false;
  955. default:
  956. break;
  957. }
  958. return true;
  959. }
  960. private void CheckViewerCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  961. {
  962. switch (uiCommand.Name)
  963. {
  964. case "Copy":
  965. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  966. if (!e.CanExecute && textSelectInfo != null)
  967. {
  968. foreach (int key in textSelectInfo.PageSelectText.Keys)
  969. {
  970. if (textSelectInfo.PageSelectText[key] != string.Empty)
  971. {
  972. e.CanExecute = true;
  973. break;
  974. }
  975. }
  976. }
  977. break;
  978. default:
  979. return;
  980. }
  981. }
  982. private void CheckAnnotCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  983. {
  984. switch (uiCommand.Name)
  985. {
  986. case "Copy":
  987. e.CanExecute = CheckAnnotCanDoCopyCut();
  988. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  989. if (!e.CanExecute && textSelectInfo != null)
  990. {
  991. foreach (int key in textSelectInfo.PageSelectText.Keys)
  992. {
  993. if (textSelectInfo.PageSelectText[key] != string.Empty)
  994. {
  995. e.CanExecute = true;
  996. break;
  997. }
  998. }
  999. }
  1000. break;
  1001. case "Cut":
  1002. e.CanExecute = CheckAnnotCanDoCopyCut(true);
  1003. break;
  1004. case "Delete":
  1005. e.CanExecute = CheckCacheHitTestAnnot("Delete");
  1006. break;
  1007. case "Paste":
  1008. if (lastAnnotList.Count > 0)
  1009. {
  1010. e.CanExecute = true;
  1011. }
  1012. break;
  1013. }
  1014. }
  1015. private void ExecuteViewerCommand(RoutedUICommand uiCommand)
  1016. {
  1017. switch (uiCommand.Name)
  1018. {
  1019. case "Copy":
  1020. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1021. if (textSelectInfo != null)
  1022. {
  1023. StringBuilder copyTextBuilder = new StringBuilder();
  1024. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1025. {
  1026. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1027. {
  1028. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  1029. }
  1030. }
  1031. if (copyTextBuilder.Length > 0)
  1032. {
  1033. try
  1034. {
  1035. Clipboard.Clear();
  1036. Clipboard.SetText(copyTextBuilder.ToString());
  1037. }
  1038. catch (Exception)
  1039. {
  1040. }
  1041. }
  1042. }
  1043. break;
  1044. default:
  1045. return;
  1046. }
  1047. }
  1048. private void ExecuteAnnotCommand(RoutedUICommand uiCommand)
  1049. {
  1050. switch (uiCommand.Name)
  1051. {
  1052. case "Copy":
  1053. if (cacheHitTestAnnot != null)
  1054. {
  1055. lastAnnotList.Clear();
  1056. AnnotParam annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1057. lastAnnotList.Add(annotParam);
  1058. }
  1059. if (cacheMoveWidget != null)
  1060. {
  1061. lastAnnotList.Clear();
  1062. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1063. lastAnnotList.Add(annotParam);
  1064. }
  1065. else
  1066. {
  1067. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1068. if (textSelectInfo != null)
  1069. {
  1070. StringBuilder copyTextBuilder = new StringBuilder();
  1071. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1072. {
  1073. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1074. {
  1075. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  1076. }
  1077. }
  1078. if (copyTextBuilder.Length > 0)
  1079. {
  1080. try
  1081. {
  1082. Clipboard.Clear();
  1083. Clipboard.SetText(copyTextBuilder.ToString());
  1084. }
  1085. catch (Exception)
  1086. {
  1087. }
  1088. }
  1089. }
  1090. }
  1091. break;
  1092. case "Cut":
  1093. if (cacheHitTestAnnot != null)
  1094. {
  1095. lastAnnotList.Clear();
  1096. AnnotParam annotParam;
  1097. if (cacheHitTestAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  1098. {
  1099. annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1100. }
  1101. else
  1102. {
  1103. annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1104. }
  1105. lastAnnotList.Add(annotParam);
  1106. }
  1107. if (cacheMoveWidget != null)
  1108. {
  1109. lastAnnotList.Clear();
  1110. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1111. lastAnnotList.Add(annotParam);
  1112. }
  1113. DeleteAnnotData();
  1114. PDFViewer.UpdateAnnotFrame();
  1115. break;
  1116. case "Delete":
  1117. DeleteAnnotData();
  1118. PDFViewer.UpdateAnnotFrame();
  1119. break;
  1120. case "Paste":
  1121. PasteAnnotData();
  1122. PDFViewer.UpdateAnnotFrame();
  1123. break;
  1124. }
  1125. }
  1126. private void PasteAnnotData(bool pasteMatchStyle = false)
  1127. {
  1128. AnnotHistory annotHistory = null;
  1129. foreach (AnnotParam item in lastAnnotList)
  1130. {
  1131. if (item == null)
  1132. {
  1133. continue;
  1134. }
  1135. Point point = rightPressPoint;
  1136. rightPressPoint = new Point(-1, -1);
  1137. switch (item.CurrentType)
  1138. {
  1139. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  1140. {
  1141. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1142. Rect rect;
  1143. annotHistory = new StickyNoteAnnotHistory();
  1144. annotHistory.PDFDoc = cPDFDocument;
  1145. int index = -1;
  1146. if (point.Equals(new Point(-1, -1)))
  1147. {
  1148. index = item.PageIndex;
  1149. rect = new Rect(
  1150. (item.ClientRect.left + 25),
  1151. (item.ClientRect.top + 25),
  1152. item.ClientRect.width(),
  1153. item.ClientRect.height()
  1154. );
  1155. }
  1156. else
  1157. {
  1158. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1159. CRect cRect = item.ClientRect;
  1160. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1161. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1162. rect = new Rect(
  1163. (pdfPoint.X - cRect.width() / 2),
  1164. (pdfPoint.Y - cRect.height() / 2),
  1165. cRect.width(),
  1166. cRect.height()
  1167. );
  1168. }
  1169. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1170. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1171. if (cPDFPage == null)
  1172. return;
  1173. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1174. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1175. cPDFAnnotation.SetRect(setRect);
  1176. CommonHelper.UpdateStickyAP(cPDFAnnotation as CPDFTextAnnotation);
  1177. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1178. (annotHistory as StickyNoteAnnotHistory).CurrentParam = (StickyNoteParam)annotParam;
  1179. }
  1180. break;
  1181. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1182. {
  1183. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1184. Rect rect;
  1185. annotHistory = new LinkAnnotHistory();
  1186. annotHistory.PDFDoc = cPDFDocument;
  1187. int index = -1;
  1188. if (point.Equals(new Point(-1, -1)))
  1189. {
  1190. index = item.PageIndex;
  1191. rect = new Rect(
  1192. (item.ClientRect.left + 25),
  1193. (item.ClientRect.top + 25),
  1194. item.ClientRect.width(),
  1195. item.ClientRect.height()
  1196. );
  1197. }
  1198. else
  1199. {
  1200. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1201. CRect cRect = item.ClientRect;
  1202. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1203. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1204. rect = new Rect(
  1205. (pdfPoint.X - cRect.width() / 2),
  1206. (pdfPoint.Y - cRect.height() / 2),
  1207. cRect.width(),
  1208. cRect.height()
  1209. );
  1210. }
  1211. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1212. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1213. if (cPDFPage == null)
  1214. return;
  1215. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1216. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1217. cPDFAnnotation.SetRect(setRect);
  1218. cPDFAnnotation.UpdateAp();
  1219. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1220. (annotHistory as LinkAnnotHistory).CurrentParam = (LinkParam)annotParam;
  1221. }
  1222. break;
  1223. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1224. {
  1225. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1226. Rect rect;
  1227. annotHistory = new FreeTextAnnotHistory();
  1228. annotHistory.PDFDoc = cPDFDocument;
  1229. int index = -1;
  1230. if (point.Equals(new Point(-1, -1)))
  1231. {
  1232. index = item.PageIndex;
  1233. rect = new Rect(
  1234. (item.ClientRect.left + 25),
  1235. (item.ClientRect.top + 25),
  1236. item.ClientRect.width(),
  1237. item.ClientRect.height()
  1238. );
  1239. }
  1240. else
  1241. {
  1242. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1243. CRect cRect = item.ClientRect;
  1244. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1245. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1246. rect = new Rect(
  1247. (pdfPoint.X - cRect.width() / 2),
  1248. (pdfPoint.Y - cRect.height() / 2),
  1249. cRect.width(),
  1250. cRect.height()
  1251. );
  1252. }
  1253. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1254. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1255. if (cPDFPage == null)
  1256. return;
  1257. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1258. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1259. cPDFAnnotation.SetRect(setRect);
  1260. cPDFAnnotation.UpdateAp();
  1261. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1262. (annotHistory as FreeTextAnnotHistory).CurrentParam = (FreeTextParam)annotParam;
  1263. }
  1264. break;
  1265. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1266. {
  1267. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1268. Rect rect;
  1269. annotHistory = new LineAnnotHistory();
  1270. annotHistory.PDFDoc = cPDFDocument;
  1271. int index = -1;
  1272. if (point.Equals(new Point(-1, -1)))
  1273. {
  1274. index = item.PageIndex;
  1275. rect = new Rect(
  1276. (item.ClientRect.left + 25),
  1277. (item.ClientRect.top + 25),
  1278. item.ClientRect.width(),
  1279. item.ClientRect.height()
  1280. );
  1281. }
  1282. else
  1283. {
  1284. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1285. CRect cRect = item.ClientRect;
  1286. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1287. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1288. rect = new Rect(
  1289. (pdfPoint.X - cRect.width() / 2),
  1290. (pdfPoint.Y - cRect.height() / 2),
  1291. cRect.width(),
  1292. cRect.height()
  1293. );
  1294. }
  1295. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1296. LineParam newLineParam = new LineParam();
  1297. item.CopyTo(newLineParam);
  1298. {
  1299. float offsetX = setRect.left - item.ClientRect.left;
  1300. float offsetY = setRect.top - item.ClientRect.top;
  1301. newLineParam.HeadPoint = new CPoint(newLineParam.HeadPoint.x + offsetX, newLineParam.HeadPoint.y + offsetY);
  1302. newLineParam.TailPoint = new CPoint(newLineParam.TailPoint.x + offsetX, newLineParam.TailPoint.y + offsetY);
  1303. }
  1304. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1305. if (cPDFPage == null)
  1306. return;
  1307. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1308. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1309. (cPDFAnnotation as CPDFLineAnnotation)?.SetLinePoints(newLineParam.HeadPoint, newLineParam.TailPoint);
  1310. cPDFAnnotation.SetRect(setRect);
  1311. cPDFAnnotation.UpdateAp();
  1312. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1313. (annotHistory as LineAnnotHistory).CurrentParam = (LineParam)annotParam;
  1314. }
  1315. break;
  1316. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  1317. {
  1318. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1319. Rect rect;
  1320. annotHistory = new SquareAnnotHistory();
  1321. annotHistory.PDFDoc = cPDFDocument;
  1322. int index = -1;
  1323. if (point.Equals(new Point(-1, -1)))
  1324. {
  1325. index = item.PageIndex;
  1326. rect = new Rect(
  1327. (item.ClientRect.left + 25),
  1328. (item.ClientRect.top + 25),
  1329. item.ClientRect.width(),
  1330. item.ClientRect.height()
  1331. );
  1332. }
  1333. else
  1334. {
  1335. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1336. CRect cRect = item.ClientRect;
  1337. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1338. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1339. rect = new Rect(
  1340. (pdfPoint.X - cRect.width() / 2),
  1341. (pdfPoint.Y - cRect.height() / 2),
  1342. cRect.width(),
  1343. cRect.height()
  1344. );
  1345. }
  1346. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1347. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1348. if (cPDFPage == null)
  1349. return;
  1350. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1351. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1352. cPDFAnnotation.SetRect(setRect);
  1353. cPDFAnnotation.UpdateAp();
  1354. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1355. (annotHistory as SquareAnnotHistory).CurrentParam = (SquareParam)annotParam;
  1356. }
  1357. break;
  1358. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1359. {
  1360. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1361. Rect rect;
  1362. annotHistory = new CircleAnnotHistory();
  1363. annotHistory.PDFDoc = cPDFDocument;
  1364. int index = -1;
  1365. if (point.Equals(new Point(-1, -1)))
  1366. {
  1367. index = item.PageIndex;
  1368. rect = new Rect(
  1369. (item.ClientRect.left + 25),
  1370. (item.ClientRect.top + 25),
  1371. item.ClientRect.width(),
  1372. item.ClientRect.height()
  1373. );
  1374. }
  1375. else
  1376. {
  1377. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1378. CRect cRect = item.ClientRect;
  1379. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1380. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1381. rect = new Rect(
  1382. (pdfPoint.X - cRect.width() / 2),
  1383. (pdfPoint.Y - cRect.height() / 2),
  1384. cRect.width(),
  1385. cRect.height()
  1386. );
  1387. }
  1388. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1389. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1390. if (cPDFPage == null)
  1391. return;
  1392. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1393. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1394. cPDFAnnotation.SetRect(setRect);
  1395. cPDFAnnotation.UpdateAp();
  1396. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1397. (annotHistory as CircleAnnotHistory).CurrentParam = (CircleParam)annotParam;
  1398. }
  1399. break;
  1400. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1401. break;
  1402. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  1403. break;
  1404. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1405. {
  1406. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1407. Rect rect;
  1408. annotHistory = new StampAnnotHistory();
  1409. annotHistory.PDFDoc = cPDFDocument;
  1410. int index = -1;
  1411. if (point.Equals(new Point(-1, -1)))
  1412. {
  1413. index = item.PageIndex;
  1414. rect = new Rect(
  1415. (item.ClientRect.left + 25),
  1416. (item.ClientRect.top + 25),
  1417. item.ClientRect.width(),
  1418. item.ClientRect.height()
  1419. );
  1420. }
  1421. else
  1422. {
  1423. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1424. CRect cRect = (item as StampParam).SourceRect;
  1425. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1426. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1427. rect = new Rect(
  1428. (pdfPoint.X - cRect.width() / 2),
  1429. (pdfPoint.Y - cRect.height() / 2),
  1430. cRect.width(),
  1431. cRect.height()
  1432. );
  1433. }
  1434. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1435. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1436. if (cPDFPage == null)
  1437. return;
  1438. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1439. (cPDFAnnotation as CPDFStampAnnotation).SetSourceRect(setRect);
  1440. //cPDFAnnotation.SetRect(setRect);
  1441. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1442. cPDFAnnotation.UpdateAp();
  1443. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1444. (annotHistory as StampAnnotHistory).CurrentParam = (StampParam)annotParam;
  1445. }
  1446. break;
  1447. case C_ANNOTATION_TYPE.C_ANNOTATION_CARET:
  1448. break;
  1449. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1450. {
  1451. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1452. Rect rect;
  1453. annotHistory = new InkAnnotHistory();
  1454. annotHistory.PDFDoc = cPDFDocument;
  1455. int index = -1;
  1456. if (point.Equals(new Point(-1, -1)))
  1457. {
  1458. index = item.PageIndex;
  1459. rect = new Rect(
  1460. (item.ClientRect.left + 25),
  1461. (item.ClientRect.top + 25),
  1462. item.ClientRect.width(),
  1463. item.ClientRect.height()
  1464. );
  1465. }
  1466. else
  1467. {
  1468. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1469. CRect cRect = item.ClientRect;
  1470. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1471. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1472. rect = new Rect(
  1473. (pdfPoint.X - cRect.width() / 2),
  1474. (pdfPoint.Y - cRect.height() / 2),
  1475. cRect.width(),
  1476. cRect.height()
  1477. );
  1478. }
  1479. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1480. InkParam newInkParam = new InkParam();
  1481. item.CopyTo(newInkParam);
  1482. if (newInkParam.InkPath != null && newInkParam.InkPath.Count > 0)
  1483. {
  1484. float offsetX = setRect.left - item.ClientRect.left;
  1485. float offsetY = setRect.top - item.ClientRect.top;
  1486. List<List<CPoint>> arrangeList = new List<List<CPoint>>();
  1487. foreach (List<CPoint> inkNode in newInkParam.InkPath)
  1488. {
  1489. List<CPoint> inkPath = new List<CPoint>();
  1490. arrangeList.Add(inkPath);
  1491. foreach (CPoint addPoint in inkNode)
  1492. {
  1493. inkPath.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1494. }
  1495. }
  1496. newInkParam.InkPath = arrangeList;
  1497. newInkParam.ClientRect = setRect;
  1498. }
  1499. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1500. if (cPDFPage == null)
  1501. return;
  1502. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newInkParam.CurrentType);
  1503. CreateDefaultAnnot(cPDFAnnotation, newInkParam.CurrentType, newInkParam);
  1504. cPDFAnnotation.UpdateAp();
  1505. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1506. (annotHistory as InkAnnotHistory).CurrentParam = (InkParam)annotParam;
  1507. }
  1508. break;
  1509. case C_ANNOTATION_TYPE.C_ANNOTATION_POPUP:
  1510. break;
  1511. case C_ANNOTATION_TYPE.C_ANNOTATION_FILEATTACHMENT:
  1512. break;
  1513. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  1514. {
  1515. rightPressPoint = point;
  1516. bool result = PasteWidgetData(ref annotHistory, item as WidgetParm);
  1517. if (!result)
  1518. return;
  1519. }
  1520. break;
  1521. case C_ANNOTATION_TYPE.C_ANNOTATION_SCREEN:
  1522. break;
  1523. case C_ANNOTATION_TYPE.C_ANNOTATION_PRINTERMARK:
  1524. break;
  1525. case C_ANNOTATION_TYPE.C_ANNOTATION_TRAPNET:
  1526. break;
  1527. case C_ANNOTATION_TYPE.C_ANNOTATION_WATERMARK:
  1528. break;
  1529. case C_ANNOTATION_TYPE.C_ANNOTATION_3D:
  1530. break;
  1531. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  1532. break;
  1533. case C_ANNOTATION_TYPE.C_ANNOTATION_INTERCHANGE:
  1534. break;
  1535. default:
  1536. break;
  1537. }
  1538. PasteParam = annotHistory.CurrentParam;
  1539. PDFViewer.UndoManager.AddHistory(annotHistory);
  1540. }
  1541. }
  1542. private void DeleteAnnotData()
  1543. {
  1544. dynamic notifyData = null;
  1545. if (cacheHitTestAnnot != null)
  1546. {
  1547. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheHitTestAnnot.GetAnnotData().Annot);
  1548. if (annotHistory == null)
  1549. {
  1550. return;
  1551. }
  1552. AnnotParam annotParam = null;
  1553. if (cacheHitTestAnnot is BaseWidget)
  1554. {
  1555. annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1556. }
  1557. else
  1558. {
  1559. annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1560. }
  1561. if (annotParam != null)
  1562. {
  1563. notifyData = new ExpandoObject();
  1564. notifyData.Action = HistoryAction.Remove;
  1565. notifyData.PageIndex = annotParam.PageIndex;
  1566. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1567. notifyData.AnnotType = annotParam.CurrentType;
  1568. notifyData.CurrentParam = annotParam;
  1569. if (annotParam is WidgetParm)
  1570. {
  1571. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1572. }
  1573. annotHistory.CurrentParam = annotParam;
  1574. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1575. annotHistory.Action = HistoryAction.Remove;
  1576. PDFViewer.UndoManager.AddHistory(annotHistory);
  1577. List<C_ANNOTATION_TYPE> checkEditType = new List<C_ANNOTATION_TYPE>()
  1578. {
  1579. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1580. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1581. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  1582. };
  1583. if (checkEditType.Contains(cacheHitTestAnnot.CurrentType))
  1584. {
  1585. //Need to clear the measurement selected edit box.
  1586. CleanEditAnnot();
  1587. }
  1588. cacheHitTestAnnot.RemoveClean(true);
  1589. cacheHitTestAnnot.GetAnnotData().Annot.RemoveAnnot();
  1590. cacheHitTestAnnot = null;
  1591. }
  1592. CleanSelectedRect();
  1593. }
  1594. if (cacheMoveWidget != null)
  1595. {
  1596. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheMoveWidget.GetAnnotData().Annot);
  1597. if (annotHistory == null)
  1598. {
  1599. return;
  1600. }
  1601. AnnotParam annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1602. if (annotParam != null)
  1603. {
  1604. notifyData = new ExpandoObject();
  1605. notifyData.Action = HistoryAction.Remove;
  1606. notifyData.PageIndex = annotParam.PageIndex;
  1607. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1608. notifyData.AnnotType = annotParam.CurrentType;
  1609. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1610. notifyData.CurrentParam = annotParam;
  1611. annotHistory.CurrentParam = annotParam;
  1612. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1613. annotHistory.Action = HistoryAction.Remove;
  1614. PDFViewer.UndoManager.AddHistory(annotHistory);
  1615. cacheMoveWidget.GetAnnotData().Annot.RemoveAnnot();
  1616. cacheMoveWidget = null;
  1617. }
  1618. CleanSelectedRect();
  1619. }
  1620. if (notifyData != null)
  1621. {
  1622. AnnotChanged?.Invoke(this, notifyData);
  1623. }
  1624. }
  1625. private bool PasteWidgetData(ref AnnotHistory annotHistory, WidgetParm item)
  1626. {
  1627. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1628. Point point = rightPressPoint;
  1629. rightPressPoint = new Point(-1, -1);
  1630. Rect rect;
  1631. int index = -1;
  1632. if (point.Equals(new Point(-1, -1)))
  1633. {
  1634. index = item.PageIndex;
  1635. rect = new Rect(
  1636. (item.ClientRect.left + 25),
  1637. (item.ClientRect.top + 25),
  1638. item.ClientRect.width(),
  1639. item.ClientRect.height()
  1640. );
  1641. }
  1642. else
  1643. {
  1644. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1645. CRect cRect = item.ClientRect;
  1646. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1647. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1648. rect = new Rect(
  1649. (pdfPoint.X - cRect.width() / 2),
  1650. (pdfPoint.Y - cRect.height() / 2),
  1651. cRect.width(),
  1652. cRect.height()
  1653. );
  1654. }
  1655. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1656. if (cPDFPage == null)
  1657. return false;
  1658. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateWidget(item.WidgetType);
  1659. CreateDefaultWidget(cPDFAnnotation, item.WidgetType, item);
  1660. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1661. cPDFAnnotation.SetRect(setRect);
  1662. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  1663. AnnotParam annotParam = ParamConverter.WidgetConverter(cPDFDocument, cPDFAnnotation);
  1664. switch (item.WidgetType)
  1665. {
  1666. case C_WIDGET_TYPE.WIDGET_NONE:
  1667. break;
  1668. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1669. annotHistory = new PushButtonHistory();
  1670. (annotHistory as PushButtonHistory).CurrentParam = (PushButtonParam)annotParam;
  1671. annotHistory.PDFDoc = cPDFDocument;
  1672. break;
  1673. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1674. annotHistory = new CheckBoxHistory();
  1675. (annotHistory as CheckBoxHistory).CurrentParam = (CheckBoxParam)annotParam;
  1676. annotHistory.PDFDoc = cPDFDocument;
  1677. break;
  1678. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1679. annotHistory = new RadioButtonHistory();
  1680. (annotHistory as RadioButtonHistory).CurrentParam = (RadioButtonParam)annotParam;
  1681. annotHistory.PDFDoc = cPDFDocument;
  1682. break;
  1683. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1684. annotHistory = new TextBoxHistory();
  1685. (annotHistory as TextBoxHistory).CurrentParam = (TextBoxParam)annotParam;
  1686. annotHistory.PDFDoc = cPDFDocument;
  1687. break;
  1688. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1689. annotHistory = new ComboBoxHistory();
  1690. (annotHistory as ComboBoxHistory).CurrentParam = (ComboBoxParam)annotParam;
  1691. annotHistory.PDFDoc = cPDFDocument;
  1692. break;
  1693. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  1694. annotHistory = new ListBoxHistory();
  1695. (annotHistory as ListBoxHistory).CurrentParam = (ListBoxParam)annotParam;
  1696. annotHistory.PDFDoc = cPDFDocument;
  1697. break;
  1698. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1699. annotHistory = new SignatureHistory();
  1700. (annotHistory as SignatureHistory).CurrentParam = (SignatureParam)annotParam;
  1701. annotHistory.PDFDoc = cPDFDocument;
  1702. break;
  1703. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1704. break;
  1705. default:
  1706. break;
  1707. }
  1708. return true;
  1709. }
  1710. #endregion
  1711. }
  1712. }