CPDFViewerTool.FindReplace.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 | CPDFEditType.EditPath);
  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. editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
  155. GoToFoundDestination(editTextFindSelection);
  156. isOtherPage = false;
  157. canLoop = true;
  158. return true;
  159. }
  160. public bool FindNext()
  161. {
  162. if (currentPageIndex == -1)
  163. {
  164. return false;
  165. }
  166. if (!CheckPageVisiable())
  167. {
  168. FindText();
  169. }
  170. int tempSelectionIndex = 0;
  171. if (isReplaced)
  172. {
  173. tempSelectionIndex = currentSelectionIndex;
  174. isReplaced = false;
  175. }
  176. else
  177. {
  178. tempSelectionIndex = currentSelectionIndex + 1;
  179. }
  180. if (tempSelectionIndex < editTextFindSelectionList?.Count)
  181. {
  182. currentSelectionIndex = tempSelectionIndex;
  183. }
  184. else
  185. {
  186. // Search across pages downwards, only allow looping when reaching the bottom for the first time
  187. if (currentPageIndex != PDFViewer.GetDocument().PageCount - 1)
  188. {
  189. tempPageIndex = currentPageIndex + 1;
  190. }
  191. else
  192. {
  193. if (canLoop)
  194. {
  195. tempPageIndex = 0;
  196. canLoop = false;
  197. }
  198. else
  199. {
  200. tempPageIndex += 1;
  201. }
  202. }
  203. if (tempPageIndex < PDFViewer.GetDocument().PageCount)
  204. {
  205. nextPageIndex = tempPageIndex;
  206. FindText();
  207. currentSelectionIndex = -1;
  208. return FindNext();
  209. }
  210. // Failed to find, set the search status to the initial state
  211. else
  212. {
  213. canLoop = true;
  214. return false;
  215. }
  216. }
  217. editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
  218. GoToFoundDestination(editTextFindSelection);
  219. canLoop = true;
  220. return true;
  221. }
  222. private void GoToFoundDestination(CPDFEditTextFindSelection editTextFindSelection, bool isReplacing = false)
  223. {
  224. float Position_X = editTextFindSelection.RectList[0].left;
  225. float Position_Y = editTextFindSelection.RectList[0].top;
  226. editTextFindSelection.GetTextArea(isReplacing).GetLastSelectChars();
  227. SelectedEditAreaForIndex(editTextFindSelection.GetPageIndex(), editTextFindSelection.GetTextAreaIndex());
  228. PDFViewer.GoToPage(editTextFindSelection.GetPageIndex(), new System.Windows.Point(Position_X, Position_Y));
  229. }
  230. public bool ReplaceText(string text)
  231. {
  232. if (findSelectionEditPage != null)
  233. {
  234. // Automatically search down when continuously triggered for replacement
  235. if (isReplaced)
  236. {
  237. FindNext();
  238. }
  239. // An overflow error occurred: Just translated and started searching, but no content was selected
  240. if (currentSelectionIndex < 0 || currentSelectionIndex > editTextFindSelectionList.Count)
  241. {
  242. return false;
  243. }
  244. // Current search age has no replaceable content error
  245. if (editTextFindSelectionList.Count == 0)
  246. {
  247. return false;
  248. }
  249. // Get the current search result
  250. CPDFEditTextFindSelection editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
  251. var area = editTextFindSelection.GetTextArea();
  252. var rect = area.SelectLineRects[0];
  253. CPDFEditTextArea editTextArea = findSelectionEditPage.ReplaceText(editTextFindSelection, text);
  254. editTextFindSelection.GetTextArea(true).GetLastSelectChars();
  255. if (editTextArea != null)
  256. {
  257. findSelectionEditPage.EndEdit();
  258. findSelectionEditPage.FindText(findText, options);
  259. editTextFindSelectionList = findSelectionEditPage.GetTextFindSelectionList();
  260. {
  261. FindReplaceHistory findReplaceHistory = new FindReplaceHistory();
  262. findReplaceHistory.EditPage = findSelectionEditPage;
  263. findReplaceHistory.PageIndex = findSelectionEditPage.GetPageIndex();
  264. PDFViewer.UndoManager.AddHistory(findReplaceHistory);
  265. }
  266. }
  267. isReplaced = true;
  268. GoToFoundDestination(editTextFindSelection, true);
  269. return true;
  270. }
  271. return false;
  272. }
  273. public int ReplaceAllText(string replaceText)
  274. {
  275. int changeCount = 0;
  276. for (int pageIndex = 0; pageIndex < PDFViewer.GetDocument().PageCount; pageIndex++)
  277. {
  278. CPDFPage page = PDFViewer.GetDocument().PageAtIndex(pageIndex);
  279. CPDFEditPage editPage = page.GetEditPage();
  280. editPage.BeginEdit(CPDFEditType.EditText | CPDFEditType.EditImage | CPDFEditType.EditPath);
  281. editPage.FindText(findText, options);
  282. List<CPDFEditTextFindSelection> editTextFindSelectionList = editPage.GetTextFindSelectionList();
  283. for (int i = editTextFindSelectionList.Count - 1; i >= 0; i--)
  284. {
  285. if (editTextFindSelectionList.Count == 0)
  286. {
  287. continue;
  288. }
  289. changeCount += editTextFindSelectionList.Count;
  290. editPage.FindText(findText, options);
  291. editPage.ReplaceText(editTextFindSelectionList[i], replaceText);
  292. }
  293. }
  294. ResetFindPlaceStatus();
  295. SelectedEditAreaForIndex(-1, -1);
  296. PDFViewer.UpdateRenderFrame();
  297. StartFindText(findText, options);
  298. return 0;
  299. }
  300. private void ResetFindPlaceStatus()
  301. {
  302. currentSelectionIndex = -1;
  303. tempPageIndex = -1;
  304. isOtherPage = false;
  305. isReplaced = false;
  306. canLoop = true;
  307. }
  308. }
  309. }