CPDFViewerTool.Command.cs 82 KB

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