CPDFViewerTool.FindReplace.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKit.PDFPage.Edit;
  3. using ComPDFKit.Tool.UndoManger;
  4. using ComPDFKit.Tool.UndoManger.FindReplaceHistory;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. namespace ComPDFKit.Tool
  9. {
  10. public partial class CPDFViewerTool
  11. {
  12. // find text
  13. private string findText;
  14. // find options
  15. private C_Search_Options options;
  16. // current selection index in editTextFindSelectionList
  17. private int currentSelectionIndex = -1;
  18. // current selection
  19. private CPDFEditTextFindSelection currentTextFindSelection;
  20. // EditTextFindSelection list in single page
  21. private List<CPDFEditTextFindSelection> editTextFindSelectionList;
  22. // Whether to allow circular search to avoid infinite loops when there are no matches in the entire document
  23. private bool canLoop = true;
  24. // When replacement occurs, set this variable to true
  25. private bool isReplaced = false;
  26. // Set this variable to true when there is a page jump during the search process
  27. private bool isOtherPage = false;
  28. // current selection
  29. private CPDFEditTextFindSelection editTextFindSelection;
  30. private CPDFEditPage findSelectionEditPage;
  31. private int nextPageIndex = 0;
  32. private int tempPageIndex = -1;
  33. private int currentPageIndex
  34. {
  35. get
  36. {
  37. if (findSelectionEditPage != null)
  38. {
  39. return findSelectionEditPage.GetPageIndex();
  40. }
  41. else
  42. {
  43. return -1;
  44. }
  45. }
  46. }
  47. public bool CheckPageVisiable()
  48. {
  49. return (currentPageIndex >= PDFViewer.CurrentRenderFrame.Renders.First().Key &&
  50. currentPageIndex <= PDFViewer.CurrentRenderFrame.Renders.Last().Key);
  51. }
  52. public void StartFindText(string findText, C_Search_Options options)
  53. {
  54. tempPageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  55. // Check if the current mode is content editing
  56. if (currentModel != ToolType.ContentEdit)
  57. {
  58. SetToolType(ToolType.ContentEdit);
  59. }
  60. // Check if the findText is empty
  61. if (string.IsNullOrEmpty(findText))
  62. {
  63. return;
  64. }
  65. if (this.findText != findText)
  66. {
  67. }
  68. this.findText = findText;
  69. this.options = options;
  70. canLoop = true;
  71. FindText();
  72. }
  73. internal void FindText([CallerMemberName] string caller = "")
  74. {
  75. // Start searching from the currently displayed page
  76. CPDFPage pdfPage = GetCPDFViewer().GetDocument().PageAtIndex(nextPageIndex);
  77. findSelectionEditPage = pdfPage.GetEditPage();
  78. findSelectionEditPage.BeginEdit(contentEditType);
  79. // If the passed value is null, it will cause the program to freeze
  80. if (string.IsNullOrEmpty(findText))
  81. {
  82. return;
  83. }
  84. findSelectionEditPage.FindText(findText, options);
  85. // Get all search content of the current page for subsequent search and highlighting
  86. editTextFindSelectionList = findSelectionEditPage.GetTextFindSelectionList();
  87. if (caller == "FindPrevious")
  88. {
  89. currentSelectionIndex = editTextFindSelectionList.Count;
  90. }
  91. else
  92. {
  93. currentSelectionIndex = -1;
  94. }
  95. isReplaced = false;
  96. }
  97. public bool FindPrevious()
  98. {
  99. if (!CheckPageVisiable())
  100. {
  101. FindText();
  102. }
  103. int tempSelectionIndex = currentSelectionIndex - 1;
  104. if (tempSelectionIndex >= 0)
  105. {
  106. currentSelectionIndex = tempSelectionIndex;
  107. }
  108. // The result of the previous option does not exist, try to find the result of another page
  109. else
  110. {
  111. // When there is no cross-page search, you need to preset the cross-page status
  112. if (!isOtherPage && editTextFindSelectionList?.Count != 0)
  113. {
  114. nextPageIndex = currentPageIndex;
  115. isOtherPage = true;
  116. }
  117. if (currentPageIndex != 0)
  118. {
  119. tempPageIndex = currentPageIndex - 1;
  120. }
  121. else
  122. {
  123. if (canLoop)
  124. {
  125. tempPageIndex = PDFViewer.GetDocument().PageCount - 1;
  126. canLoop = false;
  127. }
  128. else
  129. {
  130. tempPageIndex -= 1;
  131. }
  132. }
  133. // Search upwards
  134. if (tempPageIndex >= 0)
  135. {
  136. nextPageIndex = tempPageIndex;
  137. FindText();
  138. currentSelectionIndex = editTextFindSelectionList.Count;
  139. return FindPrevious();
  140. }
  141. // Failed to find, set the search status to the initial state
  142. else
  143. {
  144. isOtherPage = false;
  145. canLoop = true;
  146. return false;
  147. }
  148. }
  149. editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
  150. GoToFoundDestination(editTextFindSelection);
  151. isOtherPage = false;
  152. canLoop = true;
  153. return true;
  154. }
  155. public bool FindNext()
  156. {
  157. if (currentPageIndex == -1)
  158. {
  159. return false;
  160. }
  161. if (!CheckPageVisiable())
  162. {
  163. FindText();
  164. }
  165. int tempSelectionIndex = 0;
  166. if (isReplaced)
  167. {
  168. tempSelectionIndex = currentSelectionIndex;
  169. isReplaced = false;
  170. }
  171. else
  172. {
  173. tempSelectionIndex = currentSelectionIndex + 1;
  174. }
  175. if (tempSelectionIndex < editTextFindSelectionList?.Count)
  176. {
  177. currentSelectionIndex = tempSelectionIndex;
  178. }
  179. else
  180. {
  181. // Search across pages downwards, only allow looping when reaching the bottom for the first time
  182. if (currentPageIndex != PDFViewer.GetDocument().PageCount - 1)
  183. {
  184. tempPageIndex = currentPageIndex + 1;
  185. }
  186. else
  187. {
  188. if (canLoop)
  189. {
  190. tempPageIndex = 0;
  191. canLoop = false;
  192. }
  193. else
  194. {
  195. tempPageIndex += 1;
  196. }
  197. }
  198. if (tempPageIndex < PDFViewer.GetDocument().PageCount)
  199. {
  200. nextPageIndex = tempPageIndex;
  201. FindText();
  202. currentSelectionIndex = -1;
  203. return FindNext();
  204. }
  205. // Failed to find, set the search status to the initial state
  206. else
  207. {
  208. canLoop = true;
  209. return false;
  210. }
  211. }
  212. editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
  213. GoToFoundDestination(editTextFindSelection);
  214. canLoop = true;
  215. return true;
  216. }
  217. private void GoToFoundDestination(CPDFEditTextFindSelection editTextFindSelection, bool isReplacing = false)
  218. {
  219. float Position_X = editTextFindSelection.RectList[0].left;
  220. float Position_Y = editTextFindSelection.RectList[0].top;
  221. editTextFindSelection.GetTextArea(isReplacing).GetLastSelectChars();
  222. SelectedEditAreaForIndex(editTextFindSelection.GetPageIndex(), editTextFindSelection.GetTextAreaIndex());
  223. PDFViewer.GoToPage(editTextFindSelection.GetPageIndex(), new System.Windows.Point(Position_X, Position_Y));
  224. }
  225. public bool ReplaceText(string text)
  226. {
  227. if (findSelectionEditPage != null)
  228. {
  229. // Automatically search down when continuously triggered for replacement
  230. if (isReplaced)
  231. {
  232. FindNext();
  233. }
  234. // An overflow error occurred: Just translated and started searching, but no content was selected
  235. if (currentSelectionIndex < 0 || currentSelectionIndex > editTextFindSelectionList.Count)
  236. {
  237. return false;
  238. }
  239. // Current search age has no replaceable content error
  240. if (editTextFindSelectionList.Count == 0)
  241. {
  242. return false;
  243. }
  244. // Get the current search result
  245. CPDFEditTextFindSelection editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
  246. var area = editTextFindSelection.GetTextArea();
  247. var rect = area.SelectLineRects[0];
  248. CPDFEditTextArea editTextArea = findSelectionEditPage.ReplaceText(editTextFindSelection, text);
  249. editTextFindSelection.GetTextArea(true).GetLastSelectChars();
  250. if (editTextArea != null)
  251. {
  252. findSelectionEditPage.EndEdit();
  253. findSelectionEditPage.FindText(findText, options);
  254. editTextFindSelectionList = findSelectionEditPage.GetTextFindSelectionList();
  255. {
  256. FindReplaceHistory findReplaceHistory = new FindReplaceHistory();
  257. findReplaceHistory.EditPage = findSelectionEditPage;
  258. findReplaceHistory.PageIndex = findSelectionEditPage.GetPageIndex();
  259. PDFViewer.UndoManager.AddHistory(findReplaceHistory);
  260. }
  261. }
  262. isReplaced = true;
  263. GoToFoundDestination(editTextFindSelection, true);
  264. return true;
  265. }
  266. return false;
  267. }
  268. public int ReplaceAllText(string replaceText)
  269. {
  270. int changeCount = 0;
  271. GroupHistory groupHistory = new GroupHistory();
  272. for (int pageIndex = 0; pageIndex < PDFViewer.GetDocument().PageCount; pageIndex++)
  273. {
  274. CPDFPage page = PDFViewer.GetDocument().PageAtIndex(pageIndex);
  275. CPDFEditPage editPage = page.GetEditPage();
  276. editPage.BeginEdit(contentEditType);
  277. editPage.FindText(findText, options);
  278. List<CPDFEditTextFindSelection> editTextFindSelectionList = editPage.GetTextFindSelectionList();
  279. if (editTextFindSelectionList.Count == 0)
  280. {
  281. continue;
  282. }
  283. changeCount += editTextFindSelectionList.Count;
  284. for (int i = editTextFindSelectionList.Count - 1; i >= 0; i--)
  285. {
  286. editPage.FindText(findText, options);
  287. editPage.ReplaceText(editTextFindSelectionList[i], replaceText);
  288. FindReplaceHistory findReplaceHistory = new FindReplaceHistory();
  289. findReplaceHistory.EditPage = editPage;
  290. findReplaceHistory.PageIndex = editPage.GetPageIndex();
  291. groupHistory.Histories.Add(findReplaceHistory);
  292. }
  293. }
  294. ResetFindPlaceStatus();
  295. SelectedEditAreaForIndex(-1, -1);
  296. PDFViewer.UndoManager.AddHistory(groupHistory);
  297. PDFViewer.UpdateRenderFrame();
  298. return changeCount;
  299. }
  300. private void ResetFindPlaceStatus()
  301. {
  302. currentSelectionIndex = -1;
  303. tempPageIndex = -1;
  304. isOtherPage = false;
  305. isReplaced = false;
  306. canLoop = true;
  307. }
  308. }
  309. }