Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sat May 16, 2020 10:10 am

I'm getting errors or failure to find when using FindText with a search term that contains certain special characters, specifically: \ ^ and | (a subset of the regex reserved character list), Including these characters in the search phrase causes the errors/exceptions below. The workaround is that if the characters are escaped (\\,\^ and \|) then the function works as intended.

Regex Test \ Exception: parsing "Regex\s{0,1}Test\s{0,1}\" - Illegal \ at end of pattern.
Regex Test ^ phrase not found.
Regex Test | Exception: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


The code to demonstrate/recreate these issues is below:

Code: Select all
[TestClass]
    public class UnitTest1
    {
        private readonly string[] strShortList =
        {
            @"Regex Test \",
            @"Regex Test ^",
            @"Regex Test |",
            @"Regex Test\",
            @"Regex Test^",
            @"Regex Test|",
            @"\Regex Test",
            @"^Regex Test",
            @"|Regex Test"
        };

        [TestMethod]
        public void TestMethod1()
        {
            string strFileName = @".\Special Characters.pdf";
            Spire.Pdf.PdfDocument pdf = new PdfDocument(strFileName);

            TextFindParameter tfp = new TextFindParameter();
            tfp |= TextFindParameter.CrossLine;
            //tfp |= TextFindParameter.WholeWord;

            foreach (string strCurrent in strShortList)
            {
                try
                {
                    PdfTextFind[] pdfResult = pdf.Pages[0].FindText(strCurrent, tfp).Finds;

                    Console.WriteLine(pdfResult.Length > 0 ? "{0} phrase Found" : "{0} phrase not found.", strCurrent);
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception: {1}", strCurrent, e.Message);
                }

            }

            pdf.Dispose();
        }
    }

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Sun May 17, 2020 6:29 am

As a follow-up page.FindText when searching for a phrase like Regex \\ Test (spaces before and after the escaped \) does not distinguish between Regex \ Test (with spaces) and Regex\Test without spaces, in the PDF document. The same seems to be true for all regex reserved characters.

The need to escape the characters is also removed when searching for a phrase that doesn't include spaces when the TextFindParameter.WholeWord is set.

Code: Select all
        private readonly string[] strShortList =
        {
            @"Regex \\ Test",
            @"Regex \^ Test",
            @"Regex \| Test"
        };

        [TestMethod]
        public void SpecialCharsPDF()
        {
            string strFileName = @"\\Mac\Dropbox\Source\source\Grain Silo\Test Data\Special Characters.pdf";
            string strTarget = @"\\Mac\Dropbox\Source\source\Grain Silo\Test Data\Highlighted\Special Characters.pdf";
            Spire.Pdf.PdfDocument pdf = new PdfDocument(strFileName);

            TextFindParameter tfp = new TextFindParameter();
            tfp |= TextFindParameter.CrossLine;
            //tfp |= TextFindParameter.WholeWord;


            foreach (string strCurrent in strShortList)
            {
                try
                {
                    PdfTextFind[] pdfResult = pdf.Pages[0].FindText(strCurrent, tfp).Finds;

                    Console.WriteLine(pdfResult.Length > 0 ? "{0} phrase Found" : "{0} phrase not found.", strCurrent);

                    if (pdfResult.Length > 0)
                    {
                        foreach (PdfTextFind ptfCurrent in pdfResult)
                        {
                            ptfCurrent.ApplyHighLight(System.Drawing.Color.Gold);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception: {1}", strCurrent, e.Message);
                }

            }

            pdf.SaveToFile(strTarget);

            pdf.Dispose();
        }

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Mon May 18, 2020 10:57 am

Hello,

Thanks for your inquiry.
For these special characters , yes, as you know that they need to be escaped to find. At the same time, when using "Regex \\ Test", I did reproduce the issue you mentioned that the "Regex\Test " without space also were founded. This issue has been logged into our bug tracking system with the ticket SPIREPDF- 3268 for further investigating. If there is any update, we will inform you. Sorry for the inconvenience caused.

Sincerely,
Sara
E-iceblue support team
User avatar

Sara.Yang
 
Posts: 33
Joined: Wed May 06, 2020 1:05 am

Sat May 23, 2020 11:37 am

Thanks for raising this issue. Can you also confirm that you have raised the inconsistent escaping requirement?

To summarize, escape characters are required for a subset of the regex reserved characters when performing a non-regex search. The subset of characters are list in the previous post. Other regex characters do not require escaping.

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Mon May 25, 2020 10:11 am

Hello,

Thanks for your feedback.
Yes, our Dev team is also looking into that if escape characters could not be required when performing a non-regex search. Once there is any progress, we will let you know asap.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Mon May 25, 2020 10:38 am

:D Excellent thank you.

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Tue May 26, 2020 9:30 am

Hello,

I got the news from our Dev team that they have found the caused reason of these issues. They are fixing them now. Once they are fixed, we will provide the hotfix version for you asap.
Thanks for your patience.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Tue Jul 14, 2020 7:30 am

Hello,

Glad to inform that we just released the official Spire.Office Platinum(Hotfix) Version:5.7.3 which fixed your previous issues, welcome to download it from the following links to test.
Website link: https://www.e-iceblue.com/Download/download-office-for-net-now.html
Nuget link: https://www.nuget.org/packages/Spire.Office/

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Wed Jul 15, 2020 12:55 pm

Thanks Lisa, confirming that I have tested this and the issue is resolved with 5.7.3. Thanks for your help.

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Thu Jul 16, 2020 1:10 am

Hello,

Glad to hear that!
If you encounter any issue related to our products in the future, just feel free to contact us. Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF

cron