CPDFViewerTool.FindReplace.cs 12 KB

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