System.ArgumentException: 'parsing "(56SV4003" - Not enough )'s.'

Hi!

I’ve met strange problem: code line “pdfDocument.Pages.Accept(textFragmentAbsorber);” gives the error " System.ArgumentException: 'parsing “(56SV4003” - Not enough )‘s.’ ".

Error appear (and it’s simple to reproduce) when the value of origString=(56SV4003

It seems like a bug in Aspose.Pdf library.

Here is the code:

public static int replaceStringInPDF(Aspose.Pdf.Document pdfDocument, string origString, string newString)
{
// Get the extracted text fragments
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(origString);

        // Set text search option to specify regular expression usage
        TextSearchOptions textSearchOptions = new TextSearchOptions(true);
        textFragmentAbsorber.TextSearchOptions = textSearchOptions;
        // Accept the absorber for all pages
        pdfDocument.Pages.Accept(textFragmentAbsorber);  // <= ERROR

        // Get the extracted text fragments
        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
        int replacements = textFragmentCollection.Count;
        // Loop through the fragments
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            textFragment.Text = newString;
        }
        textFragmentCollection.Clear();
        return replacements;
    }

And we can’t modify original text value: it must be replaced as it is. Any ways to fix / avoid this kind of issues?

Best regards,
Alexander

@Alexander.Kuznetsov,
Can you please share the source PDF file so that we may try to reproduce the same on our end.

The key point of my post is that it doesnt matter what is the pdf file, aspose.pdf gives the error when you are trying to feed it with the very specific string. In my case string value = (56SV4003 . So, it fails when string contains open bracket and no close bracket.

Anyway, file is attached (dummy empty pdf-file :slight_smile: ). And here is the simplified code to reproduce the bug:

private void bugButton_Click(object sender, EventArgs e)
{
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(@“C:%myLicLocation%\Aspose.PDF.NET.lic”);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@“c:\temp\EmptyPDF.pdf”);

         TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("(56SV4003");
         TextSearchOptions textSearchOptions = new TextSearchOptions(true);
         textFragmentAbsorber.TextSearchOptions = textSearchOptions;
         pdfDocument.Pages.Accept(textFragmentAbsorber);

         pdfDocument.Dispose();
     }

EmptyPDF.pdf (176.3 KB)

@Alexander.Kuznetsov,
Thank you for sharing the requested information. You are using TextSearchOptions Constructor (Boolean) which specifies regular expression usage mode. Where the string in double quotes is not a valid regular expression. You need to search this text string so please comment out two lines and try following code:

private void bugButton_Click(object sender, EventArgs e)
{
    Aspose.Pdf.License license = new Aspose.Pdf.License();
    license.SetLicense(@“C:%myLicLocation%\Aspose.PDF.NET.lic”);
    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@“c:\temp\EmptyPDF.pdf”);

    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("(56SV4003");
    //TextSearchOptions textSearchOptions = new TextSearchOptions(true);
    //textFragmentAbsorber.TextSearchOptions = textSearchOptions;
    pdfDocument.Pages.Accept(textFragmentAbsorber);

    pdfDocument.Dispose();
}

It explains everything! Problem is solved, thank you :slight_smile:

@Alexander.Kuznetsov,
You are welcome.