CPDFViewerTool.Command.cs 82 KB

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