|
@@ -1,175 +0,0 @@
|
|
|
-using ComPDFKit.PDFDocument;
|
|
|
-using ComPDFKit.PDFPage;
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.IO;
|
|
|
-using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
-
|
|
|
-namespace SearchAndReplaceTest
|
|
|
-{
|
|
|
- /// <summary>
|
|
|
- /// This class contains the main entry point and methods for the SearchAndReplaceTest program.
|
|
|
- /// </summary>
|
|
|
- internal class SearchAndReplaceTest
|
|
|
- {
|
|
|
- private static string parentPath =
|
|
|
- Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))));
|
|
|
- private static string outputPath = Path.Combine(parentPath, "Output", "CS");
|
|
|
-
|
|
|
- static void Main(string[] args)
|
|
|
- {
|
|
|
- #region Perparation work
|
|
|
- Console.WriteLine("Running search and replace test sample…\r\n");
|
|
|
-
|
|
|
- SDKLicenseHelper.LicenseVerify();
|
|
|
- CPDFDocument document;
|
|
|
-
|
|
|
- if (!Directory.Exists(outputPath))
|
|
|
- {
|
|
|
- Directory.CreateDirectory(outputPath);
|
|
|
- }
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region Sample 1: Search
|
|
|
-
|
|
|
- document = CPDFDocument.InitWithFilePath("ComPDFKit_Sample_File_Windows.pdf");
|
|
|
-
|
|
|
- if (GetSearchResult(document))
|
|
|
- {
|
|
|
- Console.WriteLine("Get Search result done.");
|
|
|
- }
|
|
|
-
|
|
|
- document.Release();
|
|
|
-
|
|
|
- Console.WriteLine("--------------------");
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region Sample 2: Replace Text
|
|
|
-
|
|
|
- document = CPDFDocument.InitWithFilePath("ComPDFKit_Sample_File_Windows.pdf");
|
|
|
-
|
|
|
- if (ReplaceText(document))
|
|
|
- {
|
|
|
- Console.WriteLine("Replace selected words done.");
|
|
|
- }
|
|
|
-
|
|
|
- document.Release();
|
|
|
-
|
|
|
- Console.WriteLine("--------------------");
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region Replace all
|
|
|
- document = CPDFDocument.InitWithFilePath("ComPDFKit_Sample_File_Windows.pdf");
|
|
|
-
|
|
|
- if (ReplaceAll(document))
|
|
|
- {
|
|
|
- Console.WriteLine("Replace all wards done.");
|
|
|
- }
|
|
|
- document.Release();
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- Console.WriteLine("Done!");
|
|
|
- Console.WriteLine("--------------------");
|
|
|
- Console.ReadLine();
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// This method retrieves the search results in the given PDF document.
|
|
|
- /// </summary>
|
|
|
- /// <param name="document">The PDF document to search in.</param>
|
|
|
- /// <returns>True if the search results are retrieved successfully, otherwise false.</returns>
|
|
|
- private static bool GetSearchResult(CPDFDocument document)
|
|
|
- {
|
|
|
- CPDFPage page = document.PageAtIndex(1);
|
|
|
- CPDFEditPage editPage = page.GetEditPage();
|
|
|
- editPage.BeginEdit(CPDFEditType.EditText);
|
|
|
- editPage.TextEditFindStart("ComPDF", C_Search_Options.Search_Case_Insensitive);
|
|
|
- Console.WriteLine("Get {0} results", editPage.GetFindSelectionsCount());
|
|
|
- editPage.EndEdit();
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// This method replaces the specified text in the given PDF document.
|
|
|
- /// </summary>
|
|
|
- /// <param name="document">The PDF document to perform the text replacement in.</param>
|
|
|
- /// <returns>True if the text is replaced successfully, otherwise false.</returns>
|
|
|
- private static bool ReplaceText(CPDFDocument document)
|
|
|
- {
|
|
|
- string path = Path.Combine(outputPath, "ReplaceText.pdf");
|
|
|
-
|
|
|
- int pageIndex = 0;
|
|
|
- int count = 0;
|
|
|
- CPDFPage page;
|
|
|
- CPDFEditPage editPage;
|
|
|
-
|
|
|
- while (pageIndex < document.PageCount)
|
|
|
- {
|
|
|
- page = document.PageAtIndex(pageIndex);
|
|
|
- editPage = page.GetEditPage();
|
|
|
- editPage.BeginEdit(CPDFEditType.EditText);
|
|
|
- editPage.TextEditFindStart("ComPDF", C_Search_Options.Search_Case_Insensitive);
|
|
|
-
|
|
|
- if (!(editPage.GetFindSelectionsCount() > 0))
|
|
|
- {
|
|
|
- editPage.TextEditFindClose();
|
|
|
- editPage.EndEdit();
|
|
|
- pageIndex++;
|
|
|
- continue;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- while (editPage.FindSelectionIndex < editPage.GetFindSelectionsCount())
|
|
|
- {
|
|
|
- editPage.ReplaceSelectedText("ABCDE");
|
|
|
- Console.WriteLine("Text {0} replacement completed.", count++);
|
|
|
- editPage.TextEditFindClose();
|
|
|
- int oldFindSelectionIndex = editPage.FindSelectionIndex;
|
|
|
- editPage.TextEditFindStart("ComPDF", C_Search_Options.Search_Case_Insensitive);
|
|
|
- editPage.FindSelectionIndex = oldFindSelectionIndex;
|
|
|
- }
|
|
|
- editPage.TextEditFindClose();
|
|
|
- editPage.EndEdit();
|
|
|
- pageIndex++;
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Console.WriteLine("Browse the changed file in " + path);
|
|
|
- return document.WriteToFilePath(path);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// This method replaces all occurrences of the specified text in the given PDF document.
|
|
|
- /// </summary>
|
|
|
- /// <param name="document">The PDF document to perform the text replacement in.</param>
|
|
|
- /// <returns>True if all occurrences of the text are replaced successfully, otherwise false.</returns>
|
|
|
- private static bool ReplaceAll(CPDFDocument document)
|
|
|
- {
|
|
|
- string path = Path.Combine(outputPath, "ReplaceAll.pdf");
|
|
|
-
|
|
|
- for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
|
|
|
- {
|
|
|
- CPDFPage page = document.PageAtIndex(pageIndex);
|
|
|
- CPDFEditPage editPage = page.GetEditPage();
|
|
|
- editPage.BeginEdit(CPDFEditType.EditText);
|
|
|
-
|
|
|
- editPage.TextEditFindStart("ComPDF", C_Search_Options.Search_Case_Insensitive);
|
|
|
- Console.WriteLine("Page {0} has {1} text to replace.", pageIndex + 1, editPage.GetFindSelectionsCount());
|
|
|
-
|
|
|
- editPage.ReplacePageText("ABCDE");
|
|
|
- editPage.EndEdit();
|
|
|
-
|
|
|
- Console.WriteLine("Page {0} replacement completed.", pageIndex + 1);
|
|
|
- }
|
|
|
- Console.WriteLine("Browse the changed file in " + path);
|
|
|
- return document.WriteToFilePath(path);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|