123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- using ComPDFKit.PDFPage;
- using ComPDFKit.PDFPage.Edit;
- using ComPDFKit.Tool.Help;
- using ComPDFKit.Tool.UndoManger.FindReplaceHistory;
- using ComPDFKit.Viewer.Helper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace ComPDFKit.Tool
- {
- public partial class CPDFViewerTool
- {
-
- private string findText;
-
- private C_Search_Options options;
-
- private int currentSelectionIndex = -1;
-
- private CPDFEditTextFindSelection currentTextFindSelection;
-
- private List<CPDFEditTextFindSelection> editTextFindSelectionList;
-
- private bool canLoop = true;
-
- private bool isReplaced = false;
-
- private bool isOtherPage = false;
-
- private CPDFEditTextFindSelection editTextFindSelection;
- private CPDFEditPage findSelectionEditPage;
- private int nextPageIndex = 0;
- private int tempPageIndex = -1;
- private int currentPageIndex
- {
- get
- {
- if (findSelectionEditPage != null)
- {
- return findSelectionEditPage.GetPageIndex();
- }
- else
- {
- return -1;
- }
- }
- }
- public bool CheckPageVisiable()
- {
- return (currentPageIndex >= PDFViewer.CurrentRenderFrame.Renders.First().Key &&
- currentPageIndex <= PDFViewer.CurrentRenderFrame.Renders.Last().Key);
- }
- public void StartFindText(string findText, C_Search_Options options)
- {
- tempPageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
-
- if (currentModel != ToolType.ContentEdit)
- {
- SetToolType(ToolType.ContentEdit);
- }
-
- if (string.IsNullOrEmpty(findText))
- {
- return;
- }
- if (this.findText != findText)
- {
- }
- this.findText = findText;
- this.options = options;
- canLoop = true;
- FindText();
- }
- internal void FindText([CallerMemberName] string caller = "")
- {
-
- CPDFPage pdfPage = GetCPDFViewer().GetDocument().PageAtIndex(nextPageIndex);
- findSelectionEditPage = pdfPage.GetEditPage();
- findSelectionEditPage.BeginEdit(CPDFEditType.EditText | CPDFEditType.EditImage | CPDFEditType.EditPath);
-
- if (string.IsNullOrEmpty(findText))
- {
- return;
- }
- findSelectionEditPage.FindText(findText, options);
-
- editTextFindSelectionList = findSelectionEditPage.GetTextFindSelectionList();
- if (caller == "FindPrevious")
- {
- currentSelectionIndex = editTextFindSelectionList.Count;
- }
- else
- {
- currentSelectionIndex = -1;
- }
- isReplaced = false;
- }
- public bool FindPrevious()
- {
- if (!CheckPageVisiable())
- {
- FindText();
- }
- int tempSelectionIndex = currentSelectionIndex - 1;
- if (tempSelectionIndex >= 0)
- {
- currentSelectionIndex = tempSelectionIndex;
- }
-
- else
- {
-
- if (!isOtherPage && editTextFindSelectionList?.Count != 0)
- {
- nextPageIndex = currentPageIndex;
- isOtherPage = true;
- }
- if (currentPageIndex != 0)
- {
- tempPageIndex = currentPageIndex - 1;
- }
- else
- {
- if (canLoop)
- {
- tempPageIndex = PDFViewer.GetDocument().PageCount - 1;
- canLoop = false;
- }
- else
- {
- tempPageIndex -= 1;
- }
- }
-
- if (tempPageIndex >= 0)
- {
- nextPageIndex = tempPageIndex;
- FindText();
- currentSelectionIndex = editTextFindSelectionList.Count;
- return FindPrevious();
- }
-
- else
- {
- isOtherPage = false;
- canLoop = true;
- return false;
- }
- }
- editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
- GoToFoundDestination(editTextFindSelection);
- isOtherPage = false;
- canLoop = true;
- return true;
- }
- public bool FindNext()
- {
- if (currentPageIndex == -1)
- {
- return false;
- }
- if (!CheckPageVisiable())
- {
- FindText();
- }
- int tempSelectionIndex = 0;
- if (isReplaced)
- {
- tempSelectionIndex = currentSelectionIndex;
- isReplaced = false;
- }
- else
- {
- tempSelectionIndex = currentSelectionIndex + 1;
- }
- if (tempSelectionIndex < editTextFindSelectionList?.Count)
- {
- currentSelectionIndex = tempSelectionIndex;
- }
- else
- {
-
- if (currentPageIndex != PDFViewer.GetDocument().PageCount - 1)
- {
- tempPageIndex = currentPageIndex + 1;
- }
- else
- {
- if (canLoop)
- {
- tempPageIndex = 0;
- canLoop = false;
- }
- else
- {
- tempPageIndex += 1;
- }
- }
- if (tempPageIndex < PDFViewer.GetDocument().PageCount)
- {
- nextPageIndex = tempPageIndex;
- FindText();
- currentSelectionIndex = -1;
- return FindNext();
- }
-
- else
- {
- canLoop = true;
- return false;
- }
- }
- editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
- GoToFoundDestination(editTextFindSelection);
- canLoop = true;
- return true;
- }
- private void GoToFoundDestination(CPDFEditTextFindSelection editTextFindSelection, bool isReplacing = false)
- {
- float Position_X = editTextFindSelection.RectList[0].left;
- float Position_Y = editTextFindSelection.RectList[0].top;
- editTextFindSelection.GetTextArea(isReplacing).GetLastSelectChars();
- SelectedEditAreaForIndex(editTextFindSelection.GetPageIndex(), editTextFindSelection.GetTextAreaIndex());
- PDFViewer.GoToPage(editTextFindSelection.GetPageIndex(), new System.Windows.Point(Position_X, Position_Y));
- }
- public bool ReplaceText(string text)
- {
- if (findSelectionEditPage != null)
- {
-
- if (isReplaced)
- {
- FindNext();
- }
-
- if (currentSelectionIndex < 0 || currentSelectionIndex > editTextFindSelectionList.Count)
- {
- return false;
- }
-
- if (editTextFindSelectionList.Count == 0)
- {
- return false;
- }
-
- CPDFEditTextFindSelection editTextFindSelection = editTextFindSelectionList[currentSelectionIndex];
- var area = editTextFindSelection.GetTextArea();
- var rect = area.SelectLineRects[0];
- CPDFEditTextArea editTextArea = findSelectionEditPage.ReplaceText(editTextFindSelection, text);
- editTextFindSelection.GetTextArea(true).GetLastSelectChars();
- if (editTextArea != null)
- {
- findSelectionEditPage.EndEdit();
- findSelectionEditPage.FindText(findText, options);
- editTextFindSelectionList = findSelectionEditPage.GetTextFindSelectionList();
- {
- FindReplaceHistory findReplaceHistory = new FindReplaceHistory();
- findReplaceHistory.EditPage = findSelectionEditPage;
- findReplaceHistory.PageIndex = findSelectionEditPage.GetPageIndex();
- PDFViewer.UndoManager.AddHistory(findReplaceHistory);
- }
- }
- isReplaced = true;
- GoToFoundDestination(editTextFindSelection, true);
- return true;
- }
- return false;
- }
- public int ReplaceAllText(string replaceText)
- {
- int changeCount = 0;
- for (int pageIndex = 0; pageIndex < PDFViewer.GetDocument().PageCount; pageIndex++)
- {
- CPDFPage page = PDFViewer.GetDocument().PageAtIndex(pageIndex);
- CPDFEditPage editPage = page.GetEditPage();
- editPage.BeginEdit(CPDFEditType.EditText | CPDFEditType.EditImage | CPDFEditType.EditPath);
- editPage.FindText(findText, options);
- List<CPDFEditTextFindSelection> editTextFindSelectionList = editPage.GetTextFindSelectionList();
- for (int i = editTextFindSelectionList.Count - 1; i >= 0; i--)
- {
- if (editTextFindSelectionList.Count == 0)
- {
- continue;
- }
- changeCount += editTextFindSelectionList.Count;
- editPage.FindText(findText, options);
- editPage.ReplaceText(editTextFindSelectionList[i], replaceText);
- }
- }
- ResetFindPlaceStatus();
- SelectedEditAreaForIndex(-1, -1);
- PDFViewer.UpdateRenderFrame();
- StartFindText(findText, options);
- return 0;
- }
- private void ResetFindPlaceStatus()
- {
- currentSelectionIndex = -1;
- tempPageIndex = -1;
- isOtherPage = false;
- isReplaced = false;
- canLoop = true;
- }
- }
- }
|