CPDFViewerTool.Command.cs 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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.ClientRect;
  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. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1440. cPDFAnnotation.SetRect(setRect);
  1441. cPDFAnnotation.UpdateAp();
  1442. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1443. (annotHistory as StampAnnotHistory).CurrentParam = (StampParam)annotParam;
  1444. }
  1445. break;
  1446. case C_ANNOTATION_TYPE.C_ANNOTATION_CARET:
  1447. break;
  1448. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1449. {
  1450. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1451. Rect rect;
  1452. annotHistory = new InkAnnotHistory();
  1453. annotHistory.PDFDoc = cPDFDocument;
  1454. int index = -1;
  1455. if (point.Equals(new Point(-1, -1)))
  1456. {
  1457. index = item.PageIndex;
  1458. rect = new Rect(
  1459. (item.ClientRect.left + 25),
  1460. (item.ClientRect.top + 25),
  1461. item.ClientRect.width(),
  1462. item.ClientRect.height()
  1463. );
  1464. }
  1465. else
  1466. {
  1467. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1468. CRect cRect = item.ClientRect;
  1469. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1470. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1471. rect = new Rect(
  1472. (pdfPoint.X - cRect.width() / 2),
  1473. (pdfPoint.Y - cRect.height() / 2),
  1474. cRect.width(),
  1475. cRect.height()
  1476. );
  1477. }
  1478. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1479. InkParam newInkParam = new InkParam();
  1480. item.CopyTo(newInkParam);
  1481. if (newInkParam.InkPath != null && newInkParam.InkPath.Count > 0)
  1482. {
  1483. float offsetX = setRect.left - item.ClientRect.left;
  1484. float offsetY = setRect.top - item.ClientRect.top;
  1485. List<List<CPoint>> arrangeList = new List<List<CPoint>>();
  1486. foreach (List<CPoint> inkNode in newInkParam.InkPath)
  1487. {
  1488. List<CPoint> inkPath = new List<CPoint>();
  1489. arrangeList.Add(inkPath);
  1490. foreach (CPoint addPoint in inkNode)
  1491. {
  1492. inkPath.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1493. }
  1494. }
  1495. newInkParam.InkPath = arrangeList;
  1496. newInkParam.ClientRect = setRect;
  1497. }
  1498. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1499. if (cPDFPage == null)
  1500. return;
  1501. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newInkParam.CurrentType);
  1502. CreateDefaultAnnot(cPDFAnnotation, newInkParam.CurrentType, newInkParam);
  1503. cPDFAnnotation.UpdateAp();
  1504. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1505. (annotHistory as InkAnnotHistory).CurrentParam = (InkParam)annotParam;
  1506. }
  1507. break;
  1508. case C_ANNOTATION_TYPE.C_ANNOTATION_POPUP:
  1509. break;
  1510. case C_ANNOTATION_TYPE.C_ANNOTATION_FILEATTACHMENT:
  1511. break;
  1512. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  1513. {
  1514. rightPressPoint = point;
  1515. bool result = PasteWidgetData(ref annotHistory, item as WidgetParm);
  1516. if (!result)
  1517. return;
  1518. }
  1519. break;
  1520. case C_ANNOTATION_TYPE.C_ANNOTATION_SCREEN:
  1521. break;
  1522. case C_ANNOTATION_TYPE.C_ANNOTATION_PRINTERMARK:
  1523. break;
  1524. case C_ANNOTATION_TYPE.C_ANNOTATION_TRAPNET:
  1525. break;
  1526. case C_ANNOTATION_TYPE.C_ANNOTATION_WATERMARK:
  1527. break;
  1528. case C_ANNOTATION_TYPE.C_ANNOTATION_3D:
  1529. break;
  1530. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  1531. break;
  1532. case C_ANNOTATION_TYPE.C_ANNOTATION_INTERCHANGE:
  1533. break;
  1534. default:
  1535. break;
  1536. }
  1537. PasteParam = annotHistory.CurrentParam;
  1538. PDFViewer.UndoManager.AddHistory(annotHistory);
  1539. }
  1540. }
  1541. private void DeleteAnnotData()
  1542. {
  1543. dynamic notifyData = null;
  1544. if (cacheHitTestAnnot != null)
  1545. {
  1546. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheHitTestAnnot.GetAnnotData().Annot);
  1547. if (annotHistory == null)
  1548. {
  1549. return;
  1550. }
  1551. AnnotParam annotParam = null;
  1552. if (cacheHitTestAnnot is BaseWidget)
  1553. {
  1554. annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1555. }
  1556. else
  1557. {
  1558. annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1559. }
  1560. if (annotParam != null)
  1561. {
  1562. notifyData = new ExpandoObject();
  1563. notifyData.Action = HistoryAction.Remove;
  1564. notifyData.PageIndex = annotParam.PageIndex;
  1565. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1566. notifyData.AnnotType = annotParam.CurrentType;
  1567. notifyData.CurrentParam = annotParam;
  1568. if (annotParam is WidgetParm)
  1569. {
  1570. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1571. }
  1572. annotHistory.CurrentParam = annotParam;
  1573. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1574. annotHistory.Action = HistoryAction.Remove;
  1575. PDFViewer.UndoManager.AddHistory(annotHistory);
  1576. List<C_ANNOTATION_TYPE> checkEditType = new List<C_ANNOTATION_TYPE>()
  1577. {
  1578. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1579. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1580. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  1581. };
  1582. if (checkEditType.Contains(cacheHitTestAnnot.CurrentType))
  1583. {
  1584. //Need to clear the measurement selected edit box.
  1585. CleanEditAnnot();
  1586. }
  1587. cacheHitTestAnnot.RemoveClean(true);
  1588. cacheHitTestAnnot.GetAnnotData().Annot.RemoveAnnot();
  1589. cacheHitTestAnnot = null;
  1590. }
  1591. CleanSelectedRect();
  1592. }
  1593. if (cacheMoveWidget != null)
  1594. {
  1595. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheMoveWidget.GetAnnotData().Annot);
  1596. if (annotHistory == null)
  1597. {
  1598. return;
  1599. }
  1600. AnnotParam annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1601. if (annotParam != null)
  1602. {
  1603. notifyData = new ExpandoObject();
  1604. notifyData.Action = HistoryAction.Remove;
  1605. notifyData.PageIndex = annotParam.PageIndex;
  1606. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1607. notifyData.AnnotType = annotParam.CurrentType;
  1608. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1609. notifyData.CurrentParam = annotParam;
  1610. annotHistory.CurrentParam = annotParam;
  1611. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1612. annotHistory.Action = HistoryAction.Remove;
  1613. PDFViewer.UndoManager.AddHistory(annotHistory);
  1614. cacheMoveWidget.GetAnnotData().Annot.RemoveAnnot();
  1615. cacheMoveWidget = null;
  1616. }
  1617. CleanSelectedRect();
  1618. }
  1619. if (notifyData != null)
  1620. {
  1621. AnnotChanged?.Invoke(this, notifyData);
  1622. }
  1623. }
  1624. private bool PasteWidgetData(ref AnnotHistory annotHistory, WidgetParm item)
  1625. {
  1626. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1627. Point point = rightPressPoint;
  1628. rightPressPoint = new Point(-1, -1);
  1629. Rect rect;
  1630. int index = -1;
  1631. if (point.Equals(new Point(-1, -1)))
  1632. {
  1633. index = item.PageIndex;
  1634. rect = new Rect(
  1635. (item.ClientRect.left + 25),
  1636. (item.ClientRect.top + 25),
  1637. item.ClientRect.width(),
  1638. item.ClientRect.height()
  1639. );
  1640. }
  1641. else
  1642. {
  1643. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1644. CRect cRect = item.ClientRect;
  1645. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1646. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1647. rect = new Rect(
  1648. (pdfPoint.X - cRect.width() / 2),
  1649. (pdfPoint.Y - cRect.height() / 2),
  1650. cRect.width(),
  1651. cRect.height()
  1652. );
  1653. }
  1654. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1655. if (cPDFPage == null)
  1656. return false;
  1657. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateWidget(item.WidgetType);
  1658. CreateDefaultWidget(cPDFAnnotation, item.WidgetType, item);
  1659. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1660. cPDFAnnotation.SetRect(setRect);
  1661. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  1662. AnnotParam annotParam = ParamConverter.WidgetConverter(cPDFDocument, cPDFAnnotation);
  1663. switch (item.WidgetType)
  1664. {
  1665. case C_WIDGET_TYPE.WIDGET_NONE:
  1666. break;
  1667. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1668. annotHistory = new PushButtonHistory();
  1669. (annotHistory as PushButtonHistory).CurrentParam = (PushButtonParam)annotParam;
  1670. annotHistory.PDFDoc = cPDFDocument;
  1671. break;
  1672. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1673. annotHistory = new CheckBoxHistory();
  1674. (annotHistory as CheckBoxHistory).CurrentParam = (CheckBoxParam)annotParam;
  1675. annotHistory.PDFDoc = cPDFDocument;
  1676. break;
  1677. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1678. annotHistory = new RadioButtonHistory();
  1679. (annotHistory as RadioButtonHistory).CurrentParam = (RadioButtonParam)annotParam;
  1680. annotHistory.PDFDoc = cPDFDocument;
  1681. break;
  1682. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1683. annotHistory = new TextBoxHistory();
  1684. (annotHistory as TextBoxHistory).CurrentParam = (TextBoxParam)annotParam;
  1685. annotHistory.PDFDoc = cPDFDocument;
  1686. break;
  1687. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1688. annotHistory = new ComboBoxHistory();
  1689. (annotHistory as ComboBoxHistory).CurrentParam = (ComboBoxParam)annotParam;
  1690. annotHistory.PDFDoc = cPDFDocument;
  1691. break;
  1692. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  1693. annotHistory = new ListBoxHistory();
  1694. (annotHistory as ListBoxHistory).CurrentParam = (ListBoxParam)annotParam;
  1695. annotHistory.PDFDoc = cPDFDocument;
  1696. break;
  1697. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1698. annotHistory = new SignatureHistory();
  1699. (annotHistory as SignatureHistory).CurrentParam = (SignatureParam)annotParam;
  1700. annotHistory.PDFDoc = cPDFDocument;
  1701. break;
  1702. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1703. break;
  1704. default:
  1705. break;
  1706. }
  1707. return true;
  1708. }
  1709. #endregion
  1710. }
  1711. }