CPDFViewerTool.Command.cs 74 KB

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