CPDFViewerTool.Command.cs 82 KB

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