CPDFViewerTool.Command.cs 75 KB

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